Search code examples
javaswingjpaneljbuttongridbaglayout

How to make a component invisible, without changing the location of other elements


There are two buttons: "Next" & "Back" to pass on the list. When the reached the end, the button "Next" to disappear, but the button "back" should not jump to her place. I use the setVisible (false), but the button 'Back' jump to the place of the "Next". Location manager is GridBagLayout.


Solution

  • Place the button in a container JPanel with CardLayout together with one more empty JPanel (or JLabel). When you would like to hide the button just swap cards in the container.