kyros
kyros

Reputation: 153

JPanel won't close after switching cardLayout

I have multiple JPanels in my cardLayout. When I switch to another JPanel it will not close down the previous JPanels and I'm left with many JPanels open at the same time after using the program:

import javax.swing.*;
import java.awt.*;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.event.*;

public class guiFrames extends JFrame implements ActionListener{

  public  String name = "login";
    public boolean isDev = false;
    public boolean isMan = false;
  public  JButton loginSubmit, loginCancel,
        newIssue, newUser, list,
        detailsCancel, detailsSubmit, detailsAssign, detailsClose, detailsValidate, detailsFail,
        addUserCancel, addUserSubmit;
  public  JPanel cardGUI;
  public  JTextField loginTxt, pwTxt, userTxt, userPWTxt, issueDetailsTxt;
  public  JComboBox userCmb;
  public  CardLayout cards;

  public guiFrames(){
  }

  public Component inputFrame(){

    JPanel inputPnl = new JPanel();
    inputPnl.setLayout(new GridLayout(3,2));

    JLabel loginLbl = new JLabel("Login");
    inputPnl.add(loginLbl);
    loginTxt = new JTextField();
    inputPnl.add(loginTxt);
    JLabel pwLbl = new JLabel("Password");
    inputPnl.add(pwLbl);
    pwTxt = new JTextField();
    inputPnl.add(pwTxt);

    JPanel buttonPnl = new JPanel();
    buttonPnl.setLayout(new FlowLayout(FlowLayout.LEFT, 1,5));

    loginSubmit = new JButton("Submit");
      loginSubmit.addActionListener(this);
    buttonPnl.add(loginSubmit);
    loginCancel = new JButton("Cancel");
      loginCancel.addActionListener(this);
    buttonPnl.add(loginCancel);

    JPanel container = new JPanel();
    container.setLayout(new BorderLayout());
    container.add(inputPnl, BorderLayout.CENTER);
    container.add(buttonPnl, BorderLayout.SOUTH);

    container.setVisible(true);

    return container;
  }

  public Component issueList(){

  JPanel listPnl = new JPanel();

  JPanel listFlow = new JPanel();
  listFlow.setLayout(new FlowLayout(FlowLayout.LEFT, 1,5));

  newIssue = new JButton("New");
      newIssue.addActionListener(this);
  listFlow.add(newIssue);
  list = new JButton("Issues");
      list.addActionListener(this);
  listFlow.add(list);
  newUser = new JButton("Add User");
      newUser.setVisible(isMan);
      //newUser.setEnabled(false);
      newUser.addActionListener(this);
  listFlow.add(newUser);


  JPanel container = new JPanel();
  container.setLayout(new BorderLayout());
  container.add(listFlow, BorderLayout.CENTER);

  listPnl.add(container);
//  listPnl.pack();
  listPnl.setVisible(true);
//  listPnl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  return listPnl;
 }

  public Component issueDetails(){
  JList list = new JList();
      issueDetailsTxt = new JTextField();
      issueDetailsTxt.addActionListener(this);

  JPanel detailsFlow = new JPanel();
  detailsFlow.setLayout(new FlowLayout(FlowLayout.LEFT, 1,5));

  detailsCancel = new JButton("Cancel");
      detailsCancel.addActionListener(this);
  detailsFlow.add(detailsCancel);

      detailsFlow.add(issueDetailsTxt);
  detailsSubmit = new JButton("Submit");
      detailsSubmit.addActionListener(this);
  detailsFlow.add(detailsSubmit);

  detailsAssign = new JButton("Assign");
      detailsAssign.setVisible(isMan);
      detailsAssign.addActionListener(this);
  detailsFlow.add(detailsAssign);
  detailsClose = new JButton("Close");
      detailsClose.setVisible(isMan);
      detailsClose.addActionListener(this);
  detailsFlow.add(detailsClose);
  detailsValidate = new JButton("Validate");
      detailsValidate.setVisible(isDev);
      detailsValidate.addActionListener(this);
  detailsFlow.add(detailsValidate);
  detailsFail = new JButton("Fail");
      detailsFail.addActionListener(this);
  detailsFlow.add(detailsFail);
      detailsFail.setVisible(isDev);

  JPanel container = new JPanel();
  container.setLayout(new BorderLayout());
  container.add(detailsFlow, BorderLayout.SOUTH);
  container.add(list, BorderLayout.CENTER);
      container.add(issueDetailsTxt, BorderLayout.NORTH);

  container.setVisible(true);

  return container;

  } 

