• Post Reply Bookmark Topic Watch Topic
  • New Topic

I want distinguish buttons but not after text/caption

 
Andrzej Borucki
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am Delphi/Pascal programmer and Java beginner. I want write scienfific calculator with several modes of work. The same keys without Shift key will cause one operation, with Shift - other operation. If I press the Shift button, the buttons captions changed.
In Delphi VCL i could do with Tags. Keys has Tag 0,1,2,3,4....
I defined several different operation codes:
BTN_INV
BTN_FRACT
BTN_LOG
BTN_10_x
BTN_LN
BTN_e_x

and will configuration arrays
first for normal mode:
BTN_INV
BTN_LOG
BTN_LN
......
second for shift mode:
BTN_FRACT
BTN_10_x
BTN_e_x
......

If I press button, it see its Tag, look array for opcode
But is problem: In JButton i have not tags, but distuinguish after name!
Example:
http://examples.javacodegeeks.com/desktop-java/swing/jbutton/handle-action-events-for-jbutton/
fragment:

Button must have one caption for one mode, socond for another mode.
Several buttons can have the same caption, for example empty caption.

How solve this problem?

regards
 
K. Tsang
Bartender
Posts: 3609
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Not sure exactly what you meant by caption and mode for the buttons. But JButton(String name) should identify each button.

If that's not enough, you can always extends JButton and add your attributes.

 
Andrzej Borucki
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:But JButton(String name) should identify each button.

Thanks,
Parameter in constructor is button name (can be not unique) not button caption (caption = text visible on button)
Button name can be differ than text on button? How change text?
For example I want two buttons:
button Name "0" caption "OK"
button Name "1" caption "Cancel"
How code it ?
 
K. Tsang
Bartender
Posts: 3609
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The string in the JButton(String) constructor is your caption. Now not sure why you also need a name? Can't you use the caption as name?

JButton has a setText() method, which changes the caption in this case.
 
Andrzej Borucki
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:The string in the JButton(String) constructor is your caption. Now not sure why you also need a name? Can't you use the caption as name?
JButton has a setText() method, which changes the caption in this case.


I can't use the caption as name because:
- captions can conains special chars, unicode symbols, a I prefer name as number
- several buttons can have the same captions, but identifiers must be unique
- captions can changed by setText() method but button identifiers must be persistent

Is possible to extend JButton and add attributes but getActionCommand() in actionPerformed(ActionEvent ae) uses captions.
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only if you don't call setActionCommand.
 
Andrzej Borucki
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Only if you don't call setActionCommand.

Solving this problem is MyButton extends JButton where is Tag field setting by constructor and in MyButton is setActionCommand? Where can I place setActionCommand, how code it?
I found example
http://examples.javacodegeeks.com/desktop-java/swing/jbutton/set-action-command-for-jbutton/
In this example, no JButton extension is needed:


This may be the way
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrzej Borucki wrote:I want write scienfific calculator with several modes of work. The same keys without Shift key will cause one operation, with Shift - other operation. If I press the Shift button, the buttons captions changed.
In Delphi VCL i could do with Tags.


JButton has a property called text (which is essentially what you are calling caption) and a property called actionCommand (which is essentially what you are calling tag). You can set these properties to any strings you wish. If you don't explicitly set an actionCommand, it will be taken to be the same as text, but for the rest of this message let's assume that the buttons have different values for text and actionCommand, as if they were created like this:
So if later on the text of aButton is changedthe actionCommand remains the same. When you are handling the ActionEvent, you can call its getActionCommand() method and it will return the "someCommand" for that button regardless of what its text had been set to. So this will distinguish your buttons, presuming you have given them distinct actionCommand strings.

Andrzej Borucki wrote:Solving this problem is MyButton extends JButton where is Tag field setting by constructor and in MyButton is setActionCommand?


There is absolutely no need to subclass JButton to obtain this actionCommand functionality, since it is already there. However you may end up extending it anyway to perform your shift key handling.
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my previous post I tried to answer the question you asked. Allow me to suggest, however, that there may be cleaner ways to implement this than the "opcode" approach you are suggesting.

For example, you could create javax.swing.Action objects for each operation:

Then you could call button43.setAction(tenToTheXAction) or button43.setAction(logBaseTenAction) as you wish, depending on the state of the shift key, and the button's text and behavior will update automatically without having to worry about listeners and such.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!