Linking to lists in a swing

I am working on a desktop (swing) with the Eclipse IDE. I have three collections (countries, states and cities), and I need to update the data automatically when choosing a new country or province. I was looking for a lot of information, but all the implementations I found are made on Ajax or in the beansbinding framework in NetBeans. I tried the ItemEvent solution, but I have problems running the application, it loads a list of countries, but not other lists. And when choosing a country, a list of states is charged, but not a list of cities.

My code is:

jComboBoxCountries.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { jComboBoxStates.setModel(new javax.swing.DefaultComboBoxModel( statesOf(evt.getItem()).toArray() )); } }); jComboBoxStates.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { jComboBoxCities.setModel(new javax.swing.DefaultComboBoxModel( citiesOf(evt.getItem()).toArray()) ); } }); jComboBoxCountries.setModel(new javax.swing.DefaultComboBoxModel( countryList.toArray())); 
+2
source share
1 answer

I have problems starting the application, it loads a list of countries, but not other lists

It seems you need to specifically set the selected index to call the listener.

 jComboBoxCountries.setModel(...) jComboBoxCountries.setSelectedIndex(0); 

And when choosing a country, a list of states is charged, but not a list of cities.

I would suggest that this is the same problem, once you reset the model from the state lists, you will also need to select its index.

Another approach is to not select the default state or city, but instead ask the user for their choice. Here is the code that uses this approach:

 import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class ComboBoxTwo extends JFrame implements ActionListener { private JComboBox mainComboBox; private JComboBox subComboBox; private Hashtable subItems = new Hashtable(); public ComboBoxTwo() { String[] items = { "Select Item", "Color", "Shape", "Fruit" }; mainComboBox = new JComboBox( items ); mainComboBox.addActionListener( this ); // prevent action events from being fired when the up/down arrow keys are used // mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE); getContentPane().add( mainComboBox, BorderLayout.WEST ); // Create sub combo box with multiple models subComboBox = new JComboBox(); subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4 getContentPane().add( subComboBox, BorderLayout.EAST ); String[] subItems1 = { "Select Color", "Red", "Blue", "Green" }; subItems.put(items[1], subItems1); String[] subItems2 = { "Select Shape", "Circle", "Square", "Triangle" }; subItems.put(items[2], subItems2); String[] subItems3 = { "Select Fruit", "Apple", "Orange", "Banana" }; subItems.put(items[3], subItems3); mainComboBox.setSelectedIndex(1); } public void actionPerformed(ActionEvent e) { String item = (String)mainComboBox.getSelectedItem(); Object o = subItems.get( item ); if (o == null) { subComboBox.setModel( new DefaultComboBoxModel() ); } else { subComboBox.setModel( new DefaultComboBoxModel( (String[])o ) ); } } public static void main(String[] args) { JFrame frame = new ComboBoxTwo(); frame.setDefaultCloseOperation( EXIT_ON_CLOSE ); frame.pack(); frame.setLocationRelativeTo( null ); frame.setVisible( true ); } } 
+4
source

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


All Articles