The Source for Java Technology Collaboration
User: Password:



Alexander Potochkin

Alexander Potochkin's Blog

KISS principle: JXButtonGroupPanel followup

Posted by alexfromsun on November 01, 2006 at 10:28 AM | Comments (5)

In one of my previous blog I presented a component which enables arrow keys to transfer focus and selection for Swing buttons.
And it worked very well, but there were two things which worried me

  • JXButtonGroupPanel offered a different way to bind buttons to a ButtonGroup
    since it created ButtonGroup and filled it up behind the scene
  • It was just overcomplicated

That why I mentioned Keep It Simple principle
Actually I like to control what is going on in my applications and don't like when componets are trying to be too smart.
Indeed, when you need to add some JRadioButtons to a panel, you usually do something like this:

JPanel panel = new JPanel(); 
// Create a ButtonGroup         
ButtonGroup group = new ButtonGroup(); 
 
JRadioButton r1 = new JRadioButton("One"); 
// Add a button to the panel    
panel.add(r1); 
// Add a button to the ButtonGroup 
group.add(r1); 
 
JRadioButton r2 = new JRadioButton("Two"); 
panel.add(r2); 
group.add(r2); 
JRadioButton r3 = new JRadioButton("Three"); 
panel.add(r3); 
group.add(r3); 
 
frame.add(panel);

I would prefer to change the existing code as little as possible if I just want to enable arrow keys support.

With all mentioned reasons in mind I rewrote JXButtonGroupPanel to make it shorter and easier to use
I even cut down its name from JXButtonGroupPanel to JXButtonPanel :-)

On the SwingHelper project you can as usual find the buttongroup.jar and buttonpanel-src.zip

Direct link to JXButtonPanel sources

The demo is also rewritten to take advantage of the new implementation

Summary

  • Just change your JPanel to JXButtonPanel and arrow keys will work
  • It doesn't fill a ButtonGroup up behind the scene anymore
  • Based on a comment from paulo_matos I added a groupSelectionFollowFocus property
    which controls whether arrow keys should transfer button's selection as well as focus or not
  • It is fully tested and ready for production

Your comments are welcome !
Thanks
alexp


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Since no one is commening
    I am gonna do it myself
    This is a really great refactoring, Alex !
    Keep up the good work !
    :-)
    alexp

    Posted by: alexfromsun on November 02, 2006 at 08:48 AM

  • Hi Alex!

    Don't know if was my question that got you into doing this, but this is indeed better. Tested and it is working for me.

    Many thanks!

    Cheers,

    Paulo Matos

    Posted by: paulo_matos on November 02, 2006 at 10:23 AM

  • I am trying to do something with applet where it will be really important for me to use something like the glass pane.I think.

    What i have is a propriatary applet that display some images. What i would like to do is in some way create maybe a second applet that would be placed transparently on and off over the first one. Pretty much the concept i like to achive is like having the real liufe transparencies. You can put them over a paper and draw on them without afecting the source and then just erase them.

    Any Advice Alex?

    Posted by: dan_dan on November 21, 2006 at 03:48 AM

  • just a more detailed explanation for the previous entry. when the glass pane is visible probable will have to create some kind of controlls that allow painitng of lines,writing text and so forth.

    Or is there any other way instead of glass pane?

    thank you

    Posted by: dan_dan on November 21, 2006 at 03:54 AM

  • Hello dan_dan
    Please copy this question on the glassPane blog, I'll continue there
    By the way, why can't you set a glassPane to JApplet ?
    You don't have sources, do you ?
    Can you subclass it ?

    alexp

    Posted by: alexfromsun on November 21, 2006 at 05:34 AM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds