Jake Stanger
Jake Stanger

Reputation: 449

Java - Action Listener only responds to one if statement?

I am currently making a custom Minecraft Server launcher in Java and have got to an early stage where I'd actually like buttons to do something. I managed to get one button to respond (the start button) but as soon as I put in a second if statement to make the stop button respond, the start button which worked previously now does not. I cannot test the stop button because it is disabled by default. When I switched the if statements around (putting the stopBtn actionlistener first) the start button worked again, but the stop button does not. Please can someone have a look at the code and help?

package custommcserver;

import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;

class Window extends JFrame implements ActionListener
{
    JPanel mainPnl = new JPanel(new GridLayout(2,1));
    JPanel propPnl = new JPanel();
    JButton startBtn = new JButton("Start");
    JButton stopBtn = new JButton("Stop");
    JButton propBtn = new JButton("Properties");

    public Window()
    {
        super("Custom Minecraft Server Launcher") ;
        setSize(500,200) ; 
        setDefaultCloseOperation(EXIT_ON_CLOSE) ;
        add(mainPnl) ;
        mainPnl.add(startBtn);
        mainPnl.add(stopBtn);
        mainPnl.add(propBtn);
        stopBtn.setEnabled(false);
        startBtn.addActionListener(this);
        stopBtn.addActionListener(this);
        propBtn.addActionListener(this);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent event) 
    {

        if (event.getSource() == stopBtn);
        {
             stopBtn.setEnabled(false);
             startBtn.setEnabled(true);
        }

         if (event.getSource() == startBtn);
         {
             stopBtn.setEnabled(true);
             startBtn.setEnabled(false);
        }

      }
 }

Upvotes: 2

Views: 6114

Answers (1)

imulsion
imulsion

Reputation: 9040

You have put semi-colons after the if statements. Take them away:

if (event.getSource() == stopBtn)
        {
             stopBtn.setEnabled(false);
             startBtn.setEnabled(true);
        }

         if (event.getSource() == startBtn)
         {
             stopBtn.setEnabled(true);
             startBtn.setEnabled(false);
        }

Upvotes: 6

Related Questions