The Source for Java Technology Collaboration
User: Password:



Laird Nelson's Blog

Laird Nelson Laird Nelson is happiest immersed in server-side frameworks and middleware, where he has spent the better part of nine years obsessed with software reuse.  He has worked for both the Davids and Goliaths of the Boston area as engineer, architect, reuse evangelist, contractor and consultant.  He also plays a mean Hammond B3.



Nimbus and Opacity

Posted by ljnelson on July 31, 2008 at 10:25 AM | Permalink | Comments (5)

This one tripped me up, and I thought I'd post it here. I have a case where I need to pour text into a JTextField, but have that JTextField look like a JLabel. So as I type in one field, I need this "gray" second field to update, live, before the user's eyes. I also need the ability for the user to cut-and-paste values out of it. This sort of thing is reasonably common in properties panels and the like.

Now, normally all you have to do is something like this:

final JTextComponent previewComponent = new JTextField();
previewComponent.setBorder(null);
previewComponent.setEditable(false);
previewComponent.setOpaque(false);
...and you have what looks like a JLabel but what behaves like a JTextField.

Well, except under Nimbus, where the setOpaque(false) seems to have no effect. Turns out you need to also set the background color to a transparent color as well. Fortunately, this combination of behaviors seems to work under all the other look and feels (looks and feels?):

previewComponent.setBorder(null);
previewComponent.setEditable(false);
previewComponent.setOpaque(false);
previewComponent.setBackground(new Color(0, 0, 0, 0));

I hope this helps other Swing folks out.



August 2008
Sun Mon Tue Wed Thu Fri Sat
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            


Search this blog:
  

Categories
J2SE
Patterns
Programming
Swing
Archives

July 2008
December 2007
August 2007
July 2006
June 2005
September 2004

Recent Entries

Nimbus and Opacity

Useful Swing Thing #1

Objects and Strings and the Wrangling Thereof (Part 2)



Powered by
Movable Type 3.01D


 Feed java.net RSS Feeds