概要

JColorChooserのサンプル(Swatches)タブに配置されている最新(Recent)カラーパレットを保存、復元可能になるよう設定します。

サンプルコード

RecentSwatchPanel switchPanel = new RecentSwatchPanel();
switchPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
switchPanel.colors[0] = Color.RED;
switchPanel.colors[1] = Color.GREEN;
switchPanel.colors[2] = Color.BLUE;

JButton button = new JButton("open JColorChooser");
button.addActionListener(e -> {
  JColorChooser cc = new JColorChooser();
  AbstractColorChooserPanel[] panels = cc.getChooserPanels();
  List<AbstractColorChooserPanel> choosers =
      new ArrayList<>(Arrays.asList(panels));
  choosers.remove(0);
  MySwatchChooserPanel swatch = new MySwatchChooserPanel();
  swatch.addPropertyChangeListener("ancestor", event -> {
    Color[] colors = swatch.recentSwatchPanel.colors;
    if (event.getNewValue() == null) {
      System.arraycopy(
          colors, 0, switchPanel.colors, 0, colors.length);
    } else {
      System.arraycopy(
          switchPanel.colors, 0, colors, 0, colors.length);
    }
  });
  choosers.add(0, swatch);
  cc.setChooserPanels(choosers.toArray(new AbstractColorChooserPanel[0]));

  ColorTracker ok = new ColorTracker(cc);
  Component parent = getRootPane();
  String title = "JColorChooser";
  JDialog dialog = JColorChooser.createDialog(
      parent, title, true, cc, ok, null);
  dialog.addComponentListener(new ComponentAdapter() {
    @Override public void componentHidden(ComponentEvent e) {
      ((Window) e.getComponent()).dispose();
    }
  });
  dialog.setVisible(true);
  switchPanel.repaint();
  Color color = ok.getColor();
  if (color != null) {
    label.setBackground(color);
  }
});
View in GitHub: Java, Kotlin

解説

  • JColorChooserをシリアライズしてもサンプル(Swatches)タブで使用される最新(Recent)カラーパレットは保存、復元不可(未対応?)
  • また、サンプル(Swatches)タブで使用されるDefaultSwatchChooserPanelはパッケージプライベートなので、これをコピーしてMySwatchChooserPanelを作成
    • 最新(Recent)カラーパレット用のRecentSwatchPanelもパッケージプライベートなのでこれをパブリックに変更し、外部から操作可能に変更
  • JColorChooser#setChooserPanels(...)MySwatchChooserPanelを使用するサンプル(Swatches)パネルを差し替え
  • サンプル(Swatches)パネルにPropertyChangeListenerを追加:
    • 親パネルへの追加イベントで最新(Recent)カラーパレットへの色配列復元を実行
    • 親パネルからの削除イベントで最新(Recent)カラーパレットからの色配列取得を実行

参考リンク

コメント