Display immutable default value for JComboBox

I have a JComboBox that contains three elements {"Personel", "Magasinier", "Fournisseur"} .

I want this JComboBox display the value "Choisir une option :" , which is a non- "Choisir une option :" value.

I tried this code after initComponents(); :

 this.jComboBox1.setSelectedItem("Choisir une option :"); 

but that will not work.

How can i do this?

+4
source share
2 answers

You can override the selection code in your JComboBox model with a code, for example, the following SSCCE:

 public class JComboExample { private static JFrame frame = new JFrame(); private static final String NOT_SELECTABLE_OPTION = " - Select an Option - "; private static final String NORMAL_OPTION = "Normal Option"; public static void main(String[] args) throws Exception { JComboBox<String> comboBox = new JComboBox<String>(); comboBox.setModel(new DefaultComboBoxModel<String>() { private static final long serialVersionUID = 1L; boolean selectionAllowed = true; @Override public void setSelectedItem(Object anObject) { if (!NOT_SELECTABLE_OPTION.equals(anObject)) { super.setSelectedItem(anObject); } else if (selectionAllowed) { // Allow this just once selectionAllowed = false; super.setSelectedItem(anObject); } } }); comboBox.addItem(NOT_SELECTABLE_OPTION); comboBox.addItem(NORMAL_OPTION); frame.add(comboBox); frame.pack(); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { frame.setVisible(true); } }); } } 

This will display a combo box with the string selection " - Select an Option - ". Once the user selects another option, it will be impossible to select the original option again.

+4
source

I came across this question and made some changes to Duncan's answer. My solution looks like this:

 import javax.swing.DefaultComboBoxModel; import javax.swing.JComboBox; public class JEComboBox<T> extends JComboBox<T> { public JEComboBox(final T placeHolder){ setModel(new DefaultComboBoxModel<T>() { private static final long serialVersionUID = 1L; boolean selectionAllowed = true; @Override public void setSelectedItem(Object anObject) { if (!placeHolder.equals(anObject)) { super.setSelectedItem(anObject); } else if (selectionAllowed) { // Allow this just once selectionAllowed = false; super.setSelectedItem(anObject); } } }); addItem(placeHolder); } } 

When you add a place owner, you create an anonymous object and override the toString method. An implementation might look like this:

 public class car{ String final model; public car(String model){ this.model = model; } } 

and creating a JEComboBox:

 JEComboBox comboBoxWithPlaceHolder = new JEComboBox<Car>(new Car{ public String toString(){ return "- Select your car -" } }); 

Arguments

  • Combobox is a common

vs

  • You need to implement an anonymous subtype of the T method and Override toString () and, therefore, will not work with final classes (it may be messy if the comboBox contains classes that inherit from the interface, since the anonymous subtype must implement the interface, so there will be no return methods .)
+2
source

Source: https://habr.com/ru/post/1481897/


All Articles