Hi everyone,
I read a lot about loading jpanel in a jpanel in this forum and I tried almost everything from repaint, to revalidate. :-( I start giving up. Any help will be apprecieated, pleasseeee.. Here is the application.
I have the
HomeView which extends JPanel and contains the menu, left and right panel. The left panel includes an icon panel and button panel. The button panel should load 3 different panels according to user's login and the right panel (screen panel) should load a few more panels according to user's choises. My
MainGui that extends JFrame calls the HomeView and loads the first panel (LoginView). Until there is ok. I login successfull and nothing is displayd :-( All the panels are in different classes and I call them by e.g HomeView hv = new HomeView(); Here is the MainGui.
import javax.swing.*;
import java.awt.*;
public class MainGui extends JFrame {
public MainGui(){
HomeView1 hv = new HomeView1(); // includes the left and right panel
LoginView lv = new LoginView(); // login screen
setBounds(0, 0, 620, 430);
setTitle("Test");
getContentPane().add(hv);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
// prepare to load the login panel
hv.remove(hv.newPanel);
hv.add(hv.getScreenPanel(lv));
} // end constructor
public static void main(String[] args) {
JFrame maingui = new MainGui();
dtsmain.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dtsmain.setVisible(true);
} // end main
} // maingui
The
getScreenPanel] is a method in the HomeView class that initializes the screen panel:
public JPanel getScreenPanel(JPanel screenPanel) {
if(screenPanel == null) {
try {
screenPanel = new JPanel();
screenPanel.setBounds(162, 28, 432, 365);
//screenPanel.setOpaque(false);
newPanel = screenPanel; // this was one of the tries to know what panel is up
}
catch (Throwable e) {
e.printStackTrace();
}
}
screenPanel.setBounds(162, 28, 432, 365);
newPanel = screenPanel;
return screenPanel;
}
The part of
LoginView that calls the new panel is:
public JButton getBtnLogin() {
if(btnLogin == null) {
try {
btnLogin = new JButton();
btnLogin.setBounds(140, 260, 125, 32);
btnLogin.setText("OK");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
HomeView1 hv = new HomeView1();
JPanel hsv = new HomeStudView();
JPanel bsv = new BtnStudView();
// auth user
User u = new User();
utype = u.getlogin(txtUserName.getText(), txtPsw.getText());
switch (utype){
case 3: // one of the views
// displays the buttons
//hv.buttonPanel.removeAll(); // get an error when K add this
hv.leftPanel.add(hv.getButtonPanel(bsv));
hv.remove(hv.newPanel);
hv.add(hv.getScreenPanel(hsv));
hv.newPanel.revalidate();
} // end switch
} // end actionperformed
});
}
catch (Throwable e) {
e.printStackTrace();
}
}
return btnLogin;
}
The
getButtonPanel] is a method in the HomeView class that initializes the button panel:
public JPanel getButtonPanel(JPanel buttonPanel) {
if(buttonPanel == null) {
try {
buttonPanel = new JPanel();
buttonPanel.setLayout(null);
newbPanel = buttonPanel;
}
catch (java.lang.Throwable e) {
e.printStackTrace();
}
}
buttonPanel.setLocation(0, 45);
newbPanel = buttonPanel;
return buttonPanel;
}
}
Please help!!! I really don't know what else to do and I need this application!!! I was thinking that maybe the different classes for each panel creates this problem..(I don't see why) or maybe is something smalI that I don't see it. I was trying to make this short one but it is impossible. :((
Thanks,
Irene