Friday, November 5, 2010

Friday Fun Component III

Man that was a busy week...due to the lack of time this week's fun component will be also a little bit smaller...
Which doesn't mean that it wasn't fun...

Every morning when i switch on my magic mouse i see this little switch on the back of the magic mouse. The button is small, it shows it's state in a clean understandable way and it doesn't need some led to visualize it, just a simple green colored background if the mouse is switched on...perfect.


For those of you that don't know what i'm talking about...here it is...




I liked it so much that i decided to create a little swing component that looks like that (or similar to that)...

And here we go...



CHECKBOX.setRised(false);     (standard)

CHECKBOX.setRised(true);

So the rised property only changes the look of the colored area so that it looked "sunken" as the default and "rised" if you set rised to true;

You will find all the colors i defined in the SteelSeries lib also here in the component but if you like you could add your own colors by editing the values in the eu.hansolo.tools.ColorDef class.


Here is a little screenshot of all the available colors in "sunk" and "rise" state:



These colors are only visible if you set the colored property to true, otherwise the selected state will always look like the gray version with rised == true.


So if you like it, please find the source and the binary here:


    Source: SteelCheckBox_Source.zip


    Binary: SteelCheckBox.jar


That's it for today...enjoy the upcoming weekend...



17 comments:

  1. The more simple things are often useful too.

    ReplyDelete
  2. Only one word... "BEAUTIFUL"
    Thanks. How can i stretch it to my required size?? In netbeans i'm able to do it only for text of the SteelCheckBox.

    ReplyDelete
    Replies
    1. The knob itself is not resizeable but if you need this functionality you should take a look into SteelCheckBoxUI.java where you will find the methods that create the images. So you should be able to modifiy the size related methods in the way that they will also work for the button etc.
      Cheers,
      Gerrit

      Delete
  3. the link for download is not working now....can anyone mail the src at nikunj91@gmail.com

    ReplyDelete
  4. Hi there,

    Sorry for this, but I had to move from iDisk to DropBox which means many links doesn't work anymore, please let me know if you encounter other broken links and I will fix them.
    This one here should now work again...
    Cheers,
    Gerrit

    ReplyDelete
  5. Hi Han,
    Congrats for this component, and thanks for sharing it.
    Just wanted to point out that my first test got into an Exception I traced back: your component requires a (non null, non empty) text to be set. If you don't call setText() with a non-empty String before displaying the component, you're getting an IllegalArgumentException: "Zero length string passed to TextLayout constructor." (see exact stack trace below).

    I fixed the source by enclosing the lines SteelCheckBoxUI:152-157 in a "if (CHECKBOX.getText() != null && CHECKBOX.getText().length() > 0) {...}" test.

    Maybe you can change it in the distributed code. Otherwise maybe it can help others.

    Best regards (and thanks again)

    Vicne

    Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Zero length string passed to TextLayout constructor.
    at java.awt.font.TextLayout.(TextLayout.java:382)
    at eu.hansolo.custom.SteelCheckBoxUI.paint(SteelCheckBoxUI.java:155)
    at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
    at javax.swing.JComponent.paintComponent(JComponent.java:778)

    ReplyDelete
    Replies
    1. By the way, what are the licensing terms for your code. I would like to use that component in an Open-Source project and even modify it slightly (make the switch optionally vertical). Is that OK ?

      Delete
    2. Hi Vicne,
      Thanx for the hint, will fix the code which is btw under BSD so feel free to use it for whatever you like.
      Cheers,
      Gerrit

      Delete
    3. Great, thanks :-)

      Btw, I just noticed an issue if the layout manager does not honor the preferred size of the component: the text and foreground are centered based on the actual size of the component while the background and knob are always at the top left of the provided space. See this screenshot where it's in a GridLayout next to a 100x100 button: http://screencast.com/t/leiSxqk0

      Probably recomputing elements upon resize would be the cleanest solution. I'm having a look right now, then I'll tackle the orientation question. I'll let you know if I succeed ;-).

      Best regards,

      Vicne

      Delete
    4. OK. I guess that was due to a typo: at SteelCheckBoxUI:81, getPreferredSize() should really be getSize().

      Vicne

      Delete
    5. Me again :-).
      I added the "vertical" feature via a new Constructor (with a SwingConstants.VERTICAL parameter). Here is the code if you're interested:
      https://www.yousendit.com/download/WUJaZGluTkFqY29QWWNUQw
      Feel free to delete this post when you have downloaded the file. The yousendit link will expire soon...
      Best regards,
      Vicne

      Delete
    6. hi vince,

      i am having the same problem. can u send the link for how did you get that layout manager problem solved.

      Best Regards,
      Sanket

      Delete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Thank U!!! this button is wonderful

    I love it!

    ReplyDelete
  8. Can you update links ? Cannot download , file not found

    ReplyDelete