Before continuing
3. combo box (the JComboBox)
Example: use JComboBox choose to design a city program.
Import in java.awt *. ; Import in javax.swing *. ; public class JComboBoxExample the extends the JFrame { // define two combo boxes the JComboBox the comboBox1, comboBox2; // set a character array String cityNames [] = { "Beijing", "Tianjin "," Shanghai "," Nanjing "," Chongqing "," Wuhan "," Hangzhou " }; // constructor for creating public JComboBoxExample () { // reference to the parent class Super (" combo box " ); Container Container = getContentPane (); container.setLayout ( new new the FlowLayout ()); // create a combo box with a list of the contents of the array definition = the comboBox1 new new the JComboBox (cityNames); // Set default option to option 4, as the first character in the array is cityNames [0] comboBox1.setSelectedIndex (. 3 ); // set the option is not available, unavailable here meaning can not be edited, instead of disabling comboBox1.setEditable ( to false ); comboBox2 = new new the JComboBox (cityNames); comboBox2.setSelectedItem (cityNames [ . 1 ]); comboBox2.addItem ( new new String ( "Changsha")); // in combination adding selection box Changsha comboBox2.setEditable ( to true ); Container.add (the comboBox1); Container.add (comboBox2); //调整窗口大小 pack(); setVisible(true); } public static void main(String[] args) { // TODO Auto-generated method stub JComboBoxExample jcbe=new JComboBoxExample(); jcbe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
operation result:
Open the drop-down box:
Open another drop-down box:
Then discuss comboBox1.setEditable (false); this usage, some people think there is no difference write true or false, but it is still there, when this place is false:
We can see it at the bottom color is white, before the blue. As well as how he can not do anything to disable it, through my research, using comboBox1.enable (false) can be achieved, and remember brackets must be false, if not, then the execution result and comboBox1.setEditable ( false) makes no difference.
This is the result of running:
Nanjing this drop-down box completely unable to move.
Well, today on this, and very happy to learn and share experiences together.