Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Jtables Detecting chagnes to a single cell value

Posted on 2004-04-08
6
Medium Priority
?
242 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 1200 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
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.
Suggested Courses

916 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