java - Dynamically Updating JComboBox Items when another ComoboBox Changes Indexes -
I have a swing UI, which has two combo boxes, first by the string coming from an array of obje 1, And the second combo box needs to be populated with the strings stored in an array within that object.
Here's an example to better understand. You have a school course, and each course can have multiple sections. After choosing a course in a combo box, I want to populate the combo box for classes with sections that are associated with that course.
Modify the model of the first combo box when there is an event in a combo box something like this:
import java.awt. *; Import java.awt.event. *; Import java.util. *; Import javax.swing. *; PublicLayer combo box suit applies to ZPNL ActionListener {Private JComboBox & lt; String & gt; MainComboBox; Private JComboBox & lt; String & gt; SubComboBox; Private hashtable & lt; String, string []> SubItems = new hashtable & lt; String, string []> (); Public ComboBoxTwo () {string [] items = {"select item", "color", "size", "fruit"}; MainComboBox = new JComboBox & lt; String & gt; (item); mainComboBox.addActionListener (this); // Use Up / Down Arrow Keys to stop the action occurring when using in mainComboBox.putClientProperty ("JComboBox.isTableCellEditor", Boolean.true); Add (main combo box); // multiple models subComboBox = new JComboBox & lt; String & gt; () With a sub combo box; subComboBox.setPrototypeDisplayValue ("XXXXXXXXXX"); // JDK1.4 Add (subcombole box); String [] subItems1 = {"select color", "red", "blue", "green"}; subItems.put (items [1], subitems 1); String [] subItems2 = {"select shape", "circle", "squire", "triangle"}; subItems.put (item [2], upimes 2); String [] subItems3 = {"select fruit", "apple", "orange", "banana"}; subItems.put (item [3], subIstems 3); } Public Zero Impr. (ActionEvent E) {string item = (string) mainComboBox.getSelectedItem (); Object o = subtitles.gate (item); If (o == tap) {subComboBox.setModel (New DefaultComboBoxModel ()); } Else {subComboBox.setModel (new DefaultComboBoxModel ((string []) o);); }} Make personal static void endoshou () {Jeffre Frame = New Gefram ("SSCC"); Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Frame.add (new combo box two ()); Frame.set location placement platform (true); Frame.pack (); Frame.setVisible (true); } Public static zero main (string [] Args) {EventQueue.invokeLater (New Runnabal) (Public Zero Run) {createAndShowUI ();}}); }}
Comments
Post a Comment