I'm trying to do the following:

private void butonActionPerformed(java.awt.event.ActionEvent evt) {                                      
            buton.setLocation(500, 500);
    }

but it doesn't do anything.

Please help.

Dani AI

Generated

A quick follow-up note: ’s later post shows the runtime move was being skipped by program logic — the setLocation call itself can work fine. Common reasons a programmatic move looks like “nothing happened” are different from a broken setLocation API: a layout manager can immediately reposition or size components, the coordinates can be outside the parent’s visible area, the component might be added to a different container than expected, or the GUI change can be made off the Event Dispatch Thread (EDT).

For reliably moving a Swing component with absolute positioning, remove the parent’s layout manager and set bounds on the EDT. Example pattern:

SwingUtilities.invokeLater(() -> {
    Dimension d = button.getPreferredSize();
    button.setBounds(200, 150, d.width, d.height);
});

Notes and troubleshooting checklist (based on the thread):

  • Confirm the code path actually runs (add a simple log/println where the move happens — this was the real cause in ’s case).
  • Inspect the parent: if it has a layout manager, prefer changing layout constraints rather than forcing coordinates; otherwise set the parent to null layout and use setBounds.
  • Verify coordinates are inside the parent’s client area and that the component’s width/height are sensible.
  • Make GUI updates on the EDT.
  • As hinted, forcing the UI to refresh can help reveal whether the change took effect.

Caution: absolute layouts (no layout manager) are fragile for resizable UIs and different look-and-feels. When possible, adapt the layout-manager approach (update constraints or re-add the component) so the interface scales correctly.

Recommended Answers

All 2 Replies

you may need to call validate() and repaint() methods on the component where your button resides.

It looks like that the problem was in my logical structure of my original application. My if statements where incorrect from a logical point of view. Sorry :$ :icon_redface: . The code works fine, after all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.