如何解散一个单元格并将另一个单元格聚焦在jtable中?

时间:2013-11-12 12:43:41

标签: java swing jtable focus

这个问题长期困扰着我。 这是非常讨厌的,我设法准备样品,将它展示给你的专家。 只需复制粘贴:

public class CellBorderDemo extends JFrame
{
    private JTable dataSearchResultTable;
    private boolean selectRow = false;

    public CellBorderDemo()
    {
        JPanel panel = new JPanel(new GridLayout(2, 1, 5, 10));
        panel.setPreferredSize(new Dimension(500, 300));
        MyTableModel myTableModel = new MyTableModel();
        dataSearchResultTable = new JTable();
        dataSearchResultTable.addKeyListener(new KeyAdapter()
        {
            private String attemptId = new String();

            @Override
            public void keyTyped(KeyEvent e)
            {
                System.out.println("selectRow=" + selectRow + " keyChar=" + (int) e.getKeyChar() + " event=" + e);
                if ((e.getModifiers() == KeyEvent.CTRL_MASK && (int) e.getKeyChar() == 10)) {
                    System.out.println("Ctrl+J was pressed when focus was ok");
                    selectRow = true;
                    attemptId = new String();
                    dataSearchResultTable.editCellAt(-1, -1);
                    dataSearchResultTable.getSelectionModel().clearSelection();
                    return;
                } else if ((int) e.getKeyChar() == 32) {
                    return;
                }
                if (selectRow) {
                    char keyChar = e.getKeyChar();
                    if (keyChar >= 48 && keyChar <= 57) {
                        attemptId += String.valueOf(keyChar);
                        if (selectRow(Integer.valueOf(attemptId).intValue())) {
                            System.out.println("attemptId=" + attemptId);
                            return;
                        }
                    }
                    selectRow = false;
                    attemptId = new String();
                }
            }
        });

        dataSearchResultTable.setSelectionBackground(new Color(0xbbccdd));
        dataSearchResultTable.setSelectionForeground(new Color(0x333300));
        dataSearchResultTable.setFillsViewportHeight(true);
        dataSearchResultTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        dataSearchResultTable.setAutoCreateRowSorter(true);
        dataSearchResultTable.setRowHeight(30);
        dataSearchResultTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        dataSearchResultTable.getColumnModel().setColumnMargin(0);
        dataSearchResultTable.setPreferredScrollableViewportSize(dataSearchResultTable.getPreferredSize());

        dataSearchResultTable.setModel(myTableModel);

        panel.add(new JScrollPane(dataSearchResultTable));
        super.getContentPane().add(panel);
        super.pack();
        super.setDefaultCloseOperation(EXIT_ON_CLOSE);
        super.setVisible(true);
    }

    public boolean selectRow(int attemptId)
    {
        MyTableModel tableModel = (MyTableModel) dataSearchResultTable.getModel();
        int row = 0;
        if (attemptId >= 0 && attemptId < tableModel.getRowCount()) {
            System.out.println("rowToSelect=" + row);
            dataSearchResultTable.setRowSelectionInterval(attemptId, attemptId);
            dataSearchResultTable.setColumnSelectionInterval(2, 2);
            tableModel.fireTableCellUpdated(row, 2);
            dataSearchResultTable.requestFocusInWindow();
            return true;
        }
        return false;
    }

    class MyTableModel extends AbstractTableModel
    {
        private String[] columnNames = { "First Name", "Last name", "Vegetarian" };
        private Object[][] data;
        int prefRow = 1, prefCol = 2;

        MyTableModel()
        {
            data = new Object[][] { { "Vova", "KipokKipokKipokKipok", false }, { "Olia", "Duo", true },
                    { "Ivan", "Brown", false } };
        }

        public int getColumnCount()
        {
            return columnNames.length;
        }

        public int getRowCount()
        {
            return data.length;
        }

        public String getColumnName(int col)
        {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col)
        {
            if (data.length > 0 && data[0] != null) {
                return data[row][col];
            }
            return null;
        }

        public Class getColumnClass(int c)
        {
            Object valueAt = getValueAt(0, c);
            return valueAt == null ? Object.class : valueAt.getClass();
        }

        public boolean isCellEditable(int row, int col)
        {
            return true;
        }

        public void setValueAt(Object value, int row, int col)
        {
            if (data.length > 0 && data[0] != null) {
                if (data[row][col] instanceof Boolean && (Boolean) value) {
                    setValueAt(false, prefRow, prefCol);
                    prefRow = row;
                    prefCol = col;
                }
                data[row][col] = value;
                fireTableCellUpdated(row, col);
            }
        }
    }

    public static void main(String[] args) throws ParseException
    {
        new CellBorderDemo();
    }
}

我明白这很长。不要只看keylistener和selectRow方法,而是查看类MyTableModel。 现在请按Ctrl + J然后按0或1或2。 您会注意到选择了适当的行,您可以按空格键选择复选框。现在再次按Ctrl + J并按0到2之间的数字,但它应该与第一次尝试不同。您注意到根据新编号选择了新行。现在按空格键,您将看到我的问题选择了新单元格但没有聚焦,这就是为什么Space未选中之前选中的复选框但当前未选中的复选框。

我将非常感谢这一帮助,因为这个功能非常重要。

谢谢!

1 个答案:

答案 0 :(得分:1)

selectRow方法的下一个问题中,您永远不会将row变量始终更改为0.在下一个示例中,我更改了您的行变量并尝试使用editCellAt方法<编辑该行/ p>

public boolean selectRow(int attemptId) {
    MyTableModel tableModel = (MyTableModel) dataSearchResultTable.getModel();
    int row = 0;
    if (attemptId >= 0 && attemptId < tableModel.getRowCount()) {
        System.out.println("rowToSelect=" + row);
        dataSearchResultTable.setRowSelectionInterval(attemptId, attemptId);
        dataSearchResultTable.setColumnSelectionInterval(2, 2);
        tableModel.fireTableCellUpdated(row, 2);
        dataSearchResultTable.requestFocusInWindow();
        row = dataSearchResultTable.getSelectedRow();
        if(row != -1){
            dataSearchResultTable.editCellAt(row, 2);
        }
        return true;
    }
    return false;
}

我认为你想要什么。