Reputation: 73
I have a JCheckbox on a form that I'm trying to take the value of and put into a database. This is just a fragment of the code, but if it's not enough I can just go ahead and post the whole class (it's big and messy though, but I'll see how we go).
// Create checkbox
JCheckBox featuredCB = new JCheckBox();
topPanel.add(featuredCB);
//Take the value of it and put it in featuredCheck value
boolean featuredCheck = featuredCB.isSelected();
System.out.println(featuredCheck);
submitBT.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == submitBT)
{
idContent.setUser(userTF.getText());
idContent.setMovie(titleTF.getText());
idContent.setFeatured(featuredCheck);
idContent.setRating(Integer.parseInt(ratingTF.getText()));
if(owner.updateReview(isUpdate, idContent))
{
// success
try {
MovieReviewDSC.add(idContent);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
// fail
}
}
}
There are some other things in there it takes and passes through just fine, and that information shows up in the database and shows up as unchecked in my table model as well.
But I put the System.out.println(featuredCheck);
line in to test it and each time I run this it prints false even though I checked the checkbox. Any ideas?
Upvotes: 1
Views: 431
Reputation: 285430
You never check featuredCheck's state inside the ActionListener, but rather before the listener on code creation, before the user's had a chance to check it. Instead, inside of the ActionListener, you're checking the state of a boolean variable, featuredCheck, and its state is not going to magically change when the check box's state changes. Fix this: check the state of the JCheckBox (not the boolean variable) where its value is needed.
so.......
//!! boolean featuredCheck = featuredCB.isSelected(); // ***** get rid of this variable
submitBT.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == submitBT)
{
idContent.setUser(userTF.getText());
idContent.setMovie(titleTF.getText());
// !!! idContent.setFeatured(featuredCheck); // **** no *****
idContent.setFeatured(featuredCB.isSelected();); // *****yes ****
idContent.setRating(Integer.parseInt(ratingTF.getText()));
if(owner.updateReview(isUpdate, idContent))
{
// success
try {
MovieReviewDSC.add(idContent);
} catch (Exception e) {
e.printStackTrace();
}
} else
{
// fail
}
}
}
Upvotes: 2