Solved

Changing data in a table

Posted on 2004-08-16
11
179 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
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 84
JavaScript/Java - Changing an image background color 4 66
servlet filter example 37 59
Tomcat: Unable to run tomcat service. 2 18
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

776 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