Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

UI's and Look-And-Feels  RSS feed

 
Mark Newton
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm kind of hoping that this is a stupid question, and I'm just missing something glaringly obvious but...

Basically, I want a combobox where the editor is one size, and the popup is larger, so I can have a nice, neat combo box when it's closed, but with plenty of room to see the options when it opens. (It's to display fonts, a la Word).

Anyway, looking around here, I found this example:

http://www.java2s.com/Code/Java/Swing-Components/SteppedComboBoxExample.htm

Which does exactly what I want... in the metal look and feel. Basically, it extends MetalComboBoxUI, and over-rides the method that shows the popup, to set its size differently. That's fine, except I want to use multiple look-and-feels, automatically selecting the l&f appropriate to the user's platform. This approach gives me (on Windows) a metal-looking editor, and arrow button, but (perhaps strangely?) a popup with a Windows-looking scrollbar.

So, I can see that I'm getting a metal l&f because I'm extending the metal UI, does that mean that I need to find all the l&f's I might want, and extend each one in the same way? Hopefully not.

That feels a bit messy, and very much as though there should be a better way. Also, if that is the only way, I'm not sure how to go about it, I'm far from a look and feel expert (which is probably apparent...), although I've been reading around a bit now - but I can't actually find the classes for other look and feels - there's javax.swing.plaf.metal, obviously, so I was expecting to find a .plaf.windows.

I realise that I'm effectively asking for a crash course in L&F's here, and that there are plenty available online. Really, I'm hoping my assumption about having to extend all the different L&Fs is wrong, and someone will point out my stupid mistake... Otherwise I'll get reading (or persuade the customer that he doesn't want it...).

Cheers,
Dave Payne.
 
sammaiah kyatham
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); will resolve your problem if you are trying to run in Windows system.

Thnx,
Sam
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Payne:
Basically, it extends MetalComboBoxUI, and over-rides the method that shows the popup, to set its size differently. That's fine, except I want to use multiple look-and-feels, automatically selecting the l&f appropriate to the user's platform. This approach gives me (on Windows) a metal-looking editor, and arrow button, but (perhaps strangely?) a popup with a Windows-looking scrollbar.

So, I can see that I'm getting a metal l&f because I'm extending the metal UI, does that mean that I need to find all the l&f's I might want, and extend each one in the same way? Hopefully not.

That feels a bit messy, and very much as though there should be a better way.


Yeah, this is the common problem when overriding the component UIs. Sometimes it's even worse because Metal's theme setting (steel/ocean) causes trouble when overriding Metal UI classes.

I'm having the same problem as you with something I'm working on now because I'm overriding MetalSliderUI. There's no way to assign the custom UI to the component and still look mac-ish on macs, windows-ish on windows, and plastic-ish on plastic. Theoretically it would be possible to to override every known LnF's SliderUI subclass, but often each UI class is implemented differently so it's a heck of a lot of work. Sometimes it's flat out impossible because of private members in some of them.

There's no way out of this for me (however Metal looks mostly ok) but for what you are trying to do with the width of the JComboBox popup there is a way to do it without having to set a custom UI. Unfortunately, I don't have it handy, and probably won't until next week.
 
Mark Newton
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brian Cole:
for what you are trying to do with the width of the JComboBox popup there is a way to do it without having to set a custom UI. Unfortunately, I don't have it handy, and probably won't until next week.


Excellent, thanks very much Brian! It's kind of good to know that I'm not being completely thick, although it would have been nice if there was a simple solution.

I'm not really in any great hurry to get this implemented, so if you could get your alternative to me some time next week, I'd be very grateful.

Thanks again.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this out:
http://www.jroller.com/santhosh/entry/make_jcombobox_popup_wide_enough
Works without having to mess up with the componentUI.
 
Mark Newton
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome! Thanks very much Maneesh - does exactly what I needed
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!