Search code examples
javaswingjpaneljbuttonactionlistener

How do you change contents of a JPanel based on a previously selected JButton?


I'm making an interface for a community. The options include "Add Person", "Add to Family" and "Remove Member from Family". I thought making multiple JPanels was very time consuming so I made the JPanel dependent on the user's "choice". For example,

if(choice == 1)
 {
        addPTitle = new JLabel("ADD PERSON");
        addPTitle.setHorizontalAlignment(SwingConstants.CENTER);
        addPTitle.setBounds(75,20,350,50);
        addPTitle.setFont(calibri);
        addPTitle.setForeground(red);
    }
    else if(choice == 2)
    {
        addPTitle = new JLabel("ADD TO FAMILY");
        addPTitle.setHorizontalAlignment(SwingConstants.CENTER);
        addPTitle.setBounds(75,20,350,50);
        addPTitle.setFont(calibri);
        addPTitle.setForeground(red);
    }
    else if(choice == 3)
    {
        addPTitle = new JLabel("REMOVE MEMBER");
        addPTitle.setHorizontalAlignment(SwingConstants.CENTER);
        addPTitle.setBounds(75,20,350,50);
        addPTitle.setFont(calibri);
        addPTitle.setForeground(red);
    }

It works fine when I change the value of choice manually but when I tried adding an ActionListener for the buttons themselves, the value of choice didn't change and the contents of the JPanel that were displayed were still based from the value I set manually. Here's my code for the ActionListener:

private class ButtonHandler implements ActionListener{
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == "ADD PERSON")
        {
            choice = 1;
            frame.setContentPane(addP);
            frame.invalidate();
            frame.validate();
        }
        else if(e.getSource() == "ADD TO FAMILY"){
            choice = 2;
            frame.setContentPane(addP);
            frame.invalidate();
            frame.validate();
        }
        else if(e.getSource() == "REMOVE MEMBER FROM FAMILY"){
            choice = 3;
            frame.setContentPane(addP);
            frame.invalidate();
            frame.validate();
        }

Solution

  • e.getSource() return an object. You are trying to compare it to a string. Instead you can use e.getActionCommand() (assuming you haven't changes the action command)

    Also in case you're temped, don't compare strings with ==. Use equals

    if ("ADD PERSON".equals(e.getActionCommand()) {}
    

    Or if your buttons scope are accessble in the ActionListener you can compare the object,

    if (e.getSource() == addPersonButton) {}
    

    Another option, in case for any reason you did change the action command you can also use the text of the button to compare

    JButton button = (JButton)e.getSource();
    String text = button.getText();
    if ("ADD PERSON".equals(text)) {}
    

    SIDE NOTE

    • You should look into using a CardLayout that lets you change views. You say creating extra panels is time consuming, but debugging problems like this may be even more time consuming ;) See this simple CardLayout example and see How to use CardLayout