How to provide pagination support to a JTable in Swing?

后端 未结 5 512
遇见更好的自我
遇见更好的自我 2020-11-27 06:51

I have created one GUI in Swing Java in which I have used JTable.Now I want to display next page information into it by using pagination. How should I do that ?

5条回答
  •  有刺的猬
    2020-11-27 07:29

    Alternatively, you can make use of the QuickTable project.

    Screenshot

    Here is the DBTable component in action:

    DBTable component, embedded in JFrame, with data loaded from a CSV file.

    The DBTable component is embedded in a traditionnal JFrame.

    Sample code

    The following sample code produces the window shown in the previous screenshot:

    import javax.swing.JFrame;
    import javax.swing.UIManager;
    
    import quick.dbtable.DBTable;
    
    public class QuickTableFrame extends JFrame {
    
        private static final long serialVersionUID = -631092023960707898L;
    
        public QuickTableFrame() {
            try {
                // Use system look and feel
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    
                // set Frame properties
                setSize(300, 200);
                setVisible(true);
                setLocationRelativeTo(null);
                setDefaultCloseOperation(EXIT_ON_CLOSE);
    
                // create a new quicktable
                DBTable dBTable1 = new DBTable();
    
                // add to frame
                getContentPane().add(dBTable1);
    
                // set the database driver to be used, we are using jdbc-odbc driver
                dBTable1.setDatabaseDriver("org.h2.Driver");
    
                /*
                 * set the jdbc url,"quicktabledemo" is the data source we have
                 * created for the database
                 */
                dBTable1.setJdbcUrl("jdbc:h2:mem:test;INIT=create table employee as select * from CSVREAD('test.csv');");
    
                // set the select statement which should be used by the table
                dBTable1.setSelectSql("select * from employee");
    
                // to create the navigation bars for the table
                dBTable1.createControlPanel();
    
                // connect to database & create a connection
                dBTable1.connectDatabase();
    
                // fetch the data from database to fill the table
                dBTable1.refresh();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            // create a new table frame
            QuickTableFrame myframe = new QuickTableFrame();
        }
    }
    

    Resources and dependencies

    test.csv

    empid,emp_name,emp_dept,emp_salary
    1,Azalia,ornare,114918
    2,Jade,tristique,152878
    3,Willa,In scelerisque scelerisque,166733
    ...
    

    H2

    
        com.h2database
        h2
        1.4.187
    
    

    References

    • QuickTable basic tutorial
    • QuickTable official tutorials
    • Download latest jar
    • h2 database

提交回复
热议问题