ActionEvents和ItemEvents:处理动态JComboBox上的事件

时间:2013-06-05 03:33:07

标签: java swing jtable jcombobox tablecelleditor

关于我的代码我遇到了一些问题,我很感激任何有关此问题的见解。

首先,这里有一些上下文(在下面的代码中): 主要对象可以在左栏的组合框编辑器中选择,每个对象都有自己的数据集,其中包含相同类型的对象,可用于以后的比较和操作。右列的编辑器(再次组合框)更新以显示此数据集。但是,列表中的第一个元素应该作为向所述列表添加新元素的选项,并且当它被选中(actionEvent)时,将创建新的数据对象,但是只要组合框重新生成选中后,它会创建并显示另一个数据对象。

这段代码代表了我的应用程序中产生问题的部分的基本结构,我已经尽可能多地将它(尽可能多地)修剪为SSCCE格式中的基本结构。有些物品仍在使用他们的法语名称,如果您不了解其目的,我可以更改它们。前一段解释了很多,但如果你想要精确,我很乐意继续。

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.JScrollPane;


public class TestApp extends JFrame {

    private JPanel contentPane;
    private CustomTable table;
    private JComboBox<GeneralPrimaryObject> selecteurObjets = new JComboBox<GeneralPrimaryObject>();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestApp frame = new TestApp();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public TestApp() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(42, 46, 350, 103);
        contentPane.add(scrollPane);

        selecteurObjets.addItem(new FirstSubClass());

        table = new CustomTable();
        table.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, ""},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                },
                new String[] {
                    "Primary object", "Sublist from object"
                }
            ));
        scrollPane.setViewportView(table);
        table.getColumnModel().getColumn(1).setPreferredWidth(105);
        table.setRowHeight(15);
        TableColumn colonneObjets = table.getColumnModel().getColumn(0);
        colonneObjets.setCellEditor(new DefaultCellEditor(selecteurObjets));
        table.getModel().addTableModelListener(new TableModelListener(){
            private static final int COLONNE_OBJETS = 0;
            private GeneralPrimaryObject previousObject;
            @Override
            public void tableChanged(TableModelEvent e) {
                int row = e.getFirstRow();
                int col = e.getColumn();
                JComboBox<GeneralPrimaryObject> editor = new JComboBox<GeneralPrimaryObject>();
                GeneralPrimaryObject confirmedObject;
                Object value;
                ArrayList<GeneralPrimaryObject> objects;
                //new object standing in for the "New data set..." item
                FirstSubClass newObjectStandIn = new FirstSubClass();

                newObjectStandIn.setName("New data set...");
                previousObject = newObjectStandIn;
                TableModel modele = (TableModel)e.getSource();
                value = modele.getValueAt(row, col);
                if(col == COLONNE_OBJETS && value != null){
                    confirmedObject = (GeneralPrimaryObject)value;
                    objects = confirmedObject.getData();
                    for(GeneralPrimaryObject o : objects){
                        System.out.println("Fuck");
                        editor.addItem(o);
                    }
                    editor.addItem(newObjectStandIn);
                    editor.addItemListener(new ItemListener(){

                        @Override
                        public void itemStateChanged(ItemEvent e) {
                            if(e.getStateChange() == ItemEvent.SELECTED){

                            }
                        }

                    });

                    editor.addActionListener(new ActionListener(){

                        @Override
                        public void actionPerformed(ActionEvent e) {

                            JComboBox<GeneralPrimaryObject> source = (JComboBox<GeneralPrimaryObject>) e.getSource();

                            GeneralPrimaryObject object = null, objetSelectionne = (GeneralPrimaryObject)source.getSelectedItem();
                            String nom = "Data set " + (source.getItemCount());
                            previousObject.equals(objetSelectionne);




                            if(source.getSelectedIndex() == 0){
                                    switch(objetSelectionne.getType()){
                                    case FIRSTSUB:
                                        object = new FirstSubClass();
                                        object.setName(nom);
                                        break;
                                    case SECONDSUB:
                                        object = new SecondSubClass();
                                        object.setName(nom);
                                        break;
                                    default:
                                        break;

                                    }



                            }
                            if(object != null){
                                System.out.println("Flag1");
                                source.addItem(object);
                                source.setSelectedItem(object);

                            }
                        }

                    });
                    table.addEditor(editor, row);
                }

            }

        });
    }
    private class CustomTable extends JTable{
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private ArrayList<TableCellEditor> editors = new ArrayList<TableCellEditor>(8);

        public CustomTable(){
            super();
            editors.ensureCapacity(8);
            for (int i = 0; i < 8; i++) {
                editors.add(null);
            }
        }

        /*@Override
        public TableCellRenderer getCellRenderer(int row, int column){
            if(row == 1 && editeurs.size() > row && editeurs.get(row) != null) {
                return editeurs.get(column).;
            }
            return super.getCellRenderer(row, column);
        }
        */

        @Override
        public TableCellEditor getCellEditor(int row, int column){

                int modelColumn = convertColumnIndexToModel( column );

                if (modelColumn == 1 && row < 8){
                    return editors.get(row);
                }else{
                    return super.getCellEditor(row, column);
                }
        }

        public void addEditor(JComboBox<GeneralPrimaryObject> newEditor, int row){
            editors.set(row, new DefaultCellEditor(newEditor));

            this.repaint();
        }
    }
    private abstract class GeneralPrimaryObject{
        protected String name;
        protected ObjectType type;
        private ArrayList<GeneralPrimaryObject> data = new ArrayList<GeneralPrimaryObject>();

        private GeneralPrimaryObject(){

        }

        public abstract String getName();
        public abstract void setName(String name);
        public abstract ObjectType getType();

        public ArrayList<GeneralPrimaryObject> getData(){
            return data;
        }

        public String toString(){
            return this.name;
        }
    }
    private class FirstSubClass extends GeneralPrimaryObject{
        public FirstSubClass(){
            super();
            setName("SubclassObject");
            this.type = ObjectType.FIRSTSUB;
        }
        @Override
        public String getName() {
            return this.name;
        }
        @Override
         public void setName(String name) {

            this.name = name;
        }
        @Override
        public ObjectType getType() {

            return this.type;
        }

    }
    private class SecondSubClass extends GeneralPrimaryObject{
        public SecondSubClass(){
            super();
            setName("OtherSubclassObject");
            this.type = ObjectType.SECONDSUB;
        }
        @Override
        public String getName() {
            return this.name;
        }
        @Override
         public void setName(String name) {

            this.name = name;
        }
        @Override
        public ObjectType getType() {

            return this.type;
        }

    }
    private enum ObjectType{
        FIRSTSUB, SECONDSUB;
    }
}

我一直在玩ItemListeners和ActionListeners,似乎无法弄清问题是什么。这可能很简单,但我已经解决了这个问题很长一段时间了,我认为这意味着我需要对此有一双新的看法,否则我会寻找相当长的时间。

哦,这个问题:Why is itemStateChanged on JComboBox is called twice when changed?似乎没有解决问题(也许我错了)。

无论如何,感谢任何回复!

1 个答案:

答案 0 :(得分:2)

  

列表中的第一个元素应该作为向所述列表添加新元素的选项,并且当它被选中时(actionEvent),

我不确定我是否理解这个问题,但我建议你不要在组合框上使用ActionListener(或ItemListener。

相反,我会尝试向TableModelListener添加TableModel。选择“新数据集...”时,将更新模型。因此,在TableModelListener中,您可以创建下一个数据集并将数据集添加到编辑器中。