4

So I'm trying to make a basic program to learn more about java, and I'm having trouble switching screens. I wanted to have a display class that I could call in other classes to handle all the panels and such, and then make a class to build each panel. What I'm trying to do at the moment is use a button in my startmenu class to change from one panel to another using a method in the display class.

Here's the code in the startmenu class:

public void actionPerformed(ActionEvent e)
{
 display.switchPanel("Start");
}

And here is my display class:

public class Display 
{
 JFrame frame;
 StartMenu start = new StartMenu();
 MainMenu main = new MainMenu();
 JPanel panel = new JPanel();
 JPanel startPanel = start.createPanel();
 JPanel mainPanel = main.createPanel();
 CardLayout card = new CardLayout();
 BorderLayout border = new BorderLayout();
 public void createDisplay()
 {
 frame = new JFrame("Insert Name");
 frame.setPreferredSize(new Dimension(800,600));
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.getContentPane().add(panel, BorderLayout.CENTER);
 panel.setLayout(border);
 panel.add(startPanel);
 panel.add(mainPanel);
 mainPanel.setVisible(false);
 startPanel.setVisible(true);
 frame.add(panel);
 frame.pack();
 frame.setVisible(true);
 frame.setResizable(false);
 }
 public void switchPanel(String x)
 {
 String p = x;
 if(p.equals("Start"))
 {
 mainPanel.setVisible(true);
 startPanel.setVisible(false);
 }
 }
}
Zizouz212
4,9985 gold badges46 silver badges66 bronze badges
asked Nov 12, 2015 at 1:20
4
  • Check out CardLayout: docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html Commented Nov 12, 2015 at 1:22
  • With you're current approach, you're going to have no end of issues battling with the BorderLayout, as it will only manage a single component at any one of it's 5 available positions. Instead, you should be making using of a CardLayout, it's what it's designed for. See How to Use CardLayout for more details Commented Nov 12, 2015 at 1:23
  • I was using cardlayout before, I have it set before the createDisplay method. When I tried to use .show though, it kept giving me an error in the method I call in the startmenu class Commented Nov 12, 2015 at 1:28
  • @MadProgrammer This is my previous code using cardlayout. ' public void switchPanel(String x) { String p = x; if(p.equals("Start")) { card.show(panel, "2"); } }' 'panel.setLayout(card); panel.add(startPanel, "1"); panel.add(mainPanel, "2");' Commented Nov 12, 2015 at 1:33

1 Answer 1

4

Use a CardLayout, it's what it's designed for, for example...

public class Display {
 public static final String START_VIEW = "start";
 public static final String MAIN_VIEW = "main";
 JFrame frame;
 StartMenu start = new StartMenu();
 MainMenu main = new MainMenu();
 JPanel panel = new JPanel();
 JPanel startPanel = start.createPanel();
 JPanel mainPanel = main.createPanel();
 CardLayout card = new CardLayout();
 public void createDisplay() {
 frame = new JFrame("Insert Name");
 frame.setPreferredSize(new Dimension(800, 600));
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.getContentPane().add(panel, BorderLayout.CENTER);
 panel.setLayout(card);
 panel.add(startPanel, START_VIEW);
 panel.add(mainPanel, MAIN_VIEW);
 mainPanel.setVisible(false);
 startPanel.setVisible(true);
 frame.add(panel);
 frame.pack();
 frame.setVisible(true);
 frame.setResizable(false);
 }
 public void switchPanel(String x) {
 card.show(panel, x);
 }
}

Then you might use something like...

public void actionPerformed(ActionEvent e)
{
 display.switchPanel(Display.START_VIEW);
}

to switch between the views

See How to Use CardLayout for more details

answered Nov 12, 2015 at 1:38
Sign up to request clarification or add additional context in comments.

4 Comments

Cool, this helped me understand how to implement cardlayout better! Thanks! I tried that and still got the same error though...maybe I added action listener to my button wrong? I did this startBtn.addActionListener(this); The error is : Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
Then I assume that display is null
I did private Display display; at the start of the startmenu class, is that the incorrect way to access that class in another class?
In this yes, you need a reference to the instance of Display which is actually showing all the stuff on the screen

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.