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.
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):
null layout and use setBounds. 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.
Jump to Post— gangsta1903 2you may need to call validate() and repaint() methods on the component where your button resides.
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.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.