aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Adding ActionListener to 9 JButtons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding ActionListener to 9 JButtons" Watch "Adding ActionListener to 9 JButtons" New topic
Author

Adding ActionListener to 9 JButtons

Alessandro manocchio
Greenhorn

Joined: Mar 07, 2014
Posts: 5
can someone help me i need to add actionlistener to my code which allow the number 1 to 9 to function which are located in the nested for loop.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
Welcome to JavaRanch. What is the problem you're facing? The code seems to add ActionListeners to those buttons already.


Ping & DNS - my free Android networking tools app
Alessandro manocchio
Greenhorn

Joined: Mar 07, 2014
Posts: 5
yes there is a code ready set-up but no method as i dont know what to code could please provide me we some sort of solution to put in the code.

the buttons in the loop are numbers 1 to 9 which are suppose to once clicked to show that particular number inn the JTextField.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Have you tried a loop?And surely you want ten numbers, 0…9?
Alessandro manocchio
Greenhorn

Joined: Mar 07, 2014
Posts: 5
i have this code for my loop ;



so Campbell Ritchie could you give be a code which will work for this loop.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 690
    
    1
Also posted at: http://www.javaprogrammingforums.com/whats-wrong-my-code/36243-help-actionlistener-my-calculator%3B-button-created-within-loop-number-1-to9.html#post141532
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 688
    
  11
The code that goes between these actionListener braces depends on what you want to happen.
For a start, you would want to display the digit into your display.
Is your display panel p1?
You could then, for instance, put in te code:

But you see that you run into trouble here.
First of all, you do not have a dedicated textfield for your display.
Secondly, the pressed digit button might not be the first digit button pressed,
for instance, say you want to input the number '89'. Then first the '8' is pressed,
and then the '9' is pressed. So, in fact you would have to add the '9' to what's already there.

As it is now, you must give thought to what you want to do with your program.
What would you want to do when the user presses the '=' button? Then some
expression that the user has input must be evaluated.

But for now, I would concentrate on getting these digits on your display panel.

Greetz,
Piet
Alessandro manocchio
Greenhorn

Joined: Mar 07, 2014
Posts: 5
the code that you gave comes up with errors and i can't seem to fix them
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 688
    
  11
oops, sould have been:
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2215
    
    7
Some basic code to get you started. It will work if you click on a button or enter the number from the keyboard:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
The code PS gave was only an example; it was not intended to fit into your work.
Alessandro manocchio
Greenhorn

Joined: Mar 07, 2014
Posts: 5
it still get errors withe code



it says that for the first bit of code that it cant find the symbol and cant find symbol as well.
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 688
    
  11
hmm, indeed. As Campbell said, it was just to give a hint of what kind of code
would go between the braces.

One of the problems is that I see no dedicated display in your code.
I see a panel called 'p1', to which you add a textfield. Is that
textfield intended to be used as a display? That's what I asked.

Suppose it is, then as you can see it would be handy if you have
a dedicated variable (a 'member'). So, for instance,
in line 6 and a half:

and add that field to your p1 panel.
Then, in your actionlisteners, you could directly reference this display.

But also give a look to Rob's code, although I think it is a bit advanced
at this stage.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding ActionListener to 9 JButtons