Use UIManager in Swing to batch customize the default attributes of a single JComponent component.
Recently, I was studying Swing, and it was so angry with its complexity that I was about to change the background color of JFrame to a white background. I found that it was not as smooth as I thought, and it was totally ineffective to call setBackground, suddenly, we realized that JPanel itself had an opaque background, and things suddenly became complicated, because even in the simplest window layout, containers such as JPanel and JSplitPane were nested, one layer masks one layer, and if you want to change it to a white background, you have to change the code one by one, or use a complex traversal algorithm...
Baidu searched for it and found that a long-standing post mentioned using UIManager to change default values in batches. For example:
UIManager.put("Panel.background",new Color(250,250,250,0));
In just one line, the background color of all jpanels is changed to transparent color. In this way, you can directly set the background color on the frame to penetrate layers. The actual running effect is also very good, it is not affected by the actual set L & F, and the custom component attributes are perfect.
* It was found that this change was too violent, and some components experienced unexpected UI collapse, such as JFileChooser ..
In addition to the background color, of course, there should be various other attributes that can be changed by default in this way, but what are the valid attribute parameters? It is said that the source code and relevant documents should be translated, it is absolutely unacceptable for a lazy person like a pen master!
Therefore, the pen master used the wisdom of milk and finally found a cheating method. The following code lists all the parameters that can be changed:
System.out.println(Arrays.toString(UIManager.getDefaults().entrySet().toArray()));
However, the result is very long and messy, which exceeds the maximum display height of the console. Sort it out:
Object[] list = (Object[]) UIManager.getDefaults().entrySet().toArray();try { FileWriter fw = new FileWriter(new File("c:/UIManagerDefaults.txt")); BufferedWriter bw = new BufferedWriter(fw); for(Object o:list){ bw.write(o.toString()); bw.newLine(); } bw.flush(); bw.close();} catch (IOException e1) { e1.printStackTrace();}
Output file:
AuditoryCues. noAuditoryCues = [Ljava. lang. Object; @ bb494b
InactiveCaptionBorder = javax. swing. plaf. ColorUIResource [r = 244, g = 247, B = 252]
ToolTip. background = com. sun. java. swing. plaf. windows. shorttopproperty @ 6a3960
Tree. focusInputMap = javax. swing. UIDefaults $ LazyInputMap @ 13f136e
Label. disabledForeground = com. sun. java. swing. plaf. windows. Expose topproperty @ 44 cbbe
ScrollBar. trackForeground = com. sun. java. swing. plaf. windows. javastopproperty @ 12bf892
FileChooser. homeFolderIcon = com. sun. java. swing. plaf. windows. WindowsLookAndFeel $ LazyWindowsIcon @ f1916f
TextField. focusInputMap = javax. swing. UIDefaults $ LazyInputMap @ 19762f
Info = javax. swing. plaf. ColorUIResource [r = 255, g = 255, B = 225]
RadioButton. interiorBackground = com. sun. java. swing. plaf. windows. Exclude topproperty @ 141fab6
EditorPane. inactiveBackground = com. sun. java. swing. plaf. windows. Exclude topproperty @ 141fab6
CheckBox. textIconGap = 4
MenuText = javax. swing. plaf. ColorUIResource [r = 0, g = 0, B = 0]
TabbedPane. selectedTabPadInsets = javax. swing. plaf. InsetsUIResource [top = 2, left = 2, bottom = 2, right = 1]
ComboBox. isEnterSelectablePopup = false
... (Too much content, omitted)
The usage is like this:
UIManager.put("ComboBox.isEnterSelectablePopup", true);
How can swing components be customized or customized in Java?
Dashboard, jfreechart seems to have...
I did this by overwriting JComponent. paintComponent (Graphics g). I have an example in my hand, and I will attach it to you.
What is the Canvas component in the swing component equivalent to the awt component?
AbstractAction provides the default implementation of the jfc Action interface.
AbstractButton defines the general behavior of buttons and menu items.
AbstractCellEditor
AbstractListModel the abstract definition of the data model provides a List with content.
This class provides the ChangeListener part of the SpinnerModel interface, which should be applicable to the most specific implementation of the SpinnerModel.
ActionMap provides a ing from an Object (called a key or Action name) to an Action.
BorderFactory provides factory classes for standard Border objects.
Box uses the BoxLayout object as a lightweight container of its layout manager.
Box. Filler is involved in the implementation of lightweight components with no view.
BoxLayout allows layout manager for multiple components either vertically or horizontally.
The ButtonGroup class is used to create a multiple-exclusion scope for a group of buttons.
The CellRendererPane class is inserted between the cell Renderer and the component that uses them.
ComponentInputMap is an InputMap associated with a specific JComponent.
DebugGraphics supports image subclass debugging.
The general implementation of DefaultBoundedRangeModel BoundedRangeModel.
Default Implementation of the data model of the DefaultButtonModel Button component.
The default editor of The ultcelleditor table cells and tree cells.
The default model of the ultcomboboxmodel combo box.
DefaultDesktopManager is the implementation of DesktopManager.
DefaultFocusManager is obsolete and replaced by the 1.4 focus API.
DefaultListCellRenderer shows an item in the list.
DefaultListCellRenderer. UIResource implements the ultlistcellrenderer subclass of UIResource.
DefaultListModel implements java. util. Vector API in a loose way. It implements java. util. Vector of version 1.1.x without the support of the collection class and notifies ListDataListener when a change occurs.
Default data model selected in the DefaultListSelectionModel list.
The general implementation of DefaultSingleSelectionModel SingleSelectionModel.
FocusManager has been deprecated since 1.4 focus API.
GrayFilter is an image filter that "disables" an image by converting the image into a grayscale image and brightening the pixels in the image.
The implementation of an Image Icon interface, which draws an Icon Based on the Image.
InputMap provides binding between input events (currently only KeyStroke is used) and objects.
InputVerifier is used to help clients support smooth focus navigation through a GUI with text fields.
InternalFr ...... remaining full text>