Solved

Jtables Detecting chagnes to a single cell value

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array6 challenfge 6 65
groupSum6 challenge 6 48
base64 decode encode 12 100
Strange loading of website behaviour 3 23
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

747 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

12 Experts available now in Live!

Get 1:1 Help Now