Java 动态组合框问题

Java 动态组合框问题,java,swing,jtable,jcombobox,Java,Swing,Jtable,Jcombobox,嗨,我用的是一个有两列的表格,即仪器和方法。单击一个按钮,我将在每个单元格中添加一行,并使用组合框作为编辑器。此外,这些组合框将具有actionlistener。当我选择仪器时,方法组合框列表应该改变。我只使用两个组合框,每次添加一行时都会实例化它。我的问题是,每当我添加新行时,现有行组合都会重新加载最新的值。这意味着,组合框是唯一的,即使我以不同的方式实例化它。动态创建组合框的方法是什么,它应该有自己的值。在下面发布我的代码供您参考 addItem.addActionListener(new

嗨,我用的是一个有两列的表格,即仪器和方法。单击一个按钮,我将在每个单元格中添加一行,并使用组合框作为编辑器。此外,这些组合框将具有actionlistener。当我选择仪器时,方法组合框列表应该改变。我只使用两个组合框,每次添加一行时都会实例化它。我的问题是,每当我添加新行时,现有行组合都会重新加载最新的值。这意味着,组合框是唯一的,即使我以不同的方式实例化它。动态创建组合框的方法是什么,它应该有自己的值。在下面发布我的代码供您参考

addItem.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            comboInstrument = new CeNComboBox();
            comboMethod = new CeNComboBox();
            TableColumn instrumentColumn = table.getColumn("Instrument Used");
            TableColumn methodColumn = table.getColumn("Method Title");
            comboInstrument.removeAllItems();
            listInstruments = analyticalUtil.getInstruments(listAnalysisSummaryPrefs);
            iterateInstruments = listInstruments.iterator();
            while(iterateInstruments.hasNext()){
                comboInstrument.addItem(iterateInstruments.next());
            }
            dtm.addRow(new Object[]{" "," "});
            comboInstrument.setEditable(true);
            instrumentColumn.setCellEditor(new MyComboBoxEditor(comboInstrument));
            instrumentColumn.setCellRenderer(new MyComboBoxRenderer());
            comboMethod.setEditable(true);
            methodColumn.setCellEditor(new MyComboBoxEditor(comboMethod));
            methodColumn.setCellRenderer(new MyComboBoxRenderer());

            comboInstrument.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent argEvent){
                    comboInstrumentActionPerformed();
                }
            });
            comboMethod.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent argEvent){
                    comboMethodActionPerformed();
                }
            });
        }
    });
myComboxeditor.java

public class MyComboBoxEditor extends DefaultCellEditor {



    public MyComboBoxEditor(JComboBox combobox) {
        super(combobox);
    }
}
我对荡秋千很陌生。请帮帮我

添加一些带有硬编码值的示例代码。如果你运行这个,你就会理解我的问题。以这种方式进行测试。 1.从第一列、第一行中选择一个值,然后从另一列中选择值。 2.在第二行中执行同样的操作,现在检查第一行的第二列。所有值将根据为第二行所做的选择重新加载。 这就是我面临的问题。下面的代码是从以下链接复制和编辑的

