1

I have a JTable. Currently I have the following code:

     table.addMouseListener(new MouseAdapter() {
     public void mouseClicked(MouseEvent e) {
     if (e.getClickCount() == 1) {
     JTable target = (JTable)e.getSource();
     int row = target.getSelectedRow();

     int   value = Integer.parseInt( (String) table.getValueAt(row, 0));
     int x = 0,y=0;
     if (row==1) {x=582;y=483;} else if (row==2) {x=221;y=575;} else if (row==3) {x=231;y=435;}
     boundaryFill4(x, y, value, 50);

     my.setIcon(new ImageIcon(buffered));

            }
          }
        });

But I need to make my JTable to update itself automatically as user types in new values. The mouse listener does not do exactly what I want. It would update as the user points the cursor to the JTable cell. I could not find any "input value update" listener in documentation.

I could use keyListener, but in that case I will need to add an "UPDATE" JButton, but I need the JTable to update itself automatically without JButton.

The third method would be to create an infinite update loop : while(true) { //update} But this takes a lot of computer memory...and is not efficient way.

Can anybody recommend me how to improve my code OR can anybody correct the above MouseListener. Thank You!

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Buras
  • 3,069
  • 28
  • 79
  • 126
  • Do you want your listener to be called every time the user types a key, or when the user finishes entering a value in a cell? – VGR Apr 12 '13 at 20:56
  • when the user finishes entering a value in a cell – Buras Apr 12 '13 at 21:31
  • that's not how editing a table cell is supposed to work: it's meant to be type-all-then-commit scenario. Taking a step back, the question is: why do you need a commit-on-typed? Re-reading your last comment leaves me confused: updating when the user commits the value is default behaviour, so what exactly do you want to achieve? – kleopatra Apr 12 '13 at 21:48

1 Answers1

2

As shown in How to Use Tables, you can make every cell editable by choosing how you construct your TableModel. For control of individual cells, override isCellEditable() in your TableModel. If you still have problems, please edit your question to include an sscce that shows your chosen approach.

Addendum: I just was curious if there is a "Input" listener.

You may be looking for a TableCellEditor, seen here and here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thank you, but I have no problems with the JTable per se. I have an additional ExcelAdapter class. I just was curious if there is a "Input" Listener. I know there is a "input key" listener, but that does not provide an immidiate update on the runtime. Currently my code is updating using an infinite loop: while(true)...which is probably not the best option. – Buras Apr 12 '13 at 20:48