Search code examples
javaswingjpaneljbuttonlayout-manager

How to add buttons to JPanel starting from the right?


How to add buttons and other components to a JPanel starting from the right to the left? I have used BorderLayout manager to do that but it did not work, they are inserted in the middle of the screen!

How can I do that?


Solution

  • Add additional JPanel and FlowLayout with right alignment.

    JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT), 0, 5); // 0 for horizontal gap