Changing data in a table

Posted on 2004-08-16
Medium Priority
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?
Question by:carlos_jjang
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
LVL 86

Accepted Solution

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

Assisted Solution

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();
LVL 14

Expert Comment

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());
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

LVL 86

Expert Comment

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?
LVL 14

Expert Comment

ID: 11814651

this is ur validator against the cell
LVL 14

Expert Comment

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

LVL 92

Expert Comment

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)

LVL 14

Expert Comment

ID: 12090400
thanks :)

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month12 days, 9 hours left to enroll

777 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