  public Component addUser(){

  JPanel addUserGrid = new JPanel();
  addUserGrid.setLayout(new GridLayout(3,2));


  JLabel loginLbl = new JLabel("Login");
  addUserGrid.add(loginLbl);
  userTxt = new JTextField();
  addUserGrid.add(userTxt);
  JLabel pwLbl = new JLabel("Password");
  addUserGrid.add(pwLbl);
  userPWTxt = new JTextField();
  addUserGrid.add(userPWTxt);

  JLabel userLbl = new JLabel("User Type");
  addUserGrid.add(userLbl);
  userCmb = new JComboBox();
  userCmb.addItem("User");
  userCmb.addItem("Manager");
  userCmb.addItem("Developer");
      userCmb.addActionListener(this);
  addUserGrid.add(userCmb);

  JPanel addUserFlow = new JPanel();
  addUserFlow.setLayout(new FlowLayout(FlowLayout.LEFT, 1,5));

  addUserCancel = new JButton("Cancel");
      addUserCancel.addActionListener(this);
  addUserFlow.add(addUserCancel);
  addUserSubmit = new JButton("Submit");
      addUserSubmit.addActionListener(this);
  addUserFlow.add(addUserSubmit);

  JPanel container = new JPanel();
  container.setLayout(new BorderLayout());
  container.add(addUserFlow, BorderLayout.SOUTH);
  container.add(addUserGrid, BorderLayout.CENTER);

  container.setVisible(true);

  return container;
  }

  public void cardView(){
        JFrame cardFrame = new JFrame(name);
        cardGUI = new JPanel();
        cards = new CardLayout();
        cardGUI.setLayout(cards);
        cardGUI.add(inputFrame(), "login");
    cardGUI.add(issueList(), "issueList");
    cardGUI.add(issueDetails(), "issueDetails");
    cardGUI.add(addUser(), "addUser");

        cardFrame.add(cardGUI, BorderLayout.CENTER);
        cardFrame.pack();
        cardFrame.setVisible(true);
        cardFrame.setDefaultCloseOperation(cardFrame.EXIT_ON_CLOSE);

     cards.show(cardGUI, name);
}

  public void actionPerformed(ActionEvent e){

    if(e.getSource() == loginSubmit){
        manager newUsr = new manager();

        if(newUsr.loginValidation(loginTxt.getText(), pwTxt.getText())){
            name = "issueList";
            isMan = newUsr.isManager();
            isDev = newUsr.isDeveloper();
            System.out.println("User Type: "+isMan);
            cardView();
        }else{
            loginTxt.setText("");
            pwTxt.setText("");
        }
    }else if(e.getSource() == loginCancel){
        loginTxt.setText(name);
        pwTxt.setText("");
        cardView();
        //cards.show(cardGUI, "login");
    }else if (e.getSource() == newIssue){//Goto New Issue Screen
        name = "issueDetails";
        cardView();
    }else if(e.getSource() == newUser){
        name = "addUser";
        cardView();//Goto Add User Screen
    }else if(e.getSource() == addUserCancel){//Go back to Issue List Screen
        name = "issueList";
        cardView();
    }else if(e.getSource() == addUserSubmit){//Add user
        manager newUsr = new manager();
        newUsr.createUser(userTxt.getText(), userPWTxt.getText(), userCmb.getSelectedItem());

        userTxt.setText("");
        userPWTxt.setText("");

    }else if(e.getSource() == list){//Goto Issue Details Screen
        name = "issueDetails";
        cardView();
    }else if(e.getSource() == detailsCancel){//Go Back to Issue List Screen
        name = "issueList";
        cardView();
    }else if(e.getSource() == detailsSubmit){

    }else if(e.getSource() == detailsAssign){

    }else if (e.getSource() == detailsClose){

    }else if (e.getSource() == detailsValidate){

    }else if (e.getSource() == detailsFail){

    }
  }
}

Here's the main file:

  public class testSerializable{
    public static void main(String[] args){
       manager newUsr = new manager();
       newUsr.loadUserInfo();
       guiFrames gui = new guiFrames();
       gui.cardView();
     }  
  }

Upvotes: 0

Views: 175

Answers (1)

adchilds
adchilds

Reputation: 963

Since you're using a Container to add your JPanels to, you have the removeAll method available.

You're adding the components to your container, so I'd assume that you would need to remove all components from this Container, then add the new components, and refresh it.

Pseudocode:

container.removeAll();

container.add(NEW_COMPONENTS);

container.revalidate();
container.repaint();

Upvotes: 1

Related Questions