I am fairly new to Java so I am probably missing something fundamental here but here goes.
I have a GUI with a button and I want to click on it, change the text in a window to something then perform a task
connectButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
databaseConnectWindow.setText("Connecting...");
connectToDatabase();
}
});
But with the code above the text in databaseConnectWindow does not change until after conectToDatabse has finished. Any ideas?
Use a SwingWorker for a background thread so you don't lock the GUI thread otherwise known as the event dispatch thread or EDT. For more on this, please check out Lesson: Concurrency in Swing
You're calling connectToDatabase()
in the UI thread.
The UI isn't able to update until the UI thread is free.
You should connect to the database on a background thread.
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.