?
Solved

Changing data in a table

Posted on 2004-08-16
11
Medium Priority
?
189 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
  • 5
  • 2
8 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month16 days, 8 hours left to enroll

862 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