Kira Katou
Kira Katou

Reputation: 158

change panel from second class to first class

i am doing my exercise about insert data with login admin, student and teacher. but i got stack at logout.

First Class Test5.java

  public class Test5 extends JFrame implements ActionListener , ItemListener {
  public Container contentPane = this.getContentPane();
    public Test5(){
    CardLayout card = new CardLayout();
    setLayout(card);
    setSize(1366,768);
    setTitle("Student Data Editor");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(true);
    try{
        Class.forName("com.mysql.jdbc.Driver"); 
        conn = DriverManager.getConnection("jdbc:mysql://localhost/kampus", "root", "abc");
        stmt = conn.createStatement();
    }catch(Exception e){
    }
    dosen dos = new dosen();
    siswa sis = new siswa();
    contentPane.add("main", main);
    contentPane.add("student", sis.panel_siswa);
    contentPane.add("dosen", dos.panel_dosen);
    main = new JPanel();   //first Panel
    main.setLayout(new GridBagLayout());
    label_main = new JLabel("ID");
    label_main2 = new JLabel("Password");
    tf_main = new JTextField(15);
    tf_main2 = new JPasswordField(15);
    login = new JButton("LOGIN");
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("LOGIN")){
            card.show(contentPane, "dosen");
        }
    }
}

Second Class dosen.java

public class dosen implements ActionListener{
JPanel panel_dosen;
    public dosen(){
    panel_dosen = new JPanel(); //second panel
    panel_dosen.setLayout(new BorderLayout());
    panel_dosen.add(panel_combo, BorderLayout.NORTH);
    panel_dosen.add(scroll, BorderLayout.CENTER);
    panel_dosen.add(panel_input, BorderLayout.SOUTH);
}
    public void actionPerformed(ActionEvent e){
          if(e.getActionCommand().equals("Logout")){ //it didnt work at all and dont have any error
             Test5 tes = new Test5();
             tes.add(tes.main);
             tes.remove(panel_dosen);
             tes.tf_main.setText("");
             tes.tf_main2.setText("");
          }
   }
}

Problem

First Panel enter image description here

Second Panel enter image description here

Question

how to make second panel removed and add first panel add jframe ?

Upvotes: 0

Views: 29

Answers (2)

Natecat
Natecat

Reputation: 2182

This is a use case for CardLayout. Make CardLayout your JFrame's layout manager instead of BorderLayout

Upvotes: 1

camickr
camickr

Reputation: 324118

how to make second panel removed and add first panel add jframe ?

Use a CardLayout. The CardLayout was specifically designed to allow the swapping of panels in the same space.

Read the section from the Swing tutorial on How to Use CardLayout for more information and working examples.

Upvotes: 1

Related Questions