How to set all colors of Java Swing GUI components and foreground colors (fonts) at once?

I have tons of jbuttons, jtextfields, jlabels, jmenus, gui, and a lot of time to set the background color and foreground color one at a time.

I want colored fonts (foreground) and backgrounds all jmenus, jmenuitems, jtextfields, jbuttons, etc. performed quickly / briefly in my project, instead of installing them one at a time.

Is there any method to do this more concisely instead of doing it at a time?

+4
source share
4 answers

1) the most effective way would be to use Custom Look and Feel , some of them got nice themes

2) set the value to UIDefault , UIDefault Property List

EDIT:

best UIManager Defaults by @camickr

+6
source

You can combine Swing with CSS or use Swing Look and Feel to create a standard look for your components. Java site says:

Before moving on to implementing CSS, consider an alternative: a custom look. Swing Look and Feels (L & Fs) are sets of classes that implement the actual drawing of components at a very low level (think lines and bitmaps). They can be replaced with new ones at runtime, often to implement the look of their own platform; that is, the JDK for OSX has a set of classes that make Swing applications look like regular Aqua applications, using the candy buttons and the blue tint. Custom L & Fs are powerful, but not trivial or fast to create. Usually you need to touch 20 classes and implement a whole bunch of special drawing code.

So CSS is easier to use. The same article provides a tutorial on how to implement CSS with Swing. They provide a good step-by-step guide on creating the correct rules, and then continue to implement them in CSS. However, this is not just copy and paste code.

If you just want to use the package (without having to code it yourself), the answers to the question Can I use CSS for Java Swing? I suggest Flying Saucer and Jaxx .

+4
source

They are all JComponents , so you can make an ArrayList everything:

 //Adding everything to the ArrayList ArrayList<JComponent> myComponents = new ArrayList<JComponents>(); JButton b1 = new JButton("Button 1"); myComponents.add(b1); JMenuItem item = new JMenuItem("Menu Item 1"); myComponents.add(item); //Coloring the foreground/background for(JComponent j : myComponents) { j.setForeground(new Color("BLUE")); j.setBackground(new Color("RED")); } 
+4
source

If you use Look and Feel, which honors the user interface constants in javax.swing.UIManager , you can simply set them. There are values, for example. background panels. If not, or if you cannot control this look, you can write your own user interface delegate that draws a specific component (for example, javax.swing.plaf.ButtonUI for JButtons). If even this is not enough, you can write your own Look And Feel. If you simply extend Metal LnF, it is not so difficult, you should write your own user interface delegates and set the properties as described above, but centralized.

+3
source

Source: https://habr.com/ru/post/1396467/


All Articles