[英]Override selection color - Jtree
DefaultTreeCellRenderer
有一個名為setBackgroundSelectionColor()
的方法,當您在JTree
上選擇一個項目時,該方法可以更改選擇顏色。 當我進入DefaultTreeCellRenderer
的源代碼時,選擇顏色是由getBackgroundSelectionColor()
在paint()
設置的。
在下面的代碼中,我試圖通過簡單地覆蓋paint
方法來更改選擇顏色。 問題是,即使在繪畫方法中手動設置顏色,顏色也保持不變。 同樣,即使getBackgroundSelectionColor()
不再在paint
使用,方法setBackgroundSelectionColor()
仍然有效。
為什么會這樣,如何正確覆蓋顏色?
不使用setBackgroundSelectionColor()
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
@SuppressWarnings("serial")
public class DirectoryExplorer extends JFrame {
private DirectoryExplorer() {
super("Directory Explorer");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(1, 1));
createPanel();
setSize(800,600);
setVisible(true);
}
private void createPanel() {
JPanel panel = new JPanel(new GridLayout(1, 1));
JTree tree = new JTree();
paintRenderer pR = new paintRenderer();
tree.setCellRenderer(pR);
//pR.setBackgroundSelectionColor(Color.RED); //Why does this work when changing value in paint doesn't
panel.add(new JScrollPane(tree));
getContentPane().add(panel);
}
public static void main(String[] args) {
new DirectoryExplorer();
}
private class paintRenderer extends DefaultTreeCellRenderer {
@Override
public void paint(Graphics g) {
Color bColor;
if (selected) {
bColor = Color.RED;//= getBackgroundSelectionColor();
} else {
bColor = getBackgroundNonSelectionColor();
if (bColor == null) {
bColor = getBackground();
}
}
//super.paint(g); //Paints the correct colors but no text
int imageOffset = -1;
if (bColor != null) {
imageOffset = getLabelStart();
g.setColor(bColor);
if(getComponentOrientation().isLeftToRight()) {
g.fillRect(imageOffset, 0, getWidth() - imageOffset, getHeight());
} else {
g.fillRect(0, 0, getWidth() - imageOffset, getHeight());
}
}
if (hasFocus) {
if (imageOffset == -1) {
imageOffset = getLabelStart();
}
if(getComponentOrientation().isLeftToRight()) {
paintFocus(g, imageOffset, 0, getWidth() - imageOffset, getHeight(), bColor);
} else {
paintFocus(g, 0, 0, getWidth() - imageOffset, getHeight(), bColor);
}
}
super.paint(g); //Paints text but wrong colors
}
private void paintFocus(Graphics g, int x, int y, int w, int h, Color notColor) {
Color bsColor = Color.RED;//= getBorderSelectionColor();
if (bsColor != null && selected) {
g.setColor(bsColor);
g.drawRect(x, y, w - 1, h - 1);
}
}
private int getLabelStart() {
Icon currentI = getIcon();
if(currentI != null && getText() != null) {
return currentI.getIconWidth() + Math.max(0, getIconTextGap() - 1);
}
return 0;
}
}
}
編輯
目前的樣子
問題是您要覆蓋paint(...)
而不是paintComponent(...)
作為該規定的回答 ,你不應該覆蓋paint
的東西,這不是一個頂級容器中搖擺,相反,你應該重寫paintComponent
。
更改代碼看起來像這樣。
@Override
public void paintComponent(Graphics g) {
...
super.paintComponent(g);
}
例如
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
@SuppressWarnings("serial")
public class DirectoryExplorer extends JFrame {
private DirectoryExplorer() {
super("Directory Explorer");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(1, 1));
createPanel();
setSize(800,600);
setVisible(true);
}
private void createPanel() {
JPanel panel = new JPanel(new GridLayout(1, 1));
JTree tree = new JTree();
paintRenderer pR = new paintRenderer();
tree.setCellRenderer(pR);
panel.add(new JScrollPane(tree));
getContentPane().add(panel);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new DirectoryExplorer());
}
private class paintRenderer extends DefaultTreeCellRenderer {
@Override
public void paintComponent(Graphics g) {
Color bColor;
if (selected) {
bColor = Color.RED;
} else {
bColor = getBackgroundNonSelectionColor();
if (bColor == null) {
bColor = getBackground();
}
}
int imageOffset = -1;
if (bColor != null) {
imageOffset = getLabelStart();
g.setColor(bColor);
if(getComponentOrientation().isLeftToRight()) {
g.fillRect(imageOffset, 0, getWidth() - imageOffset, getHeight());
} else {
g.fillRect(0, 0, getWidth() - imageOffset, getHeight());
}
}
if (hasFocus) {
if (imageOffset == -1) {
imageOffset = getLabelStart();
}
if(getComponentOrientation().isLeftToRight()) {
paintFocus(g, imageOffset, 0, getWidth() - imageOffset, getHeight());
} else {
paintFocus(g, 0, 0, getWidth() - imageOffset, getHeight());
}
}
super.paintComponent(g);
}
private void paintFocus(Graphics g, int x, int y, int w, int h) {
Color bsColor = Color.RED;
if (bsColor != null && selected) {
g.setColor(bsColor);
g.drawRect(x, y, w - 1, h - 1);
}
}
private int getLabelStart() {
Icon currentI = getIcon();
if(currentI != null && getText() != null) {
return currentI.getIconWidth() + Math.max(0, getIconTextGap() - 1);
}
return 0;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.