Skip to content
Advertisement

JTextField doesn’t appear after switching panel using CardLayout

i have 4 panels associated to a content panel which uses a card layout. If i try to switch between the cards by using the buttons, i can see the JLabels but no JTextFields. I assume they are there because there is space between the JLabels, so i tried to revalidate and repaint the panels after switching to them but that wasn’t working. Do i have to repaint them somewhere else? Or am i missing something?

JavaScript

Picture of start panel

Picture of answer panel after switching to it

Advertisement

Answer

i can see the JLabels but no JTextFields

JavaScript

A Swing component does NOT have a size until the frame has been packed or made visible.

The default height is 0, so you are giving your components a preferred height of 0. The FlowLayout will respect the preferred size, so there is nothing to paint.

All Swing components are responsible for determining their own preferred size, so don’t attempt to manage the preferred size on your own with some random value.

Instead use:

JavaScript

The number specified in the constructor will allow the text field to calculated its preferred size to display 5 “W” characters based on the Font and Borders used.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement