Solved

Jtables Detecting chagnes to a single cell value

Posted on 2004-04-08
6
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

738 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