2

I am making a UI with Swing, and I want the buttons I am using for my custom dialogs to have the same style as the ones in standard dialogs.

For instance, in the attached image I have a custom dialog and the standard file select dialog. I want the 'OK' and 'Cancel' buttons from the file select dialog to be used for the equivalent buttons in my custom dialog.

I want my application to use the default system look and feel of whatever OS it is running on, so I don't want to try to manually re-create these standard buttons. Using a more rigid Swing class that automatically provides these buttons wouldn't work either, as I'd like to also use them in other, more exotic places in my UI.

Is there an easy solution to this problem?

example image

Oliver
  • 11,297
  • 18
  • 71
  • 121

2 Answers2

1

Sorry to be the bearer of bad news, but it look like there is no standard way to do this cross-platform. The behaviours, mnemonics and icons on default buttons are handled in very specific ways by each look-and-feel.

Here is a SO question that answers the question on how to set the default OK and Cancel buttons on a dialog (the default button is set using getRootPane().setDefaultButton(...) and the Cancel button needs a custom keyboard listener. If you're very lucky, setting the default button might add an icon to it, depending on how the UI is coded.

This forum thread addresses the issue of getting icon resources from the UIManager. Each LaF has a set of UI defaults such as colors, text, borders and icons. There are a number of default icons which are found across all LaFs, but for non-standard icons, such as ones on buttons, there are no guarantees. However, if you tell me which LaF you are using in the screenshot you provided, I can look up the resource keys used by its custom UI classes (or you can find it yourself if you have the source). You could then write a helper method which looks for the icons via these keys, and adds them to the buttons if they are found.

Community
  • 1
  • 1
BoffinBrain
  • 6,337
  • 6
  • 33
  • 59
  • Thanks for the info. In the screenshot I am using the system LaF for Ubuntu, but the hope with this question was that I'd be able to have the program use the system LaF of whatever platform it was running on, and appear fairly native on each of them. As there's no easy way to do this, I'll try manually tweaking things so that it looks ok on the various platforms, or perhaps just pick one LaF and use it on all platforms... – Oliver Mar 17 '11 at 02:04
  • The best you can really do is to use UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()) which will use the most appropriate LaF on the current OS. Even then, the exact level of 'nativeness' depends on the implementation, so it will never be perfect. Good luck! – BoffinBrain Mar 17 '11 at 10:43
0

JButton.setUI(ButtonUI) sets the UI for just one JButton. Use that in conjunction with a factory:

public static JButton createStyledButton(String text) {
   JButton button = new JButton(text);
   button.setUI(STYLE_UI);
   return button;
}    
Umesh K
  • 13,436
  • 25
  • 87
  • 129
  • 1
    that doesn't answer his question. The difference between the Cancel and OK button in the standard jfilechooser widget and his custom dialog is that there are icons and mmnemonics applied. – Tedil Mar 13 '11 at 17:58
  • I've already done that - in the image shown, the system look and feel is being used. What I am interested in is getting some of the standard designs of buttons used for that system look and feel to use in other places in my application, rather than trying to re-construct them by using JButtons with text + images. – Oliver Mar 13 '11 at 17:58
  • @Oliver I have edited answer with customize style of button ui hope it will help to solve your requirement. – Umesh K Mar 13 '11 at 18:29