• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Button state tracking

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Before I ask my question, I'll first explain the situation: I have an extension of a JButton called ImagedButton. This is a specific button for use with my own GUI. On top of this button an icon can be placed. So the button has four states (images): up, down, over en disabled. And on top of those images an icon is painted.
To simulate the pushing effect I want to move the icon 1px to the right and 1px to the bottom. The way I try to do it is as follows: I believe that with every state change the repaint method is called to paint the new state image. So there has to be some kind of state tracking. My overridden paint method calls super.paint(g); and should do the following:
if(state == STATE_CLICKED)
{
iconX++;
iconY++;
}
How can I access the state of a button, where I'm able to do the above construction.
Thanks in advance and cheers,
Jeroen
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is an easier way. Create 2 Images. 1 as your unpressed state that will just be added to the JButton as an ImageIcon. the 2nd image should appear as you would like it to look when it is pressed. JButton has a method, setPressedIcon(ImageIcon img). Just use that method to set your 2nd image and when you click on the button, the second image will appear. Don't forget to setBorderPainted(false) and setFocusPainted(false) on your JButton. This will really make it look nice after you have applied your images.
Here is a good link for creating buttons in PSP.
Here is a link to a sample applet I made using the above mentioned. When it prompts you for a username and password, just put anyting in and hit enter. There is no verification right now. The first 3 buttons are togglebuttons, but the 4th button is a regular JButton and has the effect you are wanting.
[ November 12, 2002: Message edited by: Gregg Bolinger ]
 
J.H.B. Oosterlaar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that. I'm also using that for every state. But on top of the button I draw an icon (on top of those set images). I want that icon to be moved 1px.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, ok. Well, I still think you are going at it the hard way. Why can't you create that icon in PSP already shifted? Make your canvas area in Paint the same, just draw the image up 1 px.??
 
J.H.B. Oosterlaar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand what your opinion is, but that is not what I'm asking (no offence ). My question looks quite clear to me and I have my reasons for doing it that way. One of the most important ones is that it much more dynamical: I only need four images. For every specific button I have a small icon image to be put on top of the image. So could you please tell me how I do this?
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got this to work by adding a MouseListener that shifted my Icon internally... for some reason it looks like there is no way to see the "state" a button is in... but anyway, here's the code...

 
J.H.B. Oosterlaar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is at least something that works. I couldn't find the state either, which is very odd, since the paint method of the given state icon should do some checking on the state the button is in, in order to paint the right icon.
Anyway, thanks for your reply. I will use your idea.
 
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what's with the ButtonModel?
The API of ButtonModel says:


State Model for buttons. This model is used for check boxes and radio buttons, which are special kinds of buttons, as well as for normal buttons. For check boxes and radio buttons, pressing the mouse selects the button. For normal buttons, pressing the mouse "arms" the button. Releasing the mouse over the button then initiates a button press, firing its action event. Releasing the mouse elsewhere disarms the button.
In use, a UI will invoke setSelected(boolean) when a mouse click occurs over a check box or radio button. It will invoke setArmed(boolean) when the mouse is pressed over a regular button and invoke setPressed(boolean) when the mouse is released. If the mouse travels outside the button in the meantime, setArmed(false) will tell the button not to fire when it sees setPressed. (If the mouse travels back in, the button will be rearmed.)


There is a property MODEL_CHANGED_PROPERTY (see source code of AbstractButton).
Chantal
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dang! I forgot about that!!!
 
If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic