that is i've got an array of buttons and i want to replace two buttons (their location on the screen) by clicking on first and then clicking on the second to replace thier location i.e. when i click on button1 first (which is at the first location in a row of buttons on the screen) and then button5, then these two button should replace locations i.e. after clicking on button5 at the fifth location in a row of buttons button5 goes to the location of button1 and button1 goes to the location of button5.

Is there any way that i can do this without replacing their captions with each other as currently i can swap them by swapping thier captions.

Dani AI

Generated

A few practical approaches that work reliably regardless of captions, layouts or look-and-feel. and were right to point attention at how the buttons are laid out — that is the deciding factor for how you should swap them.

If the buttons live in a layout-managed JPanel (GridLayout, FlowLayout, BoxLayout, etc.), change the component order in the container and refresh the panel. Typical steps: remember the first-clicked button, get both components' indices from the parent, remove the components (remove the larger index first to avoid shifting), add them back in swapped positions, then call revalidate() and repaint(). Do this on the EDT (ActionListeners already run there). Example pattern:

/* field: JButton first = null; */

ActionListener swapListener = e -> {
  JButton clicked = (JButton)e.getSource();
  JPanel panel = (JPanel)clicked.getParent();

  if (first == null) { first = clicked; /* mark selection */ return; }
  if (first == clicked) { first = null; /* unmark */ return; }

  Component[] comps = panel.getComponents();
  int i1 = Arrays.asList(comps).indexOf(first);
  int i2 = Arrays.asList(comps).indexOf(clicked);

  if (i1 > i2) { panel.remove(i1); panel.remove(i2); }
  else        { panel.remove(i2); panel.remove(i1); }

  panel.add(clicked, i1);
  panel.add(first,  i2);

  panel.revalidate();
  panel.repaint();

  first = null;
};

For web pages (tagged javascript), swap DOM nodes instead of changing labels. Store the first clicked element, on second click reinsert the nodes to swap their order. A small robust swap helper:

function swap(a, b) {
  var p = a.parentNode, aNext = a.nextSibling;
  p.insertBefore(a, b);
  if (aNext === b) p.insertBefore(b, a);
  else             p.insertBefore(b, aNext);
}

Quick gotchas: handle clicks on the same button, disabled buttons, nested containers, and components whose layout is governed by constraints (BorderLayout/GridBagLayout require swapping constraints or rebuilding the layout). Avoid null layouts unless you must control absolute coordinates. These techniques swap positions visually without touching captions.

Recommended Answers

All 3 Replies

If you are using breezy swing (not sure if it works on standard swing):

just use the setLocation() method with the appropraite button object.

ex

myButton.setLocation(1,1);

explain more ... what layout are you using ??

thanks for your reply but i have already sorted it out therefore i have unsbscribed from this thread.
many thanks

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.