Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,471
There are 1159 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     Other Programming Languages :

'Java' ActionListeners

Thread title: 'Java' ActionListeners
Closed Thread    
    Thread tools Search this thread Display Modes  
12-25-2007, 04:03 PM
#1
Seanneo is offline Seanneo
Status: Junior Member
Join date: May 2007
Location:
Expertise:
Software:
 
Posts: 26
iTrader: 1 / 100%
 

Seanneo is on a distinguished road

  Old  'Java' ActionListeners

Hey,
I have used an actionlistener for a button in a java applet. So when when i click the button it prints something out on the screen. This all works fine.

What i am curious about is how i can create 2 buttons using actionlisteners, so i can have 2 buttons that can print out different things on the screen.

I pritty sure i have to initialise the values as actionlisteners but how do i seperate their actions.

Hope this makes some sense. Dont really have code to show you im afraid. Have been searching on websites for a while to no avail. Hope someone can help.

Thx in advance,
Seanneo

12-26-2007, 11:51 AM
#2
Dr John is offline Dr John
Status: Junior Member
Join date: May 2005
Location:
Expertise:
Software:
 
Posts: 77
iTrader: 0 / 0%
 

Dr John is on a distinguished road

  Old

I've had to look at some code I work two years ago, as I haven't used Java since then (and Java is NOT JavaScript)

Where you have your public void actionPerformed(ActionEvent e)
(which is inside the class that implements the actionListener),
you have inside it a series of IFs

eg
if (e.getSource() == bBars)
{some thing to do}
if(e.getSource() == bLines)
{some other things to do}
if(e.getSource() == bPies)
{even more things to do}
if(e.getSource() == bExit)
{guess what this did next}

Where each bSomething was an instance of a button, in this case ones for drawing graphs. BUT I'm very out of practice with Java, so even though I was very good at it back then, I'm a bit limited in what I can remember now. Strangely, I was top of the class but never actually liked Java.

12-26-2007, 12:29 PM
#3
Seanneo is offline Seanneo
Status: Junior Member
Join date: May 2007
Location:
Expertise:
Software:
 
Posts: 26
iTrader: 1 / 100%
 

Seanneo is on a distinguished road

  Old

thx Dr John for your reply i actually figured this one out by myself, i had searched the web for ages and not one site had something on it...

I set a string in this case i have called it 'whichButton' to the .getActionCommand(); of action 'e' and then just create a couple of if/else statements so it knows what to do depending on which action(button) the getActionCommand gets (buttons not shown in code).

Code:
public void actionPerformed(ActionEvent e)
                    {
                        String whichButton = e.getActionCommand ();

                        if ( whichButton.equals ("Button1"))
                        {
                        }
                        else
                    }
so it was very similar to what you described,
thx again
Seanneo

12-26-2007, 03:12 PM
#4
CandyMan is offline CandyMan
Status: I'm new around here
Join date: Dec 2007
Location:
Expertise:
Software:
 
Posts: 7
iTrader: 0 / 0%
 

CandyMan is on a distinguished road

  Old

Another way is to write two separate functions and assign them as action listeners to different buttons. This way not only you'll be able to show different text, but some other - different - actions.

Java is a cool thing, it works on tons of different devices and OSes... the second unique thing after C.

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed