Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Changing data in a table

Posted on 2004-08-16
11
Medium Priority
?
187 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 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
Independent Software Vendors: 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!

 
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

Technology Partners: 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

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…
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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

618 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