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();
}