简体   繁体   中英

Hiding element in Java Swing inside a GridBagLayout

What you have to know :

  • I have a GridBagLayout inside a panel.
  • Inside my GridBagLayout I have 4 elements :
    • JLabel title at gridy = 0
    • textfield username at gridy = 1
    • textfield address IP at gridy = 2
    • error message in setVisible(false) at gridy = 3

If I don't touch anything, this is working perfectly well.

But if I setVisible(true) the error message, the whole view moves up. Making me think that the setVisible(false) is not really hiding the item but "removing it from the panel".

How can I hide my item without "removing it from the panel" ?

Don't use setVisible to show and hide elements. Pretty much only use that method for Frames. You rather want to use a TabbedPane , where you don't show the tabs and don't have any borders, and simply switch the tab in your code to a pane which has nothing on it from a pane which has some components.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM