Solved

Changing data in a table

Posted on 2004-08-16
11
184 Views
Last Modified: 2010-03-31
I have a JTable using a DefaultTableModel
I want users to be able to edit data in the table. This is not my problem.

My problem is that when the user edits this data, I want to be able to check the data before it gets stored to the table, and if it's invalid (for whatever reason), it should not be stored.

I was wondering how to do this?
0
Comment
Question by:carlos_jjang
[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
  • 5
  • 2
11 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 11813913
Just validate it using methods of the TableModel. Normally there's no automatic storage anyway
0
 
LVL 14

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 250 total points
ID: 11814596
public class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor {
       
        // This method is called just before the cell value
        // is saved. If the value is not valid, false should be returned.
        public boolean stopCellEditing() {
            String s = (String)getCellEditorValue();
   
            if (!isValid(s)) {
                // Should display an error message at this point
                return false;
            }
            return super.stopCellEditing();
        }
    }
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11814611
JTable table = new JTable();
   
    // Add some data...
   
    // Install the custom editor on the first column
    int vColIndex = 0;
    TableColumn col = table.getColumnModel().getColumn(vColIndex);
    col.setCellEditor(new MyTableCellEditor());
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 86

Expert Comment

by:CEHJ
ID: 11814644
>>I want to be able to check the data before it gets stored to the table

I thought by that you might mean the database table, as opposed to the JTable. Was i wrong?
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11814651
>>isValid(s)

this is ur validator against the cell
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11814693
>> I thought by that you might mean the database table, as opposed to the JTable. Was i wrong?

Some times it gives confusion between swing table and database table when we use word table

;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 11815844
> I thought by that you might mean the database table

When was a database even mentioned?
(Not that it actually makes any difference)

0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12090400
thanks :)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

691 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