I am writing a program that will have a large amount of panels used. I want to put each panel in their own file so that it will be more easily organized.
I am using cardlayout
My problem is that in the other class files, I can't change the visibility of another panel because the frame is in the main class and the panel will be in another class.
I wrote some sample code that encapsulates the problem. The 1st panel is in the main class, and it can switch to the second, outside panel, but the outside panel cannot switch to the main class' panel.
Here is my main class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Arrow{
public JPanel panelHouse;
public JFrame frame;
public int total = 3000;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Arrow window = new Arrow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Arrow() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(400, 400, 909, 572);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new CardLayout(0, 0));
JPanel panelHouse = new JPanel();
panelHouse.setBounds(0, 0, 10, 10);
frame.getContentPane().add(panelHouse, "Housing");
panelHouse.setLayout(null);
Flash Flash1 = new Flash();
frame.getContentPane().add(Flash1, "Flash");
Flash1.setLayout(null);
JLabel lblHouseRent = new JLabel("RENT");
lblHouseRent.setFont(new Font("Times New Roman", Font.PLAIN, 20));
lblHouseRent.setBounds(99, 53, 73, 35);
panelHouse.add(lblHouseRent);
JLabel lblHouseOwn = new JLabel("OWN");
lblHouseOwn.setFont(new Font("Times New Roman", Font.PLAIN, 20));
lblHouseOwn.setBounds(476, 66, 60, 22);
panelHouse.add(lblHouseOwn);
JButton btnHouseNext = new JButton("NEXT STATION");
btnHouseNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Flash1.setVisible(true);
panelHouse.setVisible(false);
}
});//Close housingNext button actionlistener
btnHouseNext.setFont(new Font("Times New Roman", Font.PLAIN, 20));
btnHouseNext.setBounds(344, 371, 184, 48);
panelHouse.add(btnHouseNext);
JLabel lblHousing = new JLabel("HOUSING");
lblHousing.setFont(new Font("Times New Roman", Font.PLAIN, 22));
lblHousing.setBounds(388, 11, 102, 27);
panelHouse.add(lblHousing);
}//close initialize
public JPanel getPanelHouse() {
return panelHouse;
}
}//close class body
Here is the second, outside class.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Flash extends JPanel{
private static final long serialVersionUID = 1L;
public Flash() {
Arrow a = null;
JPanel Flash1 = new JPanel();
setBounds(0, 0, 10, 10);
setLayout(null);
setBackground(Color.orange);
JButton buttonFlash = new JButton("NEXT STATION");
buttonFlash.addActionListener(new ActionListener() {
@SuppressWarnings("null")
public void actionPerformed(ActionEvent arg0) {
Flash1.setVisible(false);
a.getPanelHouse().setVisible(true);
}
});
buttonFlash.setFont(new Font("Times New Roman", Font.PLAIN, 20));
buttonFlash.setBounds(344, 371, 184, 48);
add(buttonFlash);
setVisible(true);
}
}
I need to initialize Arrow to something while in the Flash class, but it sends me a null void warning because technically there isn't anything there.
Any ideas or help would be awesome!