0

I am using Eclipse WindowBuilder to build a GUI for my Java program. I am currently stuck as I have created a button and I have give the X and Y locations different variables. These variables change in a 'While' loop when the button is clicked and sends out an event.

I have tried looking at multi-threading. However I don't think this is the most viable option. Also if I did multi-thread I don't know which bit of the code I would have to put in the separate thread.

New button = Button button(X, Y, 100,100);

I am trying to increase the x and Y coords

  • The button won't monitor the values of `x` and `y`. It just gets created with the current values. You send an event when the while loop changes the values? Doesn't the button have a method `setPosition()` (or `setX()` and `setY()`) you could use to update the position? – bkis Jul 09 '19 at 08:20
  • Is it a JButton? Would be helpful if we could see your button instantiation. – SuperHanz98 Jul 09 '19 at 08:28
  • Not at my computer at the moment, but I will update with code. – JustInitforsomeviews Jul 09 '19 at 08:32
  • 1
    Is this Swing or SWT? These are completely different GUI systems. – greg-449 Jul 09 '19 at 08:35
  • 1
    Please provide a [mcve], so we can easier understand what you want and can also improve your code. – Sergiy Medvynskyy Jul 09 '19 at 09:39
  • *"I have created a button and I have give the X and Y locations different variables. These variables change in a 'While' loop when the button is clicked"* .. ***Why?*** Or more completely, what is the point of moving the component? Is this for a game? It's the only thing I can think of, that would not be confusing & irritating to the end user. See also [What is the XY problem?](http://meta.stackexchange.com/q/66377) – Andrew Thompson Jul 09 '19 at 12:23

1 Answers1

0

Awt and Swing are not thread safe so, if you are trying to update a UI in the same thread you will have the "application freeze" behavior and the button will not change it's position if you click on it several times. You could disable the button meanwhile the loop is being executed and before starting the loop check that the button is not disabled. For example:

walkerButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        walkerButtonActionPerformed(evt);
    }
});

private void walkerButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             

    // if walker button is disabled exit the method
    if (!walkerButton.isEnabled()) {
        return;
    }       

    // Disable the button before starting the loop
    walkerButton.setEnabled(false);

    int steps = 20;
    int stepDistance = 2;        

    while (steps > 0) {  
        // Set the walker button new location          
        int x = walkerButton.getX() + stepDistance;
        int y = walkerButton.getY() + stepDistance;
        walkerButton.setLocation(x, y);
        steps--;
    }  

    // Enable the button after the loop execution
    walkerButton.setEnabled(true);
} 

enter image description here

Read also: Java awt threads issue Multithreading in Swing

eHayik
  • 2,981
  • 1
  • 21
  • 33