|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
Laird Nelson's Blog
Nimbus and OpacityPosted 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: ...and you have what looks like a JLabel but what behaves like a JTextField.final JTextComponent previewComponent = new JTextField(); previewComponent.setBorder(null); previewComponent.setEditable(false); previewComponent.setOpaque(false); 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
Search this blog:CategoriesJ2SEPatterns Programming Swing Archives
July 2008 Recent EntriesObjects and Strings and the Wrangling Thereof (Part 2) | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|