Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

TableModelEvent  RSS feed

 
barnes arnold
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi folks

I'm at a loss as how to resolve this issue. I have a gui with a DefaultTableModel. When I update one of the cells I notice the only way this event is registered, is if I actually click on another cell. I would love the event:
TableModelEvent.UPDATE
be called if the focus is lost off the cell(after I had edited it). Can someone give me a pointer?? thanks alot. Here is an example app:


import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
//import java.text.*;

public class TestTable extends JFrame implements TableModelListener
{

JTable table;
JTextArea textArea_;

public TestTable()
{

String[] columnNames = {"Item", "Value 1", "Value 2"};
Object[][] data = {
{"Item 1", "1", "1.1"},
{"Item 2", "2", "2.2"}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
model.addTableModelListener( this );
//
table = new JTable( model );

table.setPreferredScrollableViewportSize(table.getPreferredSize());

JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane , BorderLayout.NORTH);

textArea_ = new JTextArea();
textArea_.setBackground(new Color(232,232,250));
getContentPane().add(textArea_, BorderLayout.CENTER);
}



public void tableChanged(TableModelEvent e)
{
System.out.println("TableModelEvent " + e.toString());
if (e.getType() == TableModelEvent.UPDATE)
{

int row = e.getFirstRow();
int column = e.getColumn();
String name = (String)table.getValueAt(row, 0);
String value1 = (String)table.getValueAt(row, 1);
String value2 = (String)table.getValueAt(row, 2);
textArea_.setText("the values of " + name + " is :" + value1 + ", " + value2);
}
}


public static void main(String[] args)
{
TestTable frame = new TestTable();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setBounds(100, 100, 650, 250);
frame.setVisible(true);
}
}
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
barnes arnold
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you craig - that helps alot
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!