取消选择jTable中的ComboBox

时间:2015-04-30 01:21:07

标签: java swing combobox jtable

嗨,嗨。我有两个表:一个用于编辑,一个用于查看。单击编辑按钮时,将使用查看表的当前值显示要编辑的表。我的编辑表包含一个包含ComboBox的列,以及我的问题在哪里开始...例如,我已经在系统中搜索了某些内容,然后它会显示在我的查看表中,然后让我们查看。 s说有9个项目(行)我然后点击或编辑第9行的组合框然后保存它。然后它将返回到查看表但如果我再次搜索并最终得到8个结果,如果我再次编辑它将抛出

  

线程中的异常" AWT-EventQueue-0"   java.lang.ArrayIndexOutOfBoundsException:8> = 8

我怀疑它是因为它无法找到当前选定的组合框(位于第9行)...所以我该如何取消选择呢?

和uhm(旁边的问题)有没有办法同步这两个表? (因为我正在做的只是将值从一个表复制到另一个表)

编辑: 这是问题的一个示例,但是如果运行起来太麻烦我只是提供一个关于如何创建问题的屏幕截图...

http://oi58.tinypic.com/2qdap3s.jpg(对不起,我是新的,所以我不能直接在这里发布图片)

public class TableClass extends JFrame {


    public TableClass() {
        initComponents();
    }

    @SuppressWarnings("unchecked")                         
    private void initComponents() {

        jScrollPane2 = new JScrollPane();
        edittable = new JTable();
        remove = new JButton();
        edit = new JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        edittable.setModel(DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Table 1", "Table 2", "Table 3", "Table 4"
            }
        ));
        jScrollPane2.setViewportView(edittable);

        remove.setText("Remove last row");
        remove.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                removeActionPerformed(evt);
            }
        });

        edit.setText("Edit");
        edit.addActionListener(ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                editActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, GroupLayout.PREFERRED_SIZE, 375, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(remove)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(edit)
                .addGap(109, 109, 109))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, GroupLayout.PREFERRED_SIZE, 95, GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(edit)
                    .addComponent(remove))
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }                

    private void editActionPerformed(ActionEvent evt) {                                         
        TableColumn column = edittable.getColumnModel ().getColumn ( 3 );

        //I'm using a custom renderer from WebLAF

        WebTableCellRenderer renderer = new WebTableCellRenderer ();
        column.setCellRenderer ( renderer );
        JComboBox comboBox = new JComboBox ();
        comboBox.addItem ( "1" );
        comboBox.addItem ( "2" );
        comboBox.addItem ( "3" );
        comboBox.addItem ( "4" );
        column.setCellEditor ( new WebDefaultCellEditor ( comboBox ) );

    }                                        

    private void removeActionPerformed(ActionEvent evt) {                                         
        DefaultTableModel model = (DefaultTableModel)edittable.getModel();

        int rows = model.getRowCount();
        model.removeRow(rows-1);



    }                                        


    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TableClass().setVisible(true);
            }
        });
    }


    private JTable edittable;
    private JButton remove;
    private JButton edit;
    private JScrollPane jScrollPane2;

}

0 个答案:

没有答案
相关问题