4

Can I make my textField in JTable acts like a cell in Excel? Clear the text when typing in but can editing when get into the cell.

I think these 2 operations will goes to the same event. Am I wrong?

I try to use the keyPressed but nothing work. TT-TT

Here is my code

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

            private static final long serialVersionUID = 1L;

            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();
                            /*
                            text.addKeyListener(new java.awt.event.KeyAdapter() {
                                public void keyPressed(KeyEvent e){

                                }
                            });

                            SwingUtilities.invokeLater(new Runnable(){
                               public void run(){

                               }
                            });
                            */  
                            editor = new DefaultCellEditor(text);
                        ;
                        return editor;
                    }
                } catch (Exception e) {
                    LogWriter.error(e);
                }
                return editor;
            }
        };
    }
    return tblMasterData;
}

Any suggestion?

Marko
  • 20,385
  • 13
  • 48
  • 64
swingNoobie
  • 151
  • 2
  • 14

3 Answers3

4

better could be select all text in the JTable cell

text.setText(text.getText())
text.selectAll

by wrapping into invokeLater()

great workaround Table Select All Editor by @camickr

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Thank you for your reply and sorry for less describe, but I don't want to wrap the text when get into that cell. All I want is to replace data when I type in it(not get into the cell yet) but not change the behavior when double click or press F2 (still has previous data without wrap that text). – swingNoobie Sep 07 '12 at 09:33
  • 1
    In this context, _wrap_ means to invoke as a `Runnable`, not break text. See this related [example](http://stackoverflow.com/a/10067560/230513). – trashgod Sep 07 '12 at 11:04
4

Ohh! trashgod. Your example saved my life :D

All I needed was the code below and it worked. How easy! Thank you very very much.

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 (e instanceof KeyEvent) {
                    ((JTextComponent) editor).selectAll();
                }
                return result;
            }    ....
Community
  • 1
  • 1
swingNoobie
  • 151
  • 2
  • 14
0

I did it like this. First I am using the event keyReleased and then getting the row and column number on which I am working and then setting the value at that row. Code goes like this.

private void purchases_TBLKeyReleased(java.awt.event.KeyEvent evt) {                                          
    int rowWorking = purchases_TBL.getSelectedRow();
    int columnWorking = purchases_TBL.getSelectedColumn();

    if(columnWorking==3){
        model.setValueAt(null, rowWorking, columnWorking);
    }
} 

This makes the third column of the table null as soon as I focus move on to that using keyboard.

Note: The same piece of code can be placed in MouseClicked event.

Pawan
  • 423
  • 6
  • 28