private static final long serialVersionUID=1L;
专用JComboBox主组合框;
专用JComboBox子omboBox;
私有哈希表子项=新哈希表();
公共测试(){
字符串[]项={“,”选择项“,”颜色“,”形状“,”水果“,”大小“};
mainComboBox=新的JComboBox(项目);
mainComboBox.addActionListener(这个);
mainComboBox.addItemListener(此);
mainComboBox.setEditable(true);
//使用向上/向下箭头键时防止触发操作事件
//mainComboBox.putClientProperty(“JComboBox.isTableCellEditor”,Boolean.TRUE);
//getContentPane().add(mainComboBox,BorderLayout.WEST);
subComboBox=new JComboBox();//创建具有多个模型的子组合框
subComboBox.setPrototypeDisplayValue(“xxxxxxxxx”);//JDK1.4
subComboBox.addItemListener(此);
//getContentPane().add(subComboBox,BorderLayout.CENTER);
字符串[]子项1={“选择颜色”、“红色”、“蓝色”、“绿色”};
子项目。投入(项目[1],子项目1);
字符串[]子项2={“选择形状”、“圆”、“正方形”、“三角形”};
子项目。投入(项目[2],子项目2);
String[]subItems3={“选择水果”、“苹果”、“橘子”、“香蕉”};
子项目。投入(项目[3],子项目3);
字符串[]子项4={“选择大小”、“大”、“中”、“小”};
子项目。投入(项目[4],子项目4);
DefaultTableModel=new DefaultTableModel(新字符串[]{“使用的工具”,“方法标题”},0);
JTable table=新的JTable(模型);
table.getColumn(“使用的仪器”).setCellEditor(新的MyComboxEditor(mainComboBox));
table.getColumn(“使用的工具”).setCellRenderer(新的MyComboxRenderer());
table.getColumn(“方法标题”).setCellEditor(新的MyComboxEditor(子ComboBox));
table.getColumn(“方法标题”).setCellRenderer(新的MyComboxRenderer());
addRow(新字符串[]{”“});
addRow(新字符串[]{”“});
getContentPane().add(表,BorderLayout.CENTER);
}
已执行的公共无效操作(操作事件e){
String item=(String)mainComboBox.getSelectedItem();
JOptionPane.showMessageDialog(null,“已执行的操作”+项);
对象o=子项。获取(项);
如果(o==null){
setModel(新的DefaultComboxModel());
}否则{
setModel(新的DefaultComboxModel((字符串[])o));
}
}
公共无效itemStateChanged(ItemEvent e){
如果(如getStateChange()==ItemEvent.SELECTED){
如果(例如getSource()==mainComboBox){
如果(mainComboBox.getSelectedIndex()!=0){
FirstDialog FirstDialog=新建FirstDialog(Testing.this,
mainComboBox.getSelectedItem().toString(),“请稍候,正在搜索…”;
}
} 
}
}
私有类FirstDialog扩展JDialog{
私有静态最终长serialVersionUID=1L;
第一个对话框(最终帧父级、字符串winTitle、字符串msgString){
超级(家长,winTitle);
//setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
JLabel myLabel=新的JLabel(msgString);
JButton bNext=新JButton(“停止进程”);
添加(myLabel,BorderLayout.CENTER);
添加(bNext,BorderLayout.SOUTH);
bNext.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件evt){
setVisible(假);
}
});
javax.swing.Timer t=newjavax.swing.Timer(1000,newActionListener()){
已执行的公共无效操作(操作事件e){
setVisible(假);
}
});
t、 设置重复(假);
t、 start();
setLocationRelativeTo(父级);
设置尺寸(新尺寸(400100));
setVisible(真);
}
}
公共静态void main(字符串[]args){
JFrame=新测试();
frame.setDefaultCloseOperation(关闭时退出);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}

