开发者

java JComboBox issue

I was trying to have two Jcomboxes, where second Jcombox should changes its values according to the change in the first one. I tried but could not succeed,Any help is appreciated. Thanks

This is what I have tried so Far:

public class SharedDataBetweenComboBoxSample {

    static private String selectedString(ItemSelectable is) {
        Object selected[] = is.getSelectedObjects();
        return ((selected.length == 0) ? "null" : (String)selected[0]);
    }

    public static void main(String args[]) {
        final String labels[] = { "A", "B", "C" };
        final String labelsA[] = { "A", "AA", "AAA" };
        final String labelsB[] = { "B", "BB", "BBB" };
        final String labelsC[] = { "C", "CC", "CCC" };

        final JFrame frame = new JFrame("Shared Data");
      开发者_StackOverflow  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        JComboBox comboBox1 = new JComboBox();
        comboBox1.addItem(labels);
        comboBox1.setSelectedItem(null);

        final JComboBox comboBox2 = new JComboBox();
        // comboBox2.setEditable(true);
        panel.add(comboBox1);
        panel.add(comboBox2);
        frame.add(panel,BorderLayout.NORTH);

        ItemListener itemListener = new ItemListener() {
            public void itemStateChanged(ItemEvent itemEvent) {
                int state = itemEvent.getStateChange();
                System.out.println((state == ItemEvent.SELECTED) ? "Selected" : "Deselected");
                System.out.println("Item: " + itemEvent.getItem());
                ItemSelectable is = itemEvent.getItemSelectable();
                System.out.println(", Selected: " + selectedString(is));
                if (selectedString(is) == "B") {
                    comboBox2.addItem(labelsB);
                    // frame.add(comboBox1, BorderLayout.CENTER);
                } else if (selectedString(is) == "A") {
                    comboBox2.addItem(labelsA);
                    // frame.add(comboBox1, BorderLayout.CENTER);
                } else if (selectedString(is) == "C") {
                    comboBox2.addItem(labelsC);
                    // frame.add(comboBox1, BorderLayout.CENTER);
                } else {
                    comboBox2.setSelectedItem(null);
                    // frame.add(comboBox1, BorderLayout.CENTER);
                }
            }

        };
        comboBox1.addItemListener(itemListener);

        frame.setSize(300,200);
        frame.setVisible(true);
    }
}


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 );

  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);
 }

 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 );
  }
}


not really sure what your problem is as you don't say. Maybe the issue is

comboBox2.addItem(labelsB);

this is adding an array as a single item in the list, perfectly acceptable assuming it is right, however i'm guessing you want to iterate through the array and add each one as separate items. You may want to remove items on deselection.

I assume you are trying from multiple selections on the first list (based on your selectedString operation), if not your code is miles out? If you are you do not want an if/else construct, just multiple ifs

Also, don't use (selectedString(is)=="A"), you might get lucky, but you should use `"A".equals(selectedString(is))

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