I am having trouble with JCheckbox
es, although I have used them often before.
Basically, I create a very simple window with check boxes, then check if they are selected. When doing that check, it displays the JCheckbox
as selected, even if it is not. Here is my code. To reproduce my issue, run the project, then click Start. Even if the createStatisticsFilesCheckBox
is set as not selected, from the constructor, checking if it is selected in the ActionListener
method returns true
. Thanks in advance!
package Queries;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends Thread implements ActionListener
{
private JButton cancelButton, backButton, startButton;
private JCheckBox createQueriesCheckBox,createStatisticsFilesCheckBox, createPokerHandIDFilesCheckBox;
private JFrame frame;
public void actionPerformed(ActionEvent e)
{
if ("Start".equals(e.getActionCommand()))
{
if (createQueriesCheckBox.isSelected() == true);
{
// Code here
}
if (createStatisticsFilesCheckBox.isSelected() == true);
{
// Code here
// Always show as selected
System.out.println("Test");
}
if (createPokerHandIDFilesCheckBox.isSelected() == true);
{
// Code here
}
start();
}
else if ("Back".equals(e.getActionCommand()))
{
// Code here
}
else if ("Cancel".equals(e.getActionCommand()))
{
System.exit(0);
}
}
public Test()
{
JPanel mainPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(4, 4, 4, 4);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 1;
gbc.gridwidth = 1;
mainPanel.add(checkBoxesPanel(), gbc);
gbc.gridy++;
mainPanel.add(buttonsPanel(), gbc);
frame = new JFrame("Actions");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(mainPanel);
frame.setVisible(true);
frame.pack();
}
/**
* Panel that contains the Cancel and Continue buttons
*
* @return panel
*/
public JPanel buttonsPanel()
{
JPanel buttonsPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.EAST;
cancelButton = new JButton("Cancel");
cancelButton.addActionListener(this);
buttonsPanel.add(cancelButton, gbc);
backButton = new JButton("Back");
backButton.addActionListener(this);
gbc.gridx++;
buttonsPanel.add(backButton, gbc);
startButton = new JButton("Start");
startButton.addActionListener(this);
gbc.gridx++;
buttonsPanel.add(startButton, gbc);
return buttonsPanel;
}
/**
* Panel that contains the check boxes for the types of queries
*
* @return panel
*/
public JPanel checkBoxesPanel()
{
JPanel checkBoxesPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(4, 4, 4, 4);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.WEST;
createQueriesCheckBox = new JCheckBox("Create queries", true);
checkBoxesPanel.add(createQueriesCheckBox, gbc);
createStatisticsFilesCheckBox = new JCheckBox("Create statistics files", false);
gbc.gridy++;
checkBoxesPanel.add(createStatisticsFilesCheckBox, gbc);
createPokerHandIDFilesCheckBox = new JCheckBox("Create PokerHandID files", false);
gbc.gridy++;
checkBoxesPanel.add(createPokerHandIDFilesCheckBox, gbc);
return checkBoxesPanel;
}
public static void main(String[] args)
{
new Test();
}
public void run()
{
System.exit(0);
}
}
if (createStatisticsFilesCheckBox.isSelected() == true);
You have a trailing ";" on all your if statements which ends the if statement.
Therefore every statement after the if statement is executed unconditionally.
Get rid of the ";".