?
Solved

JTable refresh and sort

Posted on 2009-02-18
7
Medium Priority
?
995 Views
Last Modified: 2013-11-23
Hi,

how do I refresh a JTable after adding an element? The table is connected to the database and I would like it to refresh after a new entry has been made (or deleted for that matter) but I don't know which method to call. revalidate() doesn't work. I'm using a custom TableModel which I'll attach to the question.

Also, is it possible to add listeners to table headers that when clicked would sort the data in the database according to the column selected? MySQL would do the sorting but I don't know if this feature is supported in JTable.

private TableModel setTableModelFromRS(ResultSet rs) {
        try {
            Vector columnNames = new Vector();
 
            // Get the column names
            columnNames.addElement("Manufacturer");
            columnNames.addElement("Country");
            // Get all rows.
            Vector rows = new Vector();
 
            while (rs.next()) {
                Vector newRow = new Vector();
 
                newRow.addElement(rs.getObject(2));
                newRow.addElement(rs.getObject(3));
 
                rows.addElement(newRow);
            }
 
            return new DefaultTableModel(rows, columnNames);
        } catch (Exception e) {
            e.printStackTrace();
 
            return null;
        }
 
}

Open in new window

0
Comment
Question by:slavenweb
  • 3
  • 2
  • 2
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 23672949
If you mean you want a 'live view' of the db, you need to requery. But you'd probably be better off using a CachedRowSet
0
 

Author Comment

by:slavenweb
ID: 23672982
ok, I'll create a method that will fill the table that can be called when needed. that should work

what is the benefit of using that set? what about sorting in the JTable?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23673080
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Expert Comment

by:CEHJ
ID: 23673096
0
 
LVL 92

Accepted Solution

by:
objects earned 1600 total points
ID: 23675235
when you make a change to the underlying table model you need to fire an event.
That event will notify the table of a change so that it can update itself accordingly.

0
 
LVL 92

Expert Comment

by:objects
ID: 23675237
0
 

Author Comment

by:slavenweb
ID: 23753596
Thanks, I used TableRowSorter class from one of the tutorials on the internet to sort the table and thanks objects, the change in table model is the key it's just that is requires somewhat more coding than for example jTable.revalidate() as I had in mind in the first place. :)

final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(praviModel);
        jTable1.setRowSorter(sorter);
        String text = filterText.getText();
        if (text.length() == 0) {
            sorter.setRowFilter(null);
        } else {
            try {
                sorter.setRowFilter(RowFilter.regexFilter(text));
            } catch (PatternSyntaxException pse) {
                JOptionPane.showMessageDialog(null, "Something is wrong with the regex pattern!", "Info", JOptionPane.ERROR_MESSAGE);
            }
        }

Open in new window

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 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…
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…
This video teaches viewers about errors in exception handling.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

621 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