【问题标题】:Change color of WindowsPlacesBar in JFileChooser在 JFileChooser 中更改 WindowsPlacesBar 的颜色
【发布时间】:2009-08-10 22:02:39
【问题描述】:

这是我上一个问题的后续问题:

Need FileDialog with a file type filter in Java

我有一个 JFileChooser(使用它而不是 FileDialog,所以我可以有一个文件类型过滤器)并且我已经设法为我们的较暗配色方案选项设置了相当不错的样式,除了左侧的那个小面板。我终于发现上面的那个是“ToolBar.background”,但我不知道那个叫什么。

帮助?

alt text http://img151.imageshack.us/img151/6816/filedialog.jpg

【问题讨论】:

    标签: java swing jfilechooser


    【解决方案1】:

    我不知道如何改变它的颜色,但我知道如何摆脱它:

    UIManager.put("FileChooser.noPlacesBar", Boolean.TRUE);
    

    或者,如果您真的希望显示面板,那么您可以搜索源代码以查看该面板是如何创建的,以查看是否可以覆盖其默认颜色。

    【讨论】:

    • 好吧,我不知道它被称为“Places Bar”,所以看起来它可能会有所帮助,但我仍然无法更改它的颜色。明显的“FileChooser.placesBarBackground”是不行的。
    【解决方案2】:

    我最终通过查看 WindowsPlacesBar 的源代码找出了该属性的名称:

    Color bgColor = new Color(UIManager.getColor("ToolBar.shadow").getRGB());
    setBackground(bgColor);
    

    我设置了 ToolBar.shadow,但没有任何改变。进一步探索最终帮助我意识到 XPStyle.subAppName 属性覆盖了我输入的任何内容。我添加了这段代码:

    JFileChooser chooser = new JFileChooser();
    setWindowsPlacesBackground( chooser );
    
    private void setWindowsPlacesBackground( Container con ) {
      Component[] jc = con.getComponents();
      for( int i = 0; i < jc.length; i++ ) {
        Component c = jc[i];
        if( c instanceof WindowsPlacesBar ) {
          ((WindowsPlacesBar) c).putClientProperty("XPStyle.subAppName", null);
          return;
        }
        if( c instanceof Container ) {
          setWindowsPlacesBackground( (Container)c );
        }
      }
    }
    

    通过取消设置该属性,它允许我的颜色和方案通过。我仍然觉得应该有一种比遍历容器更干净的方法来取消它,但我找不到它。看起来 WindowsPlacesBar 始终是 FileChooser 中的第一个组件。我打算再打开一两天,以防其他人可以向我展示更“优雅”的东西。

    【讨论】:

    • 另外,显然在 Java 5 中完全不可能更改 JToolBar(它继承)的颜色,但在 Java 6 中已修复。
    猜你喜欢
    • 1970-01-01
    • 2021-01-07
    • 1970-01-01
    • 2021-05-28
    • 2013-11-06
    • 2019-04-17
    • 2017-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多