470,849 Members | 1,114 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

action listeners

Jon
Hi, im trying to create an interface, which has a button and 2 radio
buttons, which affect text which is outputted to a text area when the button
is pressed. The button works on its own, but I can not get it to work when
one of the radio buttons is selected. I can also get the radio buttons
working separetly, but I need the effect of the radiobutton to take place
only when the button is pressed.
Here is the code I am using for the event handling:

Object source = evt.getSource();
if(source == button)
{
if(source == rButton1)
{
//manipulate and display text
}
}

Also, does anyone know of any good java reference websites other than
java.sun.com?
Thanks,
Jon.
Jul 17 '05 #1
2 3547
Jon

"Jon" <iv************@blueyonder.co.uk> wrote in message
news:Ny**************@news-binary.blueyonder.co.uk...
Hi, im trying to create an interface, which has a button and 2 radio
buttons, which affect text which is outputted to a text area when the button is pressed. The button works on its own, but I can not get it to work when one of the radio buttons is selected. I can also get the radio buttons
working separetly, but I need the effect of the radiobutton to take place
only when the button is pressed.
Here is the code I am using for the event handling:

Object source = evt.getSource();
if(source == button)
{
if(source == rButton1)
{
//manipulate and display text
}
}

Also, does anyone know of any good java reference websites other than
java.sun.com?
Thanks,
Jon.


I have also tried:
if(rButton1 == true)
{
....
}
but I get an error saying I cant use ++ with the Boolean value.
With the code in the last post, it ran the code ok, but nothing happened
when I pressed the buttons even though I had added action listeners and
implemented it into the class.
Jul 17 '05 #2
Jon

"Chris" <ch*******@hotmail.com> wrote in message
news:nZq2b.50845$K44.23625@edtnps84...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jon wrote:
Hi, im trying to create an interface, which has a button and 2 radio
buttons, which affect text which is outputted to a text area when
the button
is pressed. The button works on its own, but I can not get it to
work when
one of the radio buttons is selected. I can also get the radio
buttons working separetly, but I need the effect of the radiobutton
to take place only when the button is pressed.
Here is the code I am using for the event handling:

Object source = evt.getSource();
if(source == button)
{
if(source == rButton1)
{
//manipulate and display text
}
}

Also, does anyone know of any good java reference websites other
than java.sun.com?
Thanks,
Jon.


Hi,
Assuming you're using Swing (not AWT), you need to replace the above
fragment of code with the following:

Object source = evt.getSource();
if (source == button) {
if (rButton1.isSelected()) {
// do stuff for radio 1
} else if (rButton2.isSelected()) {
// do stuff for radio 2
}
}

The point is, that the source variable is a reference to an object.
The code you tried will never get executed, since the source variable
contains the "button" reference, but if it does, it will not contain
"rButton1". It's like saying that if x is set to 5, it can't be 7 at
the same time.

The source object is the button the user actually clicked on. If you
need the state of the other buttons, you ask for it from the buttons
themselves.

If you're using AWT instead of Swing, replace isSelected() with
getState().

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/SdmvwxczzJRavJYRAp1PAJ9uWn5iD3VU9rQAgtkY0FCqkPdFBA CghcYZ
k/AxGRzBGdvlsq6Oua7HIP8=
=v2Zz
-----END PGP SIGNATURE-----


Thanks for the help, I thougth it might be a method, but I haven't been
using java for long, so I wasn't aware of it.
I'm trying to do the same with combo boxes, but am only aware of using the
following:

String choice = (String)((JComboBox)source).getSelectedItem();
if(choice=="Name of item")
{
//execute code
}

but this does not work with the button. I have a feeling that it is because
of the same reason the radio buttons would not work because I am using
source twice, but again, I am not aware of any methods that I can use.
Thanks.
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Hal Vaughan | last post: by
3 posts views Thread by Rich Wahl | last post: by
1 post views Thread by Fred Mellender | last post: by
2 posts views Thread by Michael Powe | last post: by
3 posts views Thread by Absolon | last post: by
6 posts views Thread by Zytan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.