ある条件で木の葉だけ色を変えたい。
if (l_sel_node_object.getIsEnabled().trim().equalsIgnoreCase("N"))
defaultTreeCellRenderer.setBackgroundSelectionColor(Color.red);
選択した各ノードの背景を変更しています。誰か助けてください。
ある条件で木の葉だけ色を変えたい。
if (l_sel_node_object.getIsEnabled().trim().equalsIgnoreCase("N"))
defaultTreeCellRenderer.setBackgroundSelectionColor(Color.red);
選択した各ノードの背景を変更しています。誰か助けてください。
メタル ルック アンド フィールでのみテスト:
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
public class MainPanel {
public JPanel makeUI() {
JTree tree = new JTree();
tree.setCellRenderer(new RedTreeCellRenderer());
JPanel p = new JPanel(new BorderLayout(5, 5));
p.add(new JScrollPane(tree));
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new MainPanel().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
//*
class RedTreeCellRenderer extends DefaultTreeCellRenderer {
@Override public Component getTreeCellRendererComponent(
JTree tree, Object value, boolean isSelected,
boolean expanded, boolean leaf, int row, boolean hasFocus) {
JComponent c = (JComponent)super.getTreeCellRendererComponent(
tree, value, isSelected, expanded, leaf, row, hasFocus);
if(isSelected) {
c.setForeground(getTextSelectionColor());
if (leaf && value.toString().equalsIgnoreCase("red")) {
//<strong>
c.setOpaque(true);
//</strong>
c.setBackground(Color.RED);
} else {
c.setOpaque(false);
c.setBackground(getBackgroundSelectionColor());
}
} else {
c.setOpaque(false);
c.setForeground(getTextNonSelectionColor());
c.setBackground(getBackgroundNonSelectionColor());
}
return c;
}
}
/*/
class RedTreeCellRenderer2 extends DefaultTreeCellRenderer {
@Override public Component getTreeCellRendererComponent(
JTree tree, Object value, boolean isSelected,
boolean expanded, boolean leaf, int row, boolean hasFocus) {
JComponent c = (JComponent)super.getTreeCellRendererComponent(
tree, value, isSelected, expanded, leaf, row, hasFocus);
if(isSelected) {
if(leaf) {
setParticularCondition(value.toString());
}
c.setForeground(getTextSelectionColor());
c.setBackground(getBackgroundSelectionColor());
} else {
c.setForeground(getTextNonSelectionColor());
c.setBackground(getBackgroundNonSelectionColor());
}
return c;
}
boolean particularCondition = false;
private void setParticularCondition(String str) {
particularCondition = str.equalsIgnoreCase("red");
}
@Override public Color getBackgroundSelectionColor() {
if(particularCondition) return Color.RED;
else return super.getBackgroundSelectionColor();
}
}
//*/