我将JComboBox做了一些改动(即第2段代码,没改动过的可以),然后想得到我所选择项的值(也就是文字),但是我试了好几种方法,都没有办法将选项值取出。所以发贴问问大家,我只找到了一个替代方法,就是读取 SelectedIndex 然后再对比再输出,那我还是希望得到的是其String 得形式,下面代码的方法1 就是得到数字,方法2就是得到String但是会出错。
public class Test2 extends JFrame {
static Object[][] item = {{"A", Color.BLUE, Color.WHITE},{"B", Color.BLUE, Color.WHITE}, 
{"C", Color.BLUE, Color.WHITE}};
static JComboBox comboBox = null;

public Test2(){
comboBox = new JComboBox(item);
comboBox.setRenderer(new ComboBoxListCellRenderer());
comboBox.setBackground(Color.white);
this.add(comboBox, BorderLayout.NORTH);

comboBox.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//方法1: int s = ((JComboBox)e.getSource()).getSelectedIndex();
//方法2: String s = (String)((JComboBox)e.getSource()).getSelectedItem();
// System.out.println(s);
}
});
}

public static void main(String[] args){
Test2 test2 = new Test2();
test2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        test2.setSize(200, 100);
        test2.setVisible(true);
}
}public class ComboBoxListCellRenderer implements ListCellRenderer{
DefaultListCellRenderer defaultRanderer = new DefaultListCellRenderer(); public Component getListCellRendererComponent(JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus){

String theText = null;
Color backGround = null;
Color foreGround = null;

JLabel renderer = (JLabel)defaultRanderer.getListCellRendererComponent(list, 
value, index, isSelected, cellHasFocus);
if(value instanceof Object[]){
Object[] values = (Object[])value;
theText = (String)values[0];
backGround = (Color)values[1];
foreGround = (Color)values[2];
}
if(isSelected){
renderer.setBackground(backGround);
renderer.setForeground(foreGround);
}
renderer.setText(theText);
list.setSelectionBackground(Color.WHITE);
return renderer;
}
}

解决方案 »

  1.   

    getSelectedItem
    public Object getSelectedItem()返回当前所选项。 
    如果组合框可编辑,则可能尚未使用 addItem、insertItemAt 或数据构造方法将此值添加到组合框中。 
    返回:
    当前选择的 Object
    另请参见:
    setSelectedItem(java.lang.Object)
    ----------------------------------------------------------------------------------------------------------------------
    setRenderer
    public void setRenderer(ListCellRenderer aRenderer)设置渲染器,该渲染器用于绘制列表项和从 JComboBox 字段的列表中选择的项。该渲染器在 JComboBox 不可编辑时使用。如果其可编辑,则使用编辑器呈现和编辑所选项。 
    默认渲染器显示字符串或图标。其他渲染器可处理图形图像和复合选项。 要显示所选项,请调用 aRenderer.getListCellRendererComponent,其中传递列表对象和索引 -1。 
    参数:
    aRenderer - 显示所选项的 ListCellRenderer
    另请参见:
    setEditor(javax.swing.ComboBoxEditor)
      

  2.   

    回复楼上的,我的理解能力不强,能不能说的更具体点~~谢谢~(我的JComboBox是不能编辑的)。
      

  3.   

    jComboBox.getSelectedItem().toString()
    试试看
      

  4.   

    回复4楼,toString 返回的是对象的字符串表现形式,我要返回的是我所添加的项,我一共添加了A B C 所以我想从中返回的仍旧是 A B C
      

  5.   

    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.BorderLayout;
    import java.awt.Color;
    public class Test2 extends JFrame {
    static Object[] a={"A", Color.BLUE, Color.WHITE};
    static Object[] b={"B", Color.BLUE, Color.WHITE};
    static Object[] c={"C", Color.BLUE, Color.WHITE};
        static Object[][] item = {a,b,c};
        static JComboBox comboBox = null;
        
        public Test2(){
            comboBox = new JComboBox(item);
            comboBox.setRenderer(new ComboBoxListCellRenderer());
            comboBox.setBackground(Color.white);
            this.add(comboBox, BorderLayout.NORTH);
            
            comboBox.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                 // int t = ((JComboBox)e.getSource()).getSelectedIndex();
                 Object[] tem =(Object[])((JComboBox)e.getSource()).getSelectedItem();
                 String s=tem[0].toString();
                  System.out.println("选中项对象:"+tem.toString());
                  System.out.println("选中项值:"+s);
                }
            });
        }
        
        public static void main(String[] args){
        System.out.println ("A项对象:"+a.toString());
        System.out.println ("B项对象:"+b.toString());
        System.out.println ("C项对象:"+c.toString());
            Test2 test2 = new Test2();
            test2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            test2.setSize(200, 100);
            test2.setVisible(true);    }
    }其实当你调用getSelectedItem()他返回的的确是你选中的项,只不过是一个数组,比如你选中B,那么返回Object[] b={"B", Color.BLUE, Color.WHITE};这个对象数组