0

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();
     }  
  }
Franz Kafka
  • 10,623
  • 20
  • 93
  • 149
kyros
  • 153
  • 1
  • 2
  • 11
  • 1
    What do you mean the panel is open? Only one panel will be visible at a time when you use a CardLayout. The other panels will still be in memory to you can switch back to them at any time. – camickr Apr 15 '13 at 19:01
  • *"Here's the main file"* For better help sooner, post an [SSCCE](http://sscce.org/). In this case you would need to move the `main` into `guiFrame` and demote `manager` from `public` to either default or `private` access and include it at the end of the `guiFrame` source. – Andrew Thompson Apr 16 '13 at 02:57
  • Please learn java naming conventions and stick to them. – kleopatra Apr 16 '13 at 09:54

1 Answers1

1

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();
adchilds
  • 963
  • 2
  • 9
  • 22
  • container.revalidate() and container.repaint(required in some cases) not, wrong JComponent.repaint_itsefl – mKorbel Apr 15 '13 at 18:52
  • @mKorbel, I'm sorry, I can't understand what you're saying. Can you reiterate please? – adchilds Apr 15 '13 at 18:55
  • 1
    @adchilds, You don't revalidate() the Component. You revalidate() the Container. – camickr Apr 15 '13 at 19:00
  • `contianer` is responsible for repainting its child(s) (`container.revalidate()` and `container.repaint()`) this is notifiers for (used) `LayoutManager`, please to amend this post, with ordering should be [`1.` remove `2.` add `3.` revalidate and repaint](http://stackoverflow.com/a/6989230/714968) – mKorbel Apr 15 '13 at 19:01
  • +1 for correct direction, but there could be bug in OP's code and CardLayout – mKorbel Apr 15 '13 at 19:06
  • `CardLayout` is the way to go for this use. – Andrew Thompson Apr 16 '13 at 02:55