Java:单击另一个复选框时自动更新JTable中的复选框

时间:2017-05-13 14:30:25

标签: java swing jtable

我希望在单击EventYearMatchingWindow中的JTable中的复选框时,自动选择EventYearMatchingUnitPanel类中JTable中的每个复选框。

点击"单元9"第一张图片(https://ibb.co/fsqtrQ)中的复选框应自动选中第二张图片中的所有5个复选框(https://ibb.co/f8ja5k

我使用TabelModelListener中的tableChanged()方法来检测是否单击了一个复选框。当我单击该复选框时,它会更改其他复选框的值(从Boolean.FALSE到Boolean.TRUE),但复选框不会更新(不要在物理上显示复选标记)。

//for the first JTable
public void tableChanged(TableModelEvent e){
    int row = e.getFirstRow();
    int column = e.getColumn();
    TableModel model = (TableModel)e.getSource();
    String columnName = model.getColumnName(column);
    Object thatValue = model.getValueAt(row, column);
    data[row][column] = thatValue;
    for(int i = 0; i < unitPanels[row].getNumRows(); i++){
        unitPanels[row].setToSelectedState(i);
    }
} 

//for the second JTable
public void setToSelectedState(int row){    
    data[row][0] = Boolean.TRUE;
    model.fireTableCellUpdated(row, 0);
}

以下是代码的更大图片:

public class EventYearMatchingWindow extends JFrame implements 
TableModelListener
{
    private boolean columnHeaderClicked;
    private ArrayList<Category> categories;
    private JPanel mainPanel, listPanel, buttonPanel;
    private ArrayList<Item> selectedItems;
    private EventYearMatchingItemChoosing[] unitPanels;
    Object data[][];
    public EventYearMatchingWindow(ArrayList<Category> c){
        ...
        String[] columnNames = {"Include?", ""};   
        data = new Object[6][2];
        for(int i = 0; i < 6; i++){
            data[i][0] = Boolean.FALSE;
            data[i][1] = "Unit " + (i+8);
        }
        DefaultTableModel model = new DefaultTableModel(data, columnNames) {
            private static final long serialVersionUID = 1L;
            @Override
            public Class getColumnClass(int columnIndex) {
                if (columnIndex == 0){
                    return Boolean.class;
                }
                return super.getColumnClass(columnIndex);
            }
        };

        JTable table = new JTable(model);
        table.getModel().addTableModelListener(this);
        table.addMouseListener(new java.awt.event.MouseAdapter() {
        ....
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                int row = table.rowAtPoint(evt.getPoint());
                int col = table.columnAtPoint(evt.getPoint());
                if (row == -1 && col >= 0){
                    columnHeaderClicked = true;
                }
                if (row >= 0 && col == 1 && !columnHeaderClicked) {
                    mainPanel.removeAll();

                    System.out.println(unitPanels[row].getData()[0][0]);
                    unitPanels[row].revalidate();
                    unitPanels[row].repaint();

                    mainPanel.add(unitPanels[row]);
                    JPanel nButtonPanel = new JPanel();
                    Button back = new Button("Back");
                    nButtonPanel.add(back);
                    back.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent e){
                            mainPanel.removeAll();
                            mainPanel.add(listPanel);
                            mainPanel.add(buttonPanel);
                            mainPanel.revalidate();
                            repaint();
                        }
                    });
                    mainPanel.add(nButtonPanel);
                    mainPanel.revalidate();
                    repaint();
                }
            }
        });   
    }
    public void tableChanged(TableModelEvent e){
        int row = e.getFirstRow();
        int column = e.getColumn();
        TableModel model = (TableModel)e.getSource();
        String columnName = model.getColumnName(column);
        Object thatValue = model.getValueAt(row, column);
        data[row][column] = thatValue;
        for(int i = 0; i < unitPanels[row].getNumRows(); i++){
            unitPanels[row].setToSelectedState(i);
        }
    } 
}

public class EventYearMatchingItemChoosing extends JPanel implements TableModelListener
{
    Category category;
    private boolean columnHeaderClicked;
    private int size;
    private ArrayList<Item> items;
    private Object[][] data;
    DefaultTableModel model;
    public EventYearMatchingItemChoosing(String s, ArrayList<Category> c){
        ...
        String[] columnNames = {"Include?", "Event", "Year"};   
        data = new Object[size][3]
        for(int i = 0; i < category.getItems().size(); i++){ 
            if(!category.getItems().get(i).getEvent().equals("") && !category.getItems().get(i).getYear().equals("")){
                data[i][0] = Boolean.FALSE;
                data[i][1] = category.getItems().get(i).getEvent();            
                data[i][2] = category.getItems().get(i).getYear();   
            }
        }    

        model = new DefaultTableModel(data, columnNames) {   
            private static final long serialVersionUID = 1L;   
            @Override
            public Class getColumnClass(int columnIndex) {
                if (columnIndex == 0){
                    return Boolean.class;
                }
                return super.getColumnClass(columnIndex);
            }    
        };
        JTable table = new JTable(model);
        table.getModel().addTableModelListener(this);
        ...
    }
    ...
    public void tableChanged(TableModelEvent e){
        int row = e.getFirstRow();
        int column = e.getColumn();
        TableModel model = (TableModel)e.getSource();
        String columnName = model.getColumnName(column);
        Object thatValue = model.getValueAt(row, column);
        data[row][column] = thatValue;
    }
    public void setToSelectedState(int row){    
        data[row][0] = Boolean.TRUE;
        model.fireTableCellUpdated(row, 0);
    }
}

1 个答案:

答案 0 :(得分:0)

  

但复选框不会更新(不会在物理上显示复选标记)。

data[row][0] = Boolean.TRUE;

不要访问数据阵列。数据Array用于创建DefaultTableModel,然后将数据从Array复制到DefaultTableModel使用的数据结构(恰好是Vector的Vector)。

最终结果是TableModel中的数据永远不会改变。

代码应为:

model.setValueAt(Boolean.TRUE, row, 0);