I have 2 classes; Students and RegisterStudents, and hence 2 different main_panel(Class 1) and panel_1 (Class 2). What I am trying to do is, when a button on the Students Interface is pressed, the whole panel_1 should appear within main_panel. I have set both to same size already. is that possible?
The code i got so far is:
JButton btnNewButton = new JButton("Register Student");
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
Students main_panel = new Students();
RegisterStudent panel_1 = new RegisterStudent();
main_panel.add(panel_1);
}
});
btnNewButton.setBounds(0, 162, 167, 37);
panel.add(btnNewButton);
This isnt doing anything though? its compiling, but panel_1 is not actually appearing inside the main_panel. Has anyone got any suggestions?
You were initializing the new
main_panel
, and newpanel_1
, and addingpanel_1
tomain_panel
but then you weren't doing anything with the newmain_panel
.Also, I highly suggest naming your variables otherwise - these names are very non-intuitive.
For such things I would suggest you to use CardLayout When you add something to the container, you must call revalidate() and repaint() methods to realize the changes made to it at RunTime. Like in your case you adding
main_panel.add(panel_1);
now after this you must performso that changes can be seen. A small code snippet to help you understand what I mean.