简体   繁体   中英

JFrame froze after clicking JButton

I have a JFrame called Product Management Frame and it has an Insert and a Show Button. So if I clicked Insert Button, it insert all the data(Name, Username, Password, User-ID) into a database.

The problem is if I click the "Insert" button for the second time after clicking the "Show" button which shows the data from the database to JTable, my JFrame froze and vise versa.

Here is my code which is inside the method called showData() and called from the "Show" button's action listener.

ResultSet r = s.executeQuery("SELECT * FROM EmployeeTable");
            r.last();
            r.beforeFirst();
            while (r.next()) {
                String name1 = r.getString("EmployeeName");
                String userID = r.getString("UserID");
                String username = r.getString("Username");
                String password = r.getString("Password");
                String[] data = {name1, userID, username, password};
                         DefaultTableModel m1 = (DefaultTableModel) table.getModel();
                m1.addRow(data);
                table.setModel(m1);

Are there any solutions or can you guys suggest to me what should I do?

It is really difficult to understand what's going on without the code that creates the frame and the button. Just make sure you are running the gui under SwingUtilities.invokeLater like this:

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        createAndShowGUI();
    }
});

You can read everything you need at:

https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html

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