简体   繁体   中英

Sorting jtable data from database by clicking on table header java

I have created a program that connects to a server database and retrieves information based on certain search criteria entered by the user, which is then placed into a JTable.

The user would like to be able to click on the column headers of the JTable and sort the data accordingly.

This is the code which I have but unfortunately it does nothing when clicking on the header:

pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
if (rs.next()) {
    tblR.setModel(DbUtils.resultSetToTableModel(rs));
    tblR.setAutoCreateRowSorter(true);
} else {
    JOptionPane.showMessageDialog(null, "No matches were found according to your search criteria. Please make sure all entered data is correct.");
}

Does the line tblR.setAutoCreateRowSorter(true) not work in this case? How can i get it to work?

I found my own error.

I had not placed my JTable in a JScrollpane and therefore the method was not working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM