3

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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
fenerlitk
  • 5,414
  • 9
  • 29
  • 39

3 Answers3

5

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);
4ndrew
  • 15,354
  • 2
  • 27
  • 29
3

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":

... but I would guess that this is much more involved than what you're looking for.

ziesemer
  • 27,712
  • 8
  • 86
  • 94
2

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 ?

Robin
  • 36,233
  • 5
  • 47
  • 99