This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding ActionListener to 9 JButtons

 
Alessandro manocchio
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch. What is the problem you're facing? The code seems to add ActionListeners to those buttons already.
 
Alessandro manocchio
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried a loop?And surely you want ten numbers, 0…9?
 
Alessandro manocchio
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have this code for my loop ;



so Campbell Ritchie could you give be a code which will work for this loop.
 
Piet Souris
Ranch Hand
Posts: 1098
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code that you gave comes up with errors and i can't seem to fix them
 
Piet Souris
Ranch Hand
Posts: 1098
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops, sould have been:
 
Rob Camick
Ranch Hand
Posts: 2473
8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code PS gave was only an example; it was not intended to fit into your work.
 
Alessandro manocchio
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1098
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic