I'm trying to make a basic Swing project and trying to figure out the best way to change Cards from within the panels when using Card Layout.
The only way I've managed to get it working is by parsing the Main Window into each Panel on it. Is this considered a proper way to do it? Or is there a better way?
Here's an example of what I mean:
Main Window Class:
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.CardLayout;
import javax.swing.JPanel;
import java.awt.Color;
public class Window {
private JFrame frame;
private CardLayout cardLayout;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Window window = new Window();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Window() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new CardLayout());
Orange orange = new Orange(this);
orange.setBackground(Color.ORANGE);
frame.getContentPane().add(orange, "orange");
Green green = new Green(this);
green.setBackground(Color.GREEN);
frame.getContentPane().add(green, "green");
//Shows orange by default
cardLayout = (CardLayout) frame.getContentPane().getLayout();
cardLayout.show(frame.getContentPane(), "orange");
}
//Changes the currently shown card to the "Green" Panel
public void goGreen() {
cardLayout.show(frame.getContentPane(), "green");
}
//Changes the currently shown card to the "Orange" Panel
public void goOrange() {
cardLayout.show(frame.getContentPane(), "orange");
}
}
Orange Panel:
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
public class Orange extends JPanel {
private Window parent;
public Orange(Window parent) {
this.parent = parent;
setBackground(Color.ORANGE);
//Adds a button to change to the "Orange" Panel
JButton btnGoGreen = new JButton("Go Green Panel");
btnGoGreen.setBounds(188, 132, 89, 23);
btnGoGreen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Function of the parent Window.class to change to the Orange Panel
parent.goGreen();
}
});
add(btnGoGreen);
}
}
Green Panel:
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
public class Green extends JPanel {
private Window parent;
public Green(Window parent) {
this.parent = parent;
setBackground(Color.GREEN);
//Adds a button to change to the "Green" Panel
JButton btnGoOrange = new JButton("Go Orange Panel");
btnGoOrange.setBounds(188, 132, 89, 23);
btnGoOrange.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Function of the parent Window.class to change to the Orange Panel
parent.goOrange();
}
});
add(btnGoOrange);
}
}