JTable not getting latest data (unless mouse is focussed out of the cell)
Hi,
I am using JDK 1.4.2. I am having a basic problem of reading the data present in a JTable.
JTable table =new JTable(9, 9);
JButton solveButton =new JButton("Solve");
solveButton.addActionListener(new DumpListener(table));
The dump listener just dumps the data in the table.
I have a 6*6 jtable where each value is a number. Now i focus the mouse on say square (1,2) and enter some data, then on square (5,5) and enter data say "4". There is a button in this panel, and which on being clicked, gets the table data and does some operation on it.
Problem is when i use the "getValueAt" API, it gives correct data for square(1,2) but not for (5,5). This is because the mouse focus is still on square (5,5). However if i focus out of square (5,5) using my mouse (to some other random square) then the data comes up properly. Am i missing something here?
I understand it is to do with the data model getting altered only when the mouse if focussed out? But this seems to be very trivial. Is there a way of getting the data out without focusing the mouse out of the cell?
I am not a complete newbie. I can see the method stopCellEdit in CellEditorListener. But could you elobarate on where do u want me to call it? For example, i have simplified my program here
class SimpleButtonListener implements ActionListener {
JTable table;
public SimpleButtonListener(JTable table) {
this.table = table;
}
public void actionPerformed(ActionEvent e) {
printArray(getData());
}
String[][] getData() {
TableModel model = table.getModel();
String[][] question = new String[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
question[i][j] = model.getValueAt(i,j) == null ? null : model.getValueAt(i,j).toString();
}
}
return question;
}
void printArray(String[][] question) {
int rows = question.length, columns = question[0].length;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.println("[" + i + ", " + j + "] -> "
+ question[i][j]);
}
}
}
}
public class DummyApplet extends JApplet {
public void init() {
setVisible(true);
JTable table = new JTable(9, 9);
JButton data = new JButton("DATA");
data.addActionListener(new SimpleButtonListener(table));
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(table, BorderLayout.CENTER);
panel.add(data, BorderLayout.SOUTH);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(panel);
}
}
I tried calling the code stopCellEditing in the listener but it does not work. Can you help here?
My solution uses a JFrame instead of a JApplet.
But I think the effect is the same.
First of all, the setVisible(true) should be put after all components are added.
Secondly, when you retrieve data in the table, you'd synchronized() the model.
Thirdly, I didn't find any problem when I call stopCellEditing(). Perhaps I'm using Java 5. Anyway, please try whether the following code works.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.TableModel;
public class DummyFrame extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
DummyFrame frame = new DummyFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
});
}
public DummyFrame() {
JTable table = new JTable(9, 9);
JButton data = new JButton("DATA");
data.addActionListener(new SimpleButtonListener(table));
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(table, BorderLayout.CENTER);
panel.add(data, BorderLayout.SOUTH);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(panel);
}
}
class SimpleButtonListener implements ActionListener {
JTable table;
public SimpleButtonListener(JTable table) {
this.table = table;
}
public void actionPerformed(ActionEvent e) {
synchronized (table) {
if (table.isEditing()) {
System.out.println("Is Editing!");
table.getCellEditor(table.getEditingRow(), table.getEditingColumn()).stopCellEditing();
}
}
printArray(getData());
}
String[][] getData() {
synchronized (table) {
TableModel model = table.getModel();
synchronized (model) {
String[][] question = new String[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
question[i][j] = model.getValueAt(i,j) == null ? null : model.getValueAt(i,j).toString();
}
}
return question;
}
}
}
void printArray(String[][] question) {
int rows = question.length, columns = question[0].length;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.println("[" + i + ", " + j + "] -> "
+ question[i][j]);
}
}
}
}
And ... please don't forget to give me the 5 Duke dollars if it works.
Thank you!
Asuka Kenji (UserID = 289)
(Duke Dollars Hunting now ...)