I want a way to set default settings like, background colour, size etc. for all components I use in my GUI, what's a convenient way to do this? so when I do new JButton or JLabel etc. it will already have the settings applied?
-
1+1 for asking this question :-) Regards – nIcE cOw Jan 08 '12 at 20:13
3 Answers
Also you can create your own look and feel or change default values of the colors using UIManager, like:
UIManager.put("Panel.background", ColorUIResource.YELLOW);

- 15,354
- 2
- 27
- 29
-
2A good way to figure out what properties are used by your L&F is to call UIManager.getDefaults() and print out all the key-value pairs. – Russell Zahniser Jan 08 '12 at 20:08
-
Write a factory method that returns your components with the defaults already applied, then retrieve them from the factory instead of directly creating them.
Or write a method that accepts an instance of a JComponent and sets the desired defaults, that can be reused throughout your code.
Or use a combination of both.
Another option is to create and use your own "Look and Feel":
- http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- http://java.sun.com/products/jfc/tsc/articles/sce/index.html
... but I would guess that this is much more involved than what you're looking for.

- 27,712
- 8
- 86
- 94
It is called a Look And Feel, and you can write your own and set it as the default or adjust the defaults of an existing one. For changing only a few settings like background color I would simply modify some defaults
Setting a default size seems a weird requirement. For example for a JLabel
the preferred size (which matches better with the Swing API then default size) would depend on the contents of the JLabel
. How would you define a 'default size' for all components ?

- 36,233
- 5
- 47
- 99