aspose file tools*
The moose likes Beginning Java and the fly likes Checking whether check boxes are checked Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Checking whether check boxes are checked" Watch "Checking whether check boxes are checked" New topic
Author

Checking whether check boxes are checked

Paul Trow
Greenhorn

Joined: Feb 20, 2005
Posts: 8
How do I tell whether a check box is checked? I have

Checkbox cb1 = new Checkbox();
//Do I need the following line?
cb1.addItemListener(this);

Then, when a button is pressed, I want something to happen if the checkbox is checked. Is this how to do so?

public void actionPerformed(ActionEvent event) {
if (cb1.isSelected == false) {Do something useful here}

One other question: how do you put two items side by side in a GUI? Specifically, a label and a checkbox?

Thanks.
joseph mcgratton
Ranch Hand

Joined: Feb 15, 2005
Posts: 41
hello there.

im in the middle of using checkboxes as well so maybe i can help.

if you want a property of one object to change when u select the checkbox, use

public void jButton1_actionPerformed(ActionEvent e) {

if (jCheckBox1.isSelected()){
jTextArea1.setText("Hi");
}

this code means that when the button is pressed and the checkbox is selected, you can make something else happen, such as put text in a text box.

hope this is helpful.

p.s. to put objects beside each other have a look at layouts. One method to use is to give the jpanel an XY layout, that way you can decide the objects coordinates easily.


[ February 20, 2005: Message edited by: joseph mcgratton ]
[ February 20, 2005: Message edited by: joseph mcgratton ]
Paul Trow
Greenhorn

Joined: Feb 20, 2005
Posts: 8
Joseph,

Thanks for your reply. I have

public void actionPerformed(ActionEvent event) {
if (cb1.isSelected) {
}

and I'm getting the error message

cannot find the symbol variable isSelected

Any suggestions?
joseph mcgratton
Ranch Hand

Joined: Feb 15, 2005
Posts: 41
hello.

Java is very sensitive when it comes to syntax and the such.

your example:

public void actionPerformed(ActionEvent event) {
if (cb1.isSelected) {
}

changing it to something like this might help.

public void NAMEOFBUTTON_actionPerformed(ActionEvent e){
if (cb1.isSelected()){
DO_THIS
}

please note that the two words in capitals need to be changed. there are 3 differences between mine and yours.

1) you have not identified the button's name
2) you have left out "e" after ActionEvent
3) you haveleft out () after cb1.isSelected

i hope you get you probelm fixed,

joseph.
Paul Trow
Greenhorn

Joined: Feb 20, 2005
Posts: 8
Hi,

JButton button = new JButton("Roll dice");
button.addItemListener(this);
displayPanel1 = new JPanel();
Checkbox cb1 = new Checkbox();
cb1.addItemListener(this);

throws the error message

addItemListener(java.awt.event.ItemListener) in javax.AbstractButton cannot be applied to (Yahtzee)

What's up with that?
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Paul Trow:
addItemListener(java.awt.event.ItemListener) in javax.AbstractButton cannot be applied to (Yahtzee)

What's up with that?
addItemListener() expects an object that implements java.awt.event.ItemListener. Does your Yahtzee class implement java.awt.event.ItemListener?
Paul Trow
Greenhorn

Joined: Feb 20, 2005
Posts: 8
Maybe not. The first four lines are

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.URL;

public class Yahtzee implements ActionListener {

I guess I don't really understand the difference between ActionListener and ItemListener. I don't understand why it has to be so complicated - all I want is for something to happen when a button is clicked and a check box is checked.

Anyway, thanks for your help.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you don't really need the item listener, here's a simple demo (the way you've
described wgat you're doing)

you don't even need the button with this, just change the code to add the
listener to the checkbox

David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Paul Trow:
Maybe not. ...

public class Yahtzee implements ActionListener {

I guess I don't really understand the difference between ActionListener and ItemListener.
The first step is to look up both ActionListener and ItemListener in Sun's JavaDocs. In there you'll find a handy link to How to Use Buttons, Check Boxes, and Radio Buttons that will have more details.

The basic difference is that an ItemListener receives a different method callback and is oriented more to item selection rather than low-level "actions." For example, the ItemEvent passed to ItemListener.itemStateChanged() has a method to determine the new state (SELECTED -- checked -- or DESELECTED -- unchecked) so you don't have to query the JCheckbox itself. The docs and the link to the tutorial will provide you a deeper understanding of which to use and when.

While I haven't done Swing in a long while, I would guess that you can skip ItemListener and use ActionListener for both regular buttons and checkboxes. When the checkbox is the event source, just ask for its current state.
Paul Trow
Greenhorn

Joined: Feb 20, 2005
Posts: 8
OK, I got it working - thanks to all of you. For some reason, I do need an addActionListener for the button - if I remove it, the button ceases to work. But I don't need an addItemListener for the check boxes. (Doesn't seem very logical.)
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Paul Trow:
For some reason, I do need an addActionListener for the button - if I remove it, the button ceases to work. But I don't need an addItemListener for the check boxes.
When you press a normal button, it fires a message to its action listeners. That's all it does. It has no built-in functionality.

When you press a checkbox -- look at JToggleButton.setPressed() -- it fires an action event. When you release the mouse on the button, the button itself toggles its checked state and then fires an item change event. Because it handles toggling the state, you don't need events to be able to "use" the checkbox; you only need an event if you want to do something as a result of its checked state changing.

Or did I misundertsand what you're trying to do?
Paul Trow
Greenhorn

Joined: Feb 20, 2005
Posts: 8
OK, I think I get it. If someone presses the button, I want something to happen right away - so I need addActionListener. But if someone just checks the check box, I don't need anything to happen right away - I just need to know later on that the box is checked. So I don't need addItemListener. Thanks for clarifying that.

Paul
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
You got it.

An example of where you would want to set up an ItemListener is if checking the checkbox should enable some other input field. Say you have this in your application:If the checkbox is unchecked, the entry box should be disabled since it makes no sense. When the user checks the box, the entry box should be enabled.
 
jQuery in Action, 2nd edition
 
subject: Checking whether check boxes are checked