JTable单元格确实专注于鼠标单击

时间:2019-12-05 00:08:34

标签: java jtable focus cell selection

我有一个简单的桌子和一个面板上的按钮。当我单击表格单元格时,可以对其进行编辑,但是该单元格没有焦点。如果用户更改了单元格内容,我希望能够保存它,但是,只有在我先进入单元格以获取焦点,然后用鼠标单击将其选中时,才能执行此操作。我希望能够单击该单元格,并在退出该单元格时保存所有更改。
单击该按钮时,将显示已保存的表格单元格内容。

这是我的代码:

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import javax.swing.*;
    import static javax.swing.SwingConstants.LEFT;
    import javax.swing.SwingUtilities;
    import javax.swing.table.*;

    public class TestFocus {

        public ArrayList<String> mylog;

        public TestFocus() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel panel = createPanel();
            frame.add(panel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);    
        }

        public JPanel createPanel() {
            mylog = new ArrayList<>();
            JPanel panel = new JPanel();
            TestTableModel tm = new TestTableModel();
            JLabel title = new JLabel("Test Table");
            JTable table = new JTable(tm);
            TableColumnModel tcm = table.getColumnModel();
            TestTableCellEditor editor = new TestTableCellEditor();
            TestTableCellRenderer renderer = new TestTableCellRenderer();
            for (int i = 0; i < tm.getColumnCount(); i++) {
                TableColumn column = tcm.getColumn(i);
                column.setCellEditor(editor);
                column.setCellRenderer(renderer);
            }

            JScrollPane jsp = new JScrollPane(table);

            JButton btn = new JButton("Done");
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    for (String s : mylog) {
                        System.out.println(s);
                    }
                }
            });

            panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
            panel.add(jsp);
            panel.add(btn);
            return panel;
        }

        class TestTableModel extends AbstractTableModel {
            private String[] columnNames = { "Firstname", "Lastname", "Age" };
            private Object[][] data = { 
                { "John", "Smith", 29},
                { "Mary", "Thomas", 63},
                { "Peter", "Jones", 48} };

            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) {
                return data[row][col];
            }

            public Class getColumnClass(int col) {
                return getValueAt(0, col).getClass();
            }

            public String getColumnClassName(int col) {
                if (col == 2) {
                    return "Integer";
                } else {
                    return "String";
                }
            }

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

            public void setValueAt(Object value, int row, int col) {
                data[row][col] = value;
                fireTableCellUpdated(row, col);
            }
        }

        public class TestTableCellEditor extends AbstractCellEditor
                implements TableCellEditor {
            JComponent component = new JTextField();

            @Override
            public Component getTableCellEditorComponent(JTable table, Object value,
                    boolean isSelected, int row, int column) {

                if (value != null) {
                    ((JTextField)component).setText(value.toString());
                } else {
                    ((JTextField)component).setText("");
                }

                if (isSelected) {
                    mylog.add(value.toString() + ": row/col("+row+"/"+column+")");
                }

                return (JTextField)component;
            }

            @Override
            public Object getCellEditorValue() {
                int size = mylog.size();
                if (size > 0) {
                    Object val = mylog.get(size - 1);
                    if ((((JTextField)component).getText().equals(val))) {
                        mylog.remove(size - 1);
                    }
                }
                return ((JTextField)component).getText();
            }

        }

        public class TestTableCellRenderer extends JLabel implements 
                    TableCellRenderer {

            public TestTableCellRenderer() {
                this.setOpaque(true);
            }

            public Component getTableCellRendererComponent(JTable table, Object value,
                    boolean isSelected, boolean hasFocus, final int row, int column) {

                DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
                Component c = renderer.getTableCellRendererComponent(table, value,
                        isSelected, hasFocus, row, column);
                if (hasFocus) {
                    c.setBackground(Color.yellow);
                }
                TestTableModel tm = (TestTableModel)table.getModel();
                int col = table.convertColumnIndexToModel(column);
                String colname = tm.getColumnName(col);
                String type = tm.getColumnClassName(col);

                if (type.equals("Integer") || type.equals("Int")) {
                    ((JLabel)c).setHorizontalAlignment(CENTER);
                } else {    // add padding 
                    ((JLabel)c).setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
                    ((JLabel)c).setHorizontalAlignment(LEFT);
                }

                if (type.equals("String")) {
                    String text = ((JLabel)c).getText();
                        ((JLabel)c).setToolTipText(text);
                }
               return c;
            }
        }        

        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new TestFocus();
                }
            });
        }

    }

0 个答案:

没有答案