Fseee
Fseee

Reputation: 2627

setVisible(false) to a group of JTextField and JLabel

I have a group of JTextField and JLabel. I want them to initially not be visible so I thought to initialize my applet with a method which calls setVisible(false) for each of the components.

Is it possible to create a method setVisible(false) which will set the visibility of all the components to false. Finally if I have 40 components in the applet, is it possible to do this with only one command instead of 40 commands?

Upvotes: 1

Views: 2846

Answers (1)

dogbane
dogbane

Reputation: 274888

Add your buttons and labels to a JPanel and then you can simply make your JPanel invisible to hide them all with one call.

jPanel.setVisible(false);

Alternatively, add your buttons and labels to a JComponent list, and then loop through it:

List<JComponent> list = new ArrayList<JComponent>();
list.add(button);
list.add(label);
for(JComponent c : list){
    c.setVisible(false);
}

Upvotes: 3

Related Questions