This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
Populating JComboBoxes with JLists(Help!!)
Posted by Matthew Keating on April 21, 2001 at 2:17 AM
I have an assignment where I have to add items to a JComboBox whenever an item of a string array from a JList is selected. How do I use getSelectedValues to get it to work. An examplewould be greatly appreciated. Check the code( it's messy because I got a little lost so I commented a lot out): import java.awt.*; import java.awt.event.*; import com.sun.java.swing.*; import com.sun.java.swing.event.*; import com.ms.security.*; public class MidTerm extends MyFrameWithExitHandling implements ListSelectionListener // , ActionListener // , ItemListener // { private JList jlst; private JComboBox jcboSoP = new JComboBox(); private JTextField jtfResult = new JTextField(32); Dimension dim = new Dimension(); ListSelectionModel lstMod ; private String stateprov; private PermissionUtils perm; private String[]stateprovs ={"WASHINGTON","CALIFORNIA", "QUEBEC","ONTARIO"}; //Main method public static void main(String[] args) { MidTerm frame = new MidTerm(); frame.setSize(500,500); frame.pack(); frame.center(); frame.setVisible(true); } //Constructor public MidTerm() { setTitle(" Mid Term"); JPanel p1 = new JPanel(); p1.setLayout(new FlowLayout(FlowLayout.LEFT)); p1.add(new JLabel("Result")); p1.add(jtfResult); JPanel p3 = new JPanel(); p3.setLayout(new FlowLayout(FlowLayout.LEFT)); p3.add(new JLabel("State or Province")); jlst = new JList(stateprovs); JScrollPane jsp = new JScrollPane(jlst); jlst.setVisibleRowCount(3); jlst.setSelectedIndex(0); p3.add(jsp); jlst.setSelectionMode(lstMod.SINGLE_INTERVAL_SELECTION); p3.add(new JLabel ("City")); p3.add(jcboSoP = new JComboBox()); //Initialize Choice items //jcboSoP.addItem ("SEATTLE"); //jcboSoP.addItem ("LA"); //jcboSoP.addItem ("SAN FRANSISCO"); //jcboSoP.addItem ("MONTREAL"); //jcboSoP.addItem("TORONTO"); //jcboSoP.addItem ("OTTAWA"); //jcboSoP.setSelectedIndex (1); //jcboSoP.removeAll(); JPanel jpAll = new JPanel(); jpAll.setLayout(new GridLayout (3,1)); jpAll.add(p1); jpAll.add(p3); getContentPane().setLayout(new BorderLayout()); getContentPane().add(jpAll,BorderLayout.CENTER); jlst.addListSelectionListener(this); //jcboSoP.addActionListener(this); } public void valueChanged(ListSelectionEvent e) { JList jlst = (Jlist)e.getSource(); Object values = jlst.getSelectedValues //stateprovs = new String []; //Object[] values = jlst.getSelectedValues(); //stateprovs = new String [values.length]; //stateprovs = perm.ArraytoString(values).trim(); // if ( stateprovs.length() > 1) //stateprovs = stateprovs.substring(1,state.length() - 2).trim(); jcboSoP.addItem ("SEATTLE"); //JList jlst = (JList)e.getSource(); //Object[] values = jlst.getSelectedValues(); //stateprovs = new String [values.length]; //for (int a=0; a //stateprovs[a] = (String)values[a]; //jcboSoP.addItem ("SEATTLE"); //stateprovs = new String [values.length]; //for (int b=1; b //stateprovs[b] = (String)values[b]; //Object[] values = jlst.getSelectedValues()[1]; //jcboSoP.addItem ("LA"); //for (int b=1; b //stateprovs[b] = (String)values[b]; //jcboSoP.addItem ("LA"); //jcboSoP.addItem ("SAN FRANSISCO"); //public void actionPerformed (ActionEvent e) //{ // JComboBox jcboSoP = (JComboBox)e.getSource(); // if (jcboSoP.getSelectedItem // = "SEATTLE") // jtfResult.setText = "SEATTLE"; // else if (jcboSoP.getSelectedItem // = "LA") // jtfResult.setText = "LA"; } }
Replies:
|