Search code examples
javaswingjtablejtextfieldtablecelleditor

Auto add some text in JTable


Can textField in JTable add some text automatically ? (without javaScript)

Just like auto add colon(:) for "Time" when user type number 2 digits or lost focus from that field.

I try using some invokeLater(), KeyListener() and editCellAt(), it work only when I get into the field (double click and F2), but doesn't work if I only click and type.

private JTable getTblMaster() {
if (tblMasterData == null) {
    tblMasterData = new JTable() {

           public boolean editCellAt(int row, int column, EventObject e){
                boolean result = super.editCellAt(row, column, e);
                final Component editor = getEditorComponent();
                if (editor == null || !(editor instanceof JTextComponent)) {
                    return result;
                }
          /*      if(column == 2){
                     EventQueue.invokeLater(new Runnable() {
                        @Override
                        public void run() {

                            if(((JTextComponent) editor).getText().length() == 2){
                                ((JTextComponent) editor).setText(((JTextComponent) editor).getText() + ":");
                            }                               
                        }   
                     });                     
                }
           */
                return result;
            }
            public TableCellEditor getCellEditor(int row, int column) {

                TableColumn tableColumn = getColumnModel()
                        .getColumn(column);

                TableCellEditor editor = tableColumn.getCellEditor();
                try {
                    if (editor == null) {
                            final JTextField text = new JTextField();
                       /* 
                           SwingUtilities.invokeLater(new Runnable() {

                                    @Override
                                    public void run() {
                                        if(text.getText().length() == 2){
                                            text.setText(text.getText() + ":");
                                        }                                       
                                    }                               
                                });


                            text.addKeyListener(new java.awt.event.KeyAdapter() {
                                public void keyPressed(KeyEvent e){
                                    if(text.getText().length() == 2){
                                        text.setText(text.getText() + ":");
                                    }
                                }
                                public void keyReleased(KeyEvent e){
                                    if(text.getText().length() == 2){
                                        text.setText(text.getText() + ":");
                                    }
                                }
                            });
                        */
                            editor = new DefaultCellEditor(text);
                        ;
                        return editor;
                    }
                } catch (Exception e) {
                    LogWriter.error(e);
                }
                return editor;
            }
        };
    }
    return tblMasterData;
}

Solution

  • On the textfield of your TableCellEditor, add a DocumentListener to the Document of the JTextField. Within the DocumentListener, add your method that should append/modify the text of the textfield.

    If I remember correctly, you will need to perform the modification within a SwingUtilities.invokeLater because I think that JTextField prevents text-modification during the firing of events (well actually, it is the Document which will prevent that).