This is my first time working with Java Swing, so apologies for missing a lot context. This is using Netbeans 7.0.x--business rational. Code is generated using the "Design" feature in Netbeans. Most of the gui code is generated, if not all.
I've tried a few areas of calling setEnabled to disable an upload button until the file transfer completes. For testing purposes, I added a doubly for-loop to do some floating point arithmetic; I have popup messages before the for loops, and after to mirror some "file upload busy work". None of the places I have called the setEnabled function on the button of interest, give the desired result of "greying out" or "disabling" the button--until the guiController.submitUpload() call completes inside the body of swingworker.doInBackground(). For now I commented out the done() functiion.
The desired result: on clicking the buttonX, grey out the button or disable it--that is, I shouldn't be able to click on it again--see the first popup in submitUpload(), then see the second pop up. The submitUpload() function runs it's business logic, and completes. After completing, the buttonX should be re-enabled or disable it. Ideally, I'd like to disable AND grey out this button, buttonX.
I've tried many of the results in SO, and already read up on documentation:
https://docs.oracle.com/javase/tutorial/uiswing/components/button.html
https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html
Can someone help me out with something I'm missing?
=========================
panelFile.java
//private variable
SwingWorker sw = ...;
buttonX.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt) {
//tried disabling here
//buttonX.setEnabled(false);
buttonXActionPerformed(evt);
//renable
}
}
.
.
.
private void bottonXActionPerformed(java.awt.event.ActionEvent evt) {
//also tried disabling here
sw = new SwingWorker<void, void> () {
@Override
public void doInBackground
{
try
{
if (guiController != null)
{
guiController.submitUpload();
}
}
catch (Exception e)
{
logger.log(Level.SEVERE, e.toString(), e);
}
sw = null;
return null;
}
/*
@Override
public void done()
{
buttonX.setEnabled(true);
}
*/
};
sw.execute();
}
=========================
guiController.java
public void submitUpload(){
//do stuff
//for testing I have pop-ups
// I should only see this popup once, until the "upload completes"
JOptionPane.showMessageDialog(getFrame(),
"BEFORE",
"test disable",
JOptionPane.INFORMATION_MESSAGE);
//busy loop here
JOptionPane.showMessageDialog(getFrame(),
"AFTER",
"test disable",
JOptionPane.INFORMATION_MESSAGE);
}
So yes, call
JButton#setEnabled(false)where you are calling it, before you execute the SwingWorker, and then re-enable it when the worker is done. This will work. If it doesn't work for you, then you have Swing threading problems in code not shown.Here is my example of a proof-of-concept MRE:
For more complex interactions between the background thread and the GUI, consider use of a SwingPropertyChangeSupport object and property change listeners. This will allow complex notifications to be sent from the the background thread to the GUI, and the notifications will be sent on the Swing event thread, allowing for thread safety.