0

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();
        }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
2562166
  • 17
  • 7
  • 1) For many components in one space, use a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as seen in this [short example](http://stackoverflow.com/a/5786005/418556). 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson Apr 01 '14 at 09:06
  • .. 3) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Apr 01 '14 at 09:07

2 Answers2

2

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
Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
1

you should revalidate you panel .

use Jpanel.revalidate().

M.Raheel
  • 165
  • 7
  • The changing of panels is okay. My problem is that the value of choice doesn't change. I tried setting choice to 0. After choosing all three options, the value was still 0. – 2562166 Apr 01 '14 at 06:11
  • okay after your button listener set choice =0; and please compare string with .equals() function – M.Raheel Apr 01 '14 at 06:15
  • Should I put the revalidate inside the ActionListener? I tried placing it there but the contents that depended on the value of choice weren't displayed. – 2562166 Apr 01 '14 at 06:24
  • yes but after all above logic at the end you place it – M.Raheel Apr 01 '14 at 06:30
  • I tried this and I still can't figure out how to make it work. The value of choice changes. The panel is displayed but the contents that depend on the value of choice are still missing. I also tried repaint(); but it doesn't work. It only works when I set the value of choice manually before I run the program – 2562166 Apr 01 '14 at 06:52
  • dear don't use if-else in this case the best thing is you use case ans switch choice. – M.Raheel Apr 01 '14 at 06:59
  • switch choice: case 1: – M.Raheel Apr 01 '14 at 07:00