JColorChooser:在“色板”面板中保存/恢复最近的颜色 [英] JColorChooser: Save/restore recent colors in Swatches panel

查看:187
本文介绍了JColorChooser:在“色板”面板中保存/恢复最近的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序的不同位置使用 JColorchooser 。可以有多个面板可以调用JColorChooser。

选择器中的色板面板有一个最近颜色的区域,它只存在于每个JColorChooser的实例。我想在我的应用程序中(a)在我的所有选择器中使用相同的最近颜色,并且(b)将颜色保存到磁盘以便这些颜色生存关闭并重新启动应用程序。

(至少(a)可以通过在整个应用程序中使用相同的单个选择器实例来解决,但这很麻烦,因为我需要非常小心附加的changelisteners,以及在各种对话框中添加/删除选择器面板。)

I am using a JColorchooser at various places in an application. There can be multiple instances of the panel that can invoke a JColorChooser.
The "Swatches" panel in the chooser has an area of "recent" colors, which only persists within each instance of JColorChooser. I would like to (a) have the same "recent" colors in all my choosers in my application, and (b) to save the colors to disk so that these colors survive close and restart of the application.
(At least (a) could be solved by using the same single chooser instance all over the whole app, but that apears cumbersome because I would need to be very careful with attached changelisteners, and adding/removing the chooser panel to/from various dialogs.)

我没有找到任何让我设置(恢复)这些最近颜色的方法在选择器面板中。所以对我而言,似乎实现这一目标的唯一方法是:

I did not find any method that lets me set (restore) these "recent" colors in the chooser panel. So to me, it appears that the only ways of achieving this would be:


  • 序列化并保存/恢复整个选择器(选择器面板? )

  • 从头开始创建我自己的选择器面板

这是否正确或者我错过了什么?

Is this correct, or am I missing something?

BTW:我还想检测选择器中的双击,但似乎很难找到合适的位置来连接鼠标监听器。我真的需要深入了解选择器面板的内部结构吗? (不,它不能检测同一颜色的第二次点击,因为更改侦听器仅在单击其他颜色时才会触发。)

BTW: I would also like to detect a double click in the chooser, but it seems hard to find the right place to attach my mouse listener to. Do I really need to dig into the internal structure of the chooser panel to do this? (No, it does not work to detect a second click on the same color, because the change listener only fires if a different color is clicked.)

推荐答案

正如您所注意到的,没有公共API可以访问DefaultSwatchChooserPanel中的最近颜色,即使面板本身也无法访问。

As you noticed, there is no public api to access the recent colors in the DefaultSwatchChooserPanel, even the panel itself isn't accessible.

当你将需要一些逻辑/ bean来保存和重置最近的颜色(加上扩展的鼠标交互),滚动你自己的是要走的路。有关指导,请查看样本面板的实现(咳嗽...... c& p你需要什么,并修改你不需要的)。基本上,类似

As you'll need some logic/bean which holds and resets the recent colors anyway (plus the extended mouse interaction), rolling your own is the way to go. For some guidance, have a look at the implementation of the swatch panel (cough ... c&p what you need and modify what you don't). Basically, something like

// a bean that keeps track of the colors
public static class ColorTracker extends AbstractBean {

    private List<Color> colors = new ArrayList<>();

    public void addColor(Color color) {
        List<Color> old = getColors();
        colors.add(0, color);
        firePropertyChange("colors", old, getColors());
    }

    public void setColors(List<Color> colors) {
        List<Color> old = getColors();
        this.colors = new ArrayList<>(colors);
        firePropertyChange("colors", old, getColors());
    }

    public List<Color> getColors() {
        return new ArrayList<>(colors);
    }
}

// a custom SwatchChooserPanel which takes and listens to the tracker changes
public class MySwatchChooserPanel ... {

   ColorTracker tracker;

   public void setColorTracker(....) {
       // uninstall old tracker 
       ....
       // install new tracker
       this.tracker = tracker;
       if (tracker != null) 
           tracker.addPropertyChangeListener(.... );
       updateRecentSwatchPanel()
   }

   /** 
    * A method updating the recent colors in the swatchPanel
    * This is called whenever necessary, specifically after building the panel,
    * on changes of the tracker, from the mouseListener
    */
   protected void updateRecentSwatchPanel() {
       if (recentSwatchPanel == null) return;
       recentSwatchPanel.setMostRecentColors(tracker != null ? tracker.getColors() : null);
   }

// the mouseListener which updates the tracker and triggers the doubleClickAction
// if available
class MainSwatchListener extends MouseAdapter implements Serializable {
    @Override
    public void mousePressed(MouseEvent e) {
        if (!isEnabled())
            return;
        if (e.getClickCount() == 2) {
            handleDoubleClick(e);
            return;
        }

        Color color = swatchPanel.getColorForLocation(e.getX(), e.getY());
        setSelectedColor(color);
        if (tracker != null) {
            tracker.addColor(color);
        } else {
            recentSwatchPanel.setMostRecentColor(color);
        }
    }

    /**
     * @param e
     */
    private void handleDoubleClick(MouseEvent e) {
        if (action != null) {
            action.actionPerformed(null);
        }
    }
}


} 

// client code can install the custom panel on a JFileChooser, passing in a tracker
private JColorChooser createChooser(ColorTracker tracker) {
    JColorChooser chooser = new JColorChooser();
    List<AbstractColorChooserPanel> choosers = 
            new ArrayList<>(Arrays.asList(chooser.getChooserPanels()));
    choosers.remove(0);
    MySwatchChooserPanel swatch = new MySwatchChooserPanel();
    swatch.setColorTracker(tracker);
    swatch.setAction(doubleClickAction);
    choosers.add(0, swatch);
    chooser.setChooserPanels(choosers.toArray(new AbstractColorChooserPanel[0]));
    return chooser;
}

关于doubleClick处理:增强swatchChooser以执行操作并调用该操作从mouseListener中酌情。

As to doubleClick handling: enhance the swatchChooser to take an action and invoke that action from the mouseListener as appropriate.

这篇关于JColorChooser:在“色板”面板中保存/恢复最近的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