0

This is the code i have for one button setting its corresponding Jpanel to visible and the other jpanels to false.

private void twickenhamButtonActionPerformed(java.awt.event.ActionEvent evt) {
    twickenhamPanel.setVisible(true);
    wembleyPanel.setVisible(false);
    ellandPanel.setVisible(false);
    sandyPanel.setVisible(false);
    mkPanel.setVisible(false);
    EtihadPanel.setVisible(false);
    villaParkPanel.setVisible(false);
    stJamesPanel.setVisible(false);
    millenniumPanel.setVisible(false);
    leicesterPanel.setVisible(false);
    kingsholmPanel.setVisible(false);
    OlympicPanel.setVisible(false);
}

But how to i add all of these jpanels to an array, but also use their corresponding buttons to set them to visible when clicked?

Juliano Alves
  • 2,006
  • 4
  • 35
  • 37
Karl Rez
  • 15
  • 3

1 Answers1

1

From the looks of your code, it looks like you only want one panel to be visible at a time. This seems like a perfect use of a CardLayout, that will allow you to swap view. You can map a name to each panel.

private static final String TWICKENHAM = "twickenham";

CardLayout layout = new CardLayout();
JPanel mainPanel = new JPanel(layout);
mainPanel.add(twickenhamPanel, TWICKENHAM);

Then when you want to show the twickenhamPanel, you can just call layout.show(mainPanel, TWICKENHAM). It might all sound foreign to you, but you can learn more at How to Use CardLayout.

Also by the method signature of your actionPerformed, it looks like you are using Netbeans GUI Builder. If that's the case, you may also want to look at How to use CardLayout with Netbeans GUI Builder

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720