KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
In my application I have use a java table which is similar to this example. My problem is when I change a value of a cell (even in above example) the data model doesn't get updated until I click on a different cell. Even I click on the gray area below the table after changing the cell value the model won't change. I think the reason is cell stay on focused until I click on a different cell. How can I avoid this and update the model without clicking on the table cell. Thanks in advance I have edit the above sample code to reflect the problem public class JTableDemo extends JApplet { private JTextArea txt = new JTextArea(4, 20); // The TableModel controls all the data: class DataModel extends AbstractTableModel { Object[][] data = { { "one", "two", "three", "four" }, { "five", "six", "seven", "eight" }, { "nine", "ten", "eleven", "twelve" }, }; // Prints data when table changes: class TML implements TableModelListener { public void tableChanged(TableModelEvent e) { txt.setText(""); // Clear it for (int i = 0; i < data.length; i++) { for (int j = 0; j < data[0].length; j++) txt.append(data[i][j] + " "); txt.append("\n"); } } } public DataModel() { addTableModelListener(new TML()); fireTableDataChanged(); } public int getColumnCount() { return data[0].length; } public int getRowCount() { return data.length; } public Object getValueAt(int row, int col) { return data[row][col]; } public void setValueAt(Object val, int row, int col) { data[row][col] = val; System.out.println(val); // Indicate the change has happened: fireTableDataChanged(); } public boolean isCellEditable(int row, int col) { return true; } } public void init() {
Tags (comma-separated)
Save Edits
Cancel