Solved

Jtables Detecting chagnes to a single cell value

Posted on 2004-04-08
6
203 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 68
Requested array size exceeds VM limit 3 84
Java - Why doesn't this JFrame work 3 41
GUI builder for Eclipse? 8 27
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now