概要

JFrameがデフォルトのウィンドウ装飾を使用する場合、タイトルバーの文字色、背景色などを変更します。

サンプルコード

Object[][] data = {
  {"activeCaption",         UIManager.getColor("activeCaption")},
  {"activeCaptionBorder",   UIManager.getColor("activeCaptionBorder")},
  {"activeCaptionText",     UIManager.getColor("activeCaptionText")},
  {"control",               UIManager.getColor("control")},
  {"controlDkShadow",       UIManager.getColor("controlDkShadow")},
  {"controlHighlight",      UIManager.getColor("controlHighlight")},
  {"controlLtHighlight",    UIManager.getColor("controlLtHighlight")},
  {"controlShadow",         UIManager.getColor("controlShadow")},
  {"controlText",           UIManager.getColor("controlText")},
  {"desktop",               UIManager.getColor("desktop")},
  {"inactiveCaption",       UIManager.getColor("inactiveCaption")},
  {"inactiveCaptionBorder", UIManager.getColor("inactiveCaptionBorder")},
  {"inactiveCaptionText",   UIManager.getColor("inactiveCaptionText")},
  {"info",                  UIManager.getColor("info")},
  {"infoText",              UIManager.getColor("infoText")},
  {"menu",                  UIManager.getColor("menu")},
  {"menuText",              UIManager.getColor("menuText")},
  {"scrollbar",             UIManager.getColor("scrollbar")},
  {"text",                  UIManager.getColor("text")},
  {"textHighlight",         UIManager.getColor("textHighlight")},
  {"textHighlightText",     UIManager.getColor("textHighlightText")},
  {"textInactiveText",      UIManager.getColor("textInactiveText")},
  {"textText",              UIManager.getColor("textText")},
  {"window",                UIManager.getColor("window")},
  {"windowBorder",          UIManager.getColor("windowBorder")},
  {"windowText",            UIManager.getColor("windowText")}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames) {
  @Override public boolean isCellEditable(int row, int column) {
    return column == 1;
  }

  @Override public Class<?> getColumnClass(int column) {
    return getValueAt(0, column).getClass();
  }
};
JTable table = new JTable(model);

// ...
model.addTableModelListener(new TableModelListener() {
  @Override public void tableChanged(TableModelEvent e) {
    if (e.getType() == TableModelEvent.UPDATE && e.getColumn() == 1) {
      int row = e.getFirstRow();
      String key = (String) model.getValueAt(row, 0);
      Color color = (Color) model.getValueAt(row, 1);
      UIManager.put(key, new ColorUIResource(color));
      EventQueue.invokeLater(new Runnable() {
        @Override public void run() {
          Window w = SwingUtilities.getWindowAncestor(table);
          SwingUtilities.updateComponentTreeUI(w);
        }
      });
    }
  }
});
View in GitHub: Java, Kotlin

解説

上記のサンプルでは、JFrameがデフォルトのウィンドウ装飾を使用するようにJFrame.setDefaultLookAndFeelDecorated(true)を指定し、UIManager.setColor(...)メソッドを使用してタイトルバーの文字色、背景色を変更するテストを行っています。

  • activeCaption: タイトルバーの背景色
  • activeCaptionBorder: タイトルバーのBorderの色
  • activeCaptionText: タイトルバーの文字色

参考リンク

コメント