使用Numbus L& F设置JTree选择背景

时间:2015-10-23 20:03:11

标签: java swing background jtree nimbus

使用Nimbus L& F时,如何为JTree中的所选项目设置背景颜色?

对类似问题的旧答案中的建议对Nimbus不起作用,所以我尝试了:

final UIDefaults def = new UIDefaults();
def.put( "Tree.background", Color.LIGHT_GRAY );
def.put( "Tree.selectionBackground", Color.RED );
def.put( "Tree.drawHorizontalLines", true );
def.put( "Tree.drawVerticalLines", true );
def.put( "Tree.leftChildIndent", 12 );
def.put( "Tree.rightChildIndent", 12 );
myTree.putClientProperty( "Nimbus.Overrides", def );
myTree.putClientProperty( "Nimbus.Overrides.InheritDefaults", false );

适用于背景,drawHorizontalLinesdrawVerticalLinesleftChildIndentrightChildIndent,但无法为选择背景执行任何操作。

我还尝试为“Tree.textForeground”,“Tree.foreground”和“Tree.textBackground”设置各种颜色,但这些颜色没有效果。

我也尝试过:

 final Painter<JComponent> painter = new Painter<JComponent>() {
    @Override
    public void paint( final Graphics2D g, final JComponent c, final int w, final int h ) {
       g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
       g.setColor( Color.RED );
       g.fillRect( 0, 0, w, h );
    }
 };
 def.put( "Tree:TreeCell[Enabled+Selected].backgroundPainter", painter );

也没有效果。

1 个答案:

答案 0 :(得分:0)

Tree:TreeCell[Enabled+Selected].backgroundPainter在Windows 10上使用JDK1.8.0_66可以正常使用。

import java.awt.*;
import javax.swing.*;
import javax.swing.Painter;

public class TreeCellPainterTest {
  public JComponent makeUI() {
    //UIDefaults d = new UIDefaults();
    UIDefaults d = UIManager.getLookAndFeelDefaults();
    d.put("Tree:TreeCell[Enabled+Selected].backgroundPainter", new Painter<JComponent>() {
      @Override public void paint(Graphics2D g, JComponent c, int w, int h) {
        g.setColor(Color.GREEN);
        g.fillRoundRect(0, 0, w - 1, h - 1, 15, 15);
      }
    });
    d.put("Tree:TreeCell[Focused+Selected].backgroundPainter", new Painter<JComponent>() {
      @Override public void paint(Graphics2D g, JComponent c, int w, int h) {
        g.setColor(Color.RED);
        g.fillRect(0, 0, w, h);
      }
    });

    JTree tree = new JTree();
    tree.putClientProperty("Nimbus.Overrides", d);
    tree.putClientProperty("Nimbus.Overrides.InheritDefaults", false);

    JPanel p = new JPanel(new GridLayout(1, 2));
    p.add(new JScrollPane(new JTree()));
    p.add(new JScrollPane(tree));
    return p;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    try {
      for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(laf.getName())) {
          UIManager.setLookAndFeel(laf.getClassName());
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new TreeCellPainterTest().makeUI());
    frame.setSize(320, 240);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}