What's a convenient way to set default settings for all JComponents

291 views Asked by At

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?

3

There are 3 answers

0
ziesemer On BEST ANSWER

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.

2
4ndrew On

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);
0
Robin On

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 ?