在单元格编辑器中的组件内进行焦点遍历

时间:2010-02-09 20:58:58

标签: java swing focus jtable tablecelleditor

我正在尝试轻松编辑表格,使用自定义组件显示信息。 每个Cell都有3个数据文本。

我想要的是:

  • 如果单元格获得焦点,则开始编辑第一个值。
  • 在编辑第一个值用户时按[TAB],然后编辑第二个值(不要转到下一个单元格)
  • 如果我在第3个值中按[TAB],则编辑下一个单元格(输入1sr值)

我在论坛中寻找一个我没有找到这个案例,这个问题......还在阅读学习Swing

提前感谢您的回答,这是我的代码:

public class PruebaTabla extends JFrame {

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

            public void run() {
                new PruebaTabla().setVisible(true);
            }});
    }
    public PruebaTabla(){

        JTable tabla = new JTable(); 

        tabla.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        tabla.setCellSelectionEnabled(true);
        tabla.setDefaultRenderer(ModelClass.class, new ModelClassCellRederer());
        tabla.setDefaultEditor(ModelClass.class, new ModelClasstroCellEditor());
        tabla.setRowHeight(30);

        CustomModel model = new CustomModel();
        model.setModel(new ModelClass[][]{
                                        {new ModelClass(), new ModelClass(), new ModelClass()},
                                        {new ModelClass(), new ModelClass(), new ModelClass()},
                                        {new ModelClass(), new ModelClass(), new ModelClass()}});
        tabla.setModel(model);


        getContentPane().add(new JScrollPane(tabla));
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(480,150);
        setLocationRelativeTo(null);

    }

    private class ModelClass {

        private String value1;
        private String value2;
        private String value3;
        public ModelClass(){
            setValue1("" + Math.round(Math.random() * 100));
            setValue2("" + Math.round(Math.random() * 100));
            setValue3("" + Math.round(Math.random() * 100));
        }
        public String getValue1() {
            return value1;
        }
        public void setValue1(String value1) {
            this.value1 = value1;
        }
        public String getValue2() {
            return value2;
        }
        public void setValue2(String value2) {
            this.value2 = value2;
        }
        public String getValue3() {
            return value3;
        }
        public void setValue3(String value3) {
            this.value3 = value3;
        }

    }

    private class CustomModel extends AbstractTableModel{

        ModelClass[][] model;
        String[] columnNames = new String[] {"Column1", "Column2", "Column3"};

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            return ModelClass.class;
        }

        @Override
        public String getColumnName(int column) {
            return columnNames[column];
        }
        public int getColumnCount() {       
            return 3;
        }

        public int getRowCount() {
            return 3;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {           
            return model[rowIndex][columnIndex];
        }

        public ModelClass[][] getModel() {
            return model;
        }

        public void setModel(ModelClass[][] model) {
            this.model = model;
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {          
            return true;
        }
    }

    private class ModelClassCellRederer implements TableCellRenderer {

        JPanel panel = new JPanel();
        private JLabel label1= new JLabel();
        private JLabel label2 = new JLabel();
        private JLabel label3 = new JLabel();

        ModelClassCellRederer(){
            panel.add(label1);
            panel.add(label2);
            panel.add(label3);
        }

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

             if (isSelected) {
                 panel.setBackground(table.getSelectionBackground());
                 panel.setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
             } else {
                 panel.setBackground(table.getBackground());
                 panel.setBorder(null);
             }

            ModelClass v = (ModelClass) value;
            label1.setText(v.getValue1());
            label2.setText(v.getValue2());
            label3.setText(v.getValue3());

            return panel;
        }

    }

    private class ModelClasstroCellEditor extends DefaultCellEditor {

        JPanel panel = new JPanel();
        private JTextField label1= new JTextField();
        private JTextField label2 = new JTextField();
        private JTextField label3 = new JTextField();
        ModelClass actual;

        public ModelClasstroCellEditor() {
            super(new JCheckBox());
            panel.add(label1);
            panel.add(label2);
            panel.add(label3);
            setClickCountToStart(1);
        }

        @Override
        public Component getTableCellEditorComponent(JTable table,
                Object value, boolean isSelected, int row, int column) {
            actual = (ModelClass) value;
            label1.setText(actual.getValue1());
            label2.setText(actual.getValue2());
            label3.setText(actual.getValue3());

            return panel;
        }

        @Override
        public Object getCellEditorValue() {
            if (actual != null){
                actual.setValue1(label1.getText());
                actual.setValue2(label2.getText());
                actual.setValue3(label3.getText());
            }
            return actual;
        }

        @Override
        public boolean isCellEditable(EventObject anEvent) {
             if (anEvent instanceof KeyEvent) {
                 final KeyEvent keyEvent = (KeyEvent) anEvent;

                 SwingUtilities.invokeLater(new Runnable() {

                    public void run() {
                       if (!Character.isIdentifierIgnorable(keyEvent.getKeyChar())) {
                           label1.setText("" + keyEvent.getKeyChar());
                       }
                       label1.setCaretPosition(label1.getText().length());
                       label1.requestFocusInWindow();
                    }
                 });
              }
              return super.isCellEditable(anEvent);

        }

    }
}

1 个答案:

答案 0 :(得分:1)

目前,您的JTable使用来自UIManager.get("Table.ancestorInputMap")的InputMap处理键盘输入。这个选项卡当前有一个映射到String“selectNextColumnCell”的选项卡,它映射到JTable的ActionMap中的一个Action,它将你带到下一个单元格。

您可以执行以下操作:

  1. 创建一个Action,其actionPerformed方法执行您所追求的遍历策略。

  2. 在ActionMap中,将“someStringThatDescribesYourAction”作为键,将“1”中的“Action”作为值。

  3. 在InputMap中,将KeyStroke.getKeyStroke("TAB")作为键,将2中的String作为值。这将取代当前的Action,无论如何都会将你带到下一个单元格。