action listeners

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon

    action listeners

    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.


  • Jon

    #2
    Re: action listeners


    "Jon" <ivegotsexyhair @blueyonder.co. uk> wrote in message
    news:Nyp2b.277$ yc5.67@news-binary.blueyond er.co.uk...[color=blue]
    > 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[/color]
    button[color=blue]
    > is pressed. The button works on its own, but I can not get it to work[/color]
    when[color=blue]
    > 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.
    >
    >[/color]

    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.


    Comment

    • Jon

      #3
      Re: action listeners


      "Chris" <chris2k01@hotm ail.com> wrote in message
      news:nZq2b.5084 5$K44.23625@edt nps84...[color=blue]
      > -----BEGIN PGP SIGNED MESSAGE-----
      > Hash: SHA1
      >
      > Jon wrote:
      >[color=green]
      > > 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.[/color]
      >
      > 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.isSel ected()) {
      > // do stuff for radio 1
      > } else if (rButton2.isSel ected()) {
      > // 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/SdmvwxczzJRavJY RAp1PAJ9uWn5iD3 VU9rQAgtkY0FCqk PdFBACghcYZ
      > k/AxGRzBGdvlsq6Ou a7HIP8=
      > =v2Zz
      > -----END PGP SIGNATURE-----[/color]

      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)((JComb oBox)source).ge tSelectedItem() ;
      if(choice=="Nam e 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.


      Comment

      Working...