Swing UIManager.getColor() keys

匿名 (未验证) 提交于 2019-12-03 01:33:01

问题:

Is there a list somewhere of the UIManager.getColor() keys for Swing? I can't seem to find it online, just occasional references to strings like "Panel.background" and "Table.selectionBackground".

回答1:

I was looking for the same thing and found this page as well as an excellent overview of all these properties on http://nadeausoftware.com/node/85.



回答2:

I don't think there is a defined standard set of keys. But you could try this bit of code to list the ones currently available in alphabetical order:

List colors = new ArrayList(); for (Map.Entry entry : UIManager.getDefaults().entrySet()) {     if (entry.getValue() instanceof Color) {         colors.add((String) entry.getKey()); // all the keys are strings     } } Collections.sort(colors); for (String name : colors)     System.out.println(name); 

This produces a list too long to reproduce here.



回答3:

@mmyers got me inspired. Here's a short program to list the UIManager defaults in a sortable table.

package com.example.test.gui;  import java.awt.Color; import java.awt.Component; import java.util.Map;  import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.UIManager; import javax.swing.table.DefaultTableCellRenderer;  import ca.odell.glazedlists.BasicEventList; import ca.odell.glazedlists.EventList; import ca.odell.glazedlists.GlazedLists; import ca.odell.glazedlists.SortedList; import ca.odell.glazedlists.gui.AbstractTableComparatorChooser; import ca.odell.glazedlists.gui.TableFormat; import ca.odell.glazedlists.swing.EventTableModel; import ca.odell.glazedlists.swing.TableComparatorChooser;  public class UIManagerDefaultsViewer {      public static class UIEntry     {         final private String key;         final private Object value;          UIEntry(Map.Entry e)         {             this.key = e.getKey().toString();             this.value = e.getValue();          }          public String getKey() {             return key;         }         public Object getValue() {             return value;         }         public Class getValueClass() {             if (value == null)                 return null; // ?!?!?!             return value.getClass();         }         public String getClassName() {             // doesn't handle arrays properly             if (value == null)                 return "";              return value.getClass().getName();         }     }         public static class UIEntryRenderer extends DefaultTableCellRenderer     {         Color[] defaults = new Color[4];         public UIEntryRenderer()         {             super();             defaults[0] = UIManager.getColor("Table.background");             defaults[1] = UIManager.getColor("Table.selectionBackground");             defaults[2] = UIManager.getColor("Table.foreground");             defaults[3] = UIManager.getColor("Table.selectionForeground");         }          public void setDefaultColors(Component cell, boolean isSelected)         {             cell.setBackground(defaults[isSelected ? 1 : 0]);             cell.setForeground(defaults[isSelected ? 3 : 2]);         }          @Override         public Component getTableCellRendererComponent(JTable table, Object value,                 boolean isSelected, boolean hasFocus, int row, int column)         {             Component cell = super.getTableCellRendererComponent(table, value,                     isSelected, hasFocus, row, column);             if (table.convertColumnIndexToModel(column) == 1) // the value column             {                 final EventTableModel tableModel =                      (EventTableModel) table.getModel();                 UIEntry e = tableModel.getElementAt(row);                 JLabel l = (JLabel)cell;                   if (value instanceof Color)                 {                     Color c = (Color)value;                     cell.setBackground(c);                     cell.setForeground(                             c.getRed()+c.getGreen()+c.getBlue() >= 128*3                             ? Color.black : Color.white);                     // choose either black or white depending on brightness                      l.setText(String.format("Color 0x%08x (%d,%d,%d alpha=%d)",                         c.getRGB(), c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha()));                     return cell;                 }                 else if (e.getKey().endsWith("ont"))                     // possible font, not always ".font"                 {                     // fonts are weird, for some reason the value returned                     // in the entry set of UIManager.getDefaults()                     // is not the same type as the value "v" below                                       Object v = UIManager.get(e.getKey());                     if (v instanceof javax.swing.plaf.FontUIResource)                     {                         javax.swing.plaf.FontUIResource font =                              (javax.swing.plaf.FontUIResource)v;                         l.setText("Font "+font.getFontName()+" "+font.getSize());                     }                 }             }              setDefaultColors(cell, isSelected);              return cell;         }     }         public static void main(String[] args) {         final EventList uiEntryList =              GlazedLists.threadSafeList(new BasicEventList());          for (Map.Entry key : UIManager.getDefaults().entrySet())         {             uiEntryList.add(new UIEntry(key));         }          final SortedList sortedUIEntryList = new SortedList(uiEntryList, null);          // build a JTable         String[] propertyNames = new String[] {"key","value","className"};         String[] columnLabels = new String[] {"Key", "Value", "Class"};         TableFormat tf = GlazedLists.tableFormat(UIEntry.class, propertyNames, columnLabels);         EventTableModel etm = new EventTableModel(sortedUIEntryList, tf);          JTable t = new JTable(etm);         TableComparatorChooser tcc = TableComparatorChooser.install(t,                  sortedUIEntryList, AbstractTableComparatorChooser.SINGLE_COLUMN,                 tf);         sortedUIEntryList.setComparator(tcc.getComparatorsForColumn(0).get(0));         // default to sort by the key          t.setDefaultRenderer(Object.class, new UIEntryRenderer());                  JFrame f = new JFrame("UI Manager Defaults Viewer");         // show the frame         f.add(new JScrollPane(t));         f.pack();         f.setLocationRelativeTo(null);         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         f.setVisible(true);          }  } 


回答4:

This program is the best I've seen for visualizing the UIManager values (e.g. Fonts, Colors, Borders): http://tips4java.wordpress.com/2008/10/09/uimanager-defaults/

It lacks search, but it can filter by component or value type, which is pretty great.



回答5:

They are kind of Look and Feel implementation dependent. Look in BasicLookAndFeel.java for the basic keys. Don't expect all PL&F to behave the same, or even remain the same between versions.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!