-2

I'm new to Java. I have a jFrame with 3 buttons. Button2 performs some action on a SwingWorker. I want to click Button3 and open a new dialog. When I am in the "Design" mode and click Button3 it sticks the Button3 actionevent code in the middle of Button2's code. Of course this doesn't work for Button3 and breaks Button2's code also.

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    SwingWorker <Void,Void> worker = new SwingWorker<Void,Void>() {
        @Override
        protected Void doInBackground() throws Exception {
            for(int i=1;i <=100;i +=1){
                //jLabel1.setText(String.valueOf(i));
                //this.repaint();
                jLabel1.setText(String.valueOf(i));
                Thread.sleep(500);
                System.out.print(i + " ");
                // TODO add your handling code here:
            }

            private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
                // TODO add your handling code here:
            }
            return null;
        }

        @Override
        protected void done() {
        }
    }
}

The only way to get rid of the code is by deleting the button. If I add the button back and try to add the code manually it doesn't work (I think because the "generated" code required isn't there).

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Chris J.
  • 1
  • 1
  • 1
    A guess: Some part of the file that helps generate code and keep track of generated code must have been corrupted. Perhaps re-start the program? For my money, though I avoid code generation of this sort, often for this very reason. Side suggestion: please format your posted code better. – Hovercraft Full Of Eels Aug 04 '23 at 13:39
  • I have just now formatted your code for readability, but in the future, this is something that you should be doing yourself – Hovercraft Full Of Eels Aug 04 '23 at 13:59
  • Side note: You should not be making direct Swing calls of *any* sort from within a background thread, such as within the SwingWorker's doInBackground method. If you need to change the state of a JLabel during the background thread, then use the worker's publish/process method pair, or use a property change listener. – Hovercraft Full Of Eels Aug 04 '23 at 14:02
  • Actually, a SwingWorker has a progress property that would work best for you. Call `setProgress(i)` within the worker, and add a PropertyChangeListener to the worker that monitors the progress's state, as per the [API](https://docs.oracle.com/en/java/javase/14/docs/api/java.desktop/javax/swing/SwingWorker.html). Then in the listener, you can update your label's text safely. Or if all you want to do is update an int with delay, use a [Swing Timer](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) . – Hovercraft Full Of Eels Aug 04 '23 at 14:09
  • Thank you. These replies were very helpful. I wound up deleting the swing worker code, adding the button and then pasting the worker again. All is well now. – Chris J. Aug 05 '23 at 17:37
  • @ChrisJ. It's good that you resolved your issue, but please consider posting an answer to your question, and accepting that answer. That approach is far more helpful to the community than having your resolution buried in a comment. – skomisa Aug 09 '23 at 19:24

0 Answers0