首先,您不需要每次调用
setCellEditor()
setCellRenderer()
private static final long serialVersionUID = 1L;
        private JComboBox mainComboBox;
        private JComboBox subComboBox;
        private Hashtable<Object, Object> subItems = new Hashtable<Object, Object>();

        public Testing() {
            String[] items = {"","Select Item", "Color", "Shape", "Fruit", "Size"};
            mainComboBox = new JComboBox(items);
            mainComboBox.addActionListener(this);
            mainComboBox.addItemListener(this);
            mainComboBox.setEditable(true);
            //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);
            subComboBox = new JComboBox();//  Create sub combo box with multiple models
            subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
            subComboBox.addItemListener(this);
//          getContentPane().add(subComboBox, BorderLayout.CENTER);
            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);
            String[] subItems4 = {"Select Size", "Big", "Middle", "Small"};
            subItems.put(items[4], subItems4);
            DefaultTableModel model = new DefaultTableModel(new String[]{"Instrument Used","Method Title"},0);
            JTable table = new JTable(model);
            table.getColumn("Instrument Used").setCellEditor(new MyComboBoxEditor(mainComboBox));
            table.getColumn("Instrument Used").setCellRenderer(new MyComboBoxRenderer());
            table.getColumn("Method Title").setCellEditor(new MyComboBoxEditor(subComboBox));
            table.getColumn("Method Title").setCellRenderer(new MyComboBoxRenderer());
            model.addRow(new String[]{""});
            model.addRow(new String[]{""});
            getContentPane().add(table, BorderLayout.CENTER);
        }

        public void actionPerformed(ActionEvent e) {
            String item = (String) mainComboBox.getSelectedItem();
            JOptionPane.showMessageDialog(null, "Action Performed "+item);
            Object o = subItems.get(item);
            if (o == null) {
                subComboBox.setModel(new DefaultComboBoxModel());
            } else {
                subComboBox.setModel(new DefaultComboBoxModel((String[]) o));
            }
        }

        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                if (e.getSource() == mainComboBox) {
                    if (mainComboBox.getSelectedIndex() != 0) {
                        FirstDialog firstDialog = new FirstDialog(Testing.this,
                                mainComboBox.getSelectedItem().toString(), "Please wait,  Searching for ..... ");
                    }
                } 
            }
        }

        private class FirstDialog extends JDialog {

            private static final long serialVersionUID = 1L;

            FirstDialog(final Frame parent, String winTitle, String msgString) {
                super(parent, winTitle);
                //setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
                JLabel myLabel = new JLabel(msgString);
                JButton bNext = new JButton("Stop Processes");
                add(myLabel, BorderLayout.CENTER);
                add(bNext, BorderLayout.SOUTH);
                bNext.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        setVisible(false);
                    }
                });
                javax.swing.Timer t = new javax.swing.Timer(1000, new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        setVisible(false);
                    }
                });
                t.setRepeats(false);
                t.start();
                setLocationRelativeTo(parent);
                setSize(new Dimension(400, 100));
                setVisible(true);
            }
        }

        public static void main(String[] args) {
            JFrame frame = new Testing();
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
public class Testing extends JFrame implements ItemListener{
    private static final long serialVersionUID = 1L;
        private JComboBox mainComboBox;
        private Hashtable<Object, Object> subItems = new Hashtable<Object, Object>();

        public Testing() {
            String[] items = {"","Select Item", "Color", "Shape", "Fruit", "Size"};
            mainComboBox = new JComboBox(items);
            mainComboBox.addItemListener(this);
            mainComboBox.setEditable(true);
            String[] subItems1 = {"Select Color", "Red", "Blue", "Green"};
            subItems.put(items[2], subItems1);
            String[] subItems2 = {"Select Shape", "Circle", "Square", "Triangle"};
            subItems.put(items[3], subItems2);
            String[] subItems3 = {"Select Fruit", "Apple", "Orange", "Banana"};
            subItems.put(items[4], subItems3);
            String[] subItems4 = {"Select Size", "Big", "Middle", "Small"};
            subItems.put(items[5], subItems4);
            DefaultTableModel model = new DefaultTableModel(new String[]{"Instrument Used","Method Title"},0);
            JTable table = new JTable(model);
            table.getColumn("Instrument Used").setCellEditor(new DefaultCellEditor(mainComboBox));
            //table.getColumn("Instrument Used").setCellRenderer(new MyComboBoxRenderer());
            table.getColumn("Method Title").setCellEditor(new MyComboBoxEditor());
            //table.getColumn("Method Title").setCellRenderer(new MyComboBoxRenderer());
            model.addRow(new String[]{""});
            model.addRow(new String[]{""});
            getContentPane().add(table, BorderLayout.CENTER);
        }

       public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                if (e.getSource() == mainComboBox) {
                    if (mainComboBox.getSelectedIndex() != 0) {
                        FirstDialog firstDialog = new FirstDialog(Testing.this,
                                mainComboBox.getSelectedItem().toString(), "Please wait,  Searching for ..... ");
                    }
                } 
            }
        }

        private class FirstDialog extends JDialog {

            private static final long serialVersionUID = 1L;

            FirstDialog(final Frame parent, String winTitle, String msgString) {
                super(parent, winTitle);
                //setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
                JLabel myLabel = new JLabel(msgString);
                JButton bNext = new JButton("Stop Processes");
                add(myLabel, BorderLayout.CENTER);
                add(bNext, BorderLayout.SOUTH);
                bNext.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        setVisible(false);
                    }
                });
                javax.swing.Timer t = new javax.swing.Timer(1000, new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        setVisible(false);
                    }
                });
                t.setRepeats(false);
                t.start();
                setLocationRelativeTo(parent);
                setSize(new Dimension(400, 100));
                setVisible(true);
            }
        }

        public static void main(String[] args) {
            JFrame frame = new Testing();
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }

    class MyComboBoxEditor extends AbstractCellEditor implements TableCellEditor, ItemListener {

            private JComboBox editorComboBox;

            public MyComboBoxEditor() {
                editorComboBox = new JComboBox();
            editorComboBox.addItemListener(this);
            }

            public Object getCellEditorValue()
            {
                return editorComboBox.getSelectedItem();
            }

            public Component getTableCellEditorComponent(JTable table,
                    Object value,
                    boolean isSelected,
                    int row,
                    int column)
            {
                // which instrument is selected?
                String instrument = (String) table.getValueAt(row, 0);
                String[] methods = (String[]) subItems.get(instrument);
                editorComboBox.setModel(new DefaultComboBoxModel(methods));
                editorComboBox.setSelectedItem(value);
                return editorComboBox;
            }

        public void itemStateChanged(ItemEvent e)
        {
            stopCellEditing();
        }
        }
}