Solved

Jtables Detecting chagnes to a single cell value

Posted on 2004-04-08
6
219 Views
Last Modified: 2010-03-31
I am using a Jtable, some of the columns are tick boxes.

How can I detetect that a value in any cell has changed? If the value of a cell is changed I want some code to be exectuted.

How do I add the needed listner? How can I tell which cell has been changed?

Thanks
0
Comment
Question by:wilmow
  • 3
6 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 10783868
TableModelListener listening for TableModelEvent's
cf. http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/event/TableModelListener.html
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10783916
>> How can I tell which cell has been changed?

About TableModelEvent:

 int getColumn() : Returns the column for the event.
 int getFirstRow() : Returns the first row that changed.
 int getLastRow() : Returns the last row that changed.
 int getType() : Returns the type of event - one of: INSERT, UPDATE and DELETE.

>> How do I add the needed listner?

MyTable.getModel().addTableModelListener( new MyTableModelListener() );

with

class MyTableModelListener implements TableModelListener {

    void tableChanged(TableModelEvent e) {
         int columnChanged = e.getColumn();
         // whatever you want to do
    }
}
0
 

Author Comment

by:wilmow
ID: 10811935
Can you please help me with the implementation of the above code. Im using netbeans and it doesnt seemt to like the line
class MyTableModelListener implements TableModelListener {
or
MyTable.getModel().addTableModelListener( new MyTableModelListener() );

thnaks
0
 
LVL 37

Accepted Solution

by:
zzynx earned 300 total points
ID: 10812217
>> doesnt seemt to like the line
>>  class MyTableModelListener implements TableModelListener {

import javax.swing.event.*;        // <<<<<<<<<< The interface TableModelListener is defined in this package

class MyTableModelListener implements TableModelListener {
}


>> MyTable.getModel().addTableModelListener( new MyTableModelListener() );

1) replace "MyTable" by the name of your JTable variable
2) if you use your class MyTableModelListener you have to be sure it is "known"

     + Create a new directory "myapp"  (New > folder )
     + create your MyTableModelListener class in that folder

MyTableModelListener.java:

package myapp;               // <<<<<<<<<<<<<

import javax.swing.event.*;

class MyTableModelListener implements TableModelListener {
    ...
}

     + make sure the file where you use MyTableModelListener is also in that folder

YourWhateverClass.java:

package myapp;  // <<<<<<<<<<<<<<<<<<<<<<<<<<<

import ...


By making both class files a member of the same package (myapp) you are assured that they will "know" each other.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
windows explorer path to command prompt 5 54
null output 3 36
How  can  i  resolve  HTTP Status 404 -? 8 46
Java basic valueOf question 1 29
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question