0

I am working on a project from a textbook and I'm stuck. The goal is: When the GUI first appears, both buttons are visible, but when one button is clicked, that button disappears and only the other button is visible. Thereafter only one button is visible; when the button is clicked, it disappears and the other button appears

public class ButtonDemo extends JFrame implements ActionListener
{
    public static final int WIDTH = 400;
    public static final int HEIGHT = 300;

    public ButtonDemo()
    {
        setSize(WIDTH, HEIGHT);
        WindowDestroyer listener = new WindowDestroyer();
        addWindowListener(listener);

        Container contentPane = getContentPane();
        contentPane.setBackground(Color.WHITE);

        contentPane.setLayout(new FlowLayout());

        JButton sunnyButton = new JButton("Sunny");
        sunnyButton.addActionListener(this);
        contentPane.add(sunnyButton);
        JButton cloudyButton = new JButton("Cloudy");
        cloudyButton.addActionListener(this);
        contentPane.add(cloudyButton);
    }

    public void actionPerformed(ActionEvent e)
    {
        String actionCommand = e.getActionCommand();
        Container contentPane = getContentPane();

        if(actionCommand.equals("Sunny"))
        {
            contentPane.setBackground(Color.BLUE);
        }

        else if (actionCommand.equals("Cloudy"))
        {
            contentPane.setBackground(Color.GRAY);


        }
        else
            System.out.println("Error in button interface.");
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2668069
  • 249
  • 1
  • 3
  • 7
  • what have you tried so far? There is a setVisible(boolean) method that seems to be a good start – mschenk74 May 26 '14 at 16:29
  • I have tried to use the setVisible method in the if statement of each sunny and cloudy but it closes the application when i just write serVisible(false) – user2668069 May 26 '14 at 16:32
  • @Infested I'm pretty sure with swing, the button will repaint itself – Cruncher May 26 '14 at 16:34
  • @Cruncher may be, but the frame isnt and thats what its painted on. if the buttons were static you wouldnt need to repaint the whole thing. – Ariel Pinchover May 26 '14 at 16:35
  • @Infested Swing takes care of it all for you. I promise. – Cruncher May 26 '14 at 16:39
  • 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 May 27 '14 at 03:50

3 Answers3

3

Using setVisible on the buttons should work. Try the following:

Move the following lines to the fields of ButtonDemo:

JButton sunnyButton = new JButton("Sunny");
JButton cloudyButton = new JButton("Cloudy");

Change the if statements in your actionPerformed to:

if(actionCommand.equals("Sunny"))
{
    contentPane.setBackground(Color.BLUE);
    sunnyButton.setVisible(false);
    cloudyButton.setVisible(true);
}
else if (actionCommand.equals("Cloudy"))
{
    contentPane.setBackground(Color.GRAY);
    sunnyButton.setVisible(true);
    cloudyButton.setVisible(false);
}
Cruncher
  • 7,641
  • 1
  • 31
  • 65
1

It's very simple code. Make sunnyButton and cloudyButton as instance member.

Simply check for the source of the action event and hide the source component and show the other one.

public void actionPerformed(ActionEvent e) {

    if (sunnyButton == e.getSource()) {
        sunnyButton.setVisible(false);
        cloudyButton.setVisible(true);
    } else if (cloudyButton == e.getSource()) {
        sunnyButton.setVisible(true);
        cloudyButton.setVisible(false);
    }
    ...
}
Braj
  • 46,415
  • 5
  • 60
  • 76
  • I tried that but the button did not dissapear though, i made sunnyButton and cloudyButton instance variables of class ButtonDemo – user2668069 May 26 '14 at 17:38
  • Find the [CODE here](http://ideone.com/MH58N7) and validate with your code or try this one. – Braj May 26 '14 at 17:43
0

What about

source.setVisible(false) 

to hide and

source.setVisible(true) 

to show the buttons?

Edit: You should get the source of the event (e.getSource()) and hide it

lihudi
  • 860
  • 1
  • 12
  • 21