I have inherited code and for reasons to long to explain I am required to use a null layout. I have been attempting to take what they have an navigate between JPanels. I haven't been able to figure out how. This is what I have now which compiles broken down into a SSCCE below. What I am attempting to do is add the JPanels to an ArrayList which contains a reference to previous JPanels. That way I can call a "home" JPanel from the current JPanel the user is in. As of now it goes to the previous JPanel but the contents are empty. Any help would be great, THANKS!
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Mainscreen extends JFrame {
public JPanel Home;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Mainscreen frame = new Mainscreen();
frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Mainscreen() {
ArrayList <JPanel> jpLayout = new ArrayList();
final Dataentrylog DEL = new Dataentrylog(this, jpLayout);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setBounds(100, 100, 618, 373);
Home=new JPanel();
Home.setBackground(new Color(255, 250, 250));
Home.setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
Home.setVisible(true);
setContentPane(Home);
Home.setLayout(null);
JButton delLog = new JButton("Next JPanel");
delLog.setFont(new Font("Tahoma", Font.PLAIN, 14));
delLog.setForeground(new Color(0, 0, 0));
delLog.setBackground(UIManager.getColor("Menu.selectionBackground"));
Home.add(delLog);
jpLayout.add(Home);
delLog.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Home.setVisible(false);
setContentPane(DEL);
getContentPane().setLayout(null);
}
});
delLog.setBounds(44, 214, 213, 61);
}
}
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Dataentrylog extends JPanel {
public Dataentrylog(final JFrame parent, final ArrayList <JPanel> jpLayout) {
setBounds(100, 100, 618, 373);
setBackground(new Color(255, 250, 250));
setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
setLayout(null);
final JButton btnSignIn = new JButton("Go Back");
btnSignIn.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnSignIn.setBackground(UIManager.getColor("EditorPane.selectionBackground"));
btnSignIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
parent.setContentPane(jpLayout.get(0));
setLayout(null);
}
});
btnSignIn.setBounds(226, 282, 153, 52);
add(btnSignIn);
}
}
If you are asked by some mentor to perform navigation among panels using
null layout
, here is what you need to do:Check out the tutorial: How to use CardLayout
Working Example:
here is a written
CardLayoutDemo
which navigates among10 panels
including Home panel using button click action: