트리의 일부 노드에 아이콘이있는 JTree가 있습니다. 아이콘이있는 노드를 선택하면 렌더러가 전체 노드를 선택하지는 않지만 아이콘이 아래 노드의 왼쪽에 있다고 생각하는 것처럼 오프셋이 적용된 것처럼 보입니다. :JTree 셀 렌더러 문제
(DefaultTreeCellRenderer 확장) 렌더러에 대한 코드는 다음과 같습니다 :
public ProfileTreeRenderer() {
super.setLeafIcon(null);
super.setClosedIcon(null);
super.setOpenIcon(null);
}
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
Component c = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
if (c instanceof JLabel) {
JLabel label = (JLabel) c;
label.setHorizontalTextPosition(SwingConstants.LEADING);
}
if(sel && !hasFocus) {
setBackgroundSelectionColor(UIManager.getColor("Panel.background"));
setTextSelectionColor(UIManager.getColor("Panel.foreground"));
} else {
setTextSelectionColor(UIManager.getColor("Tree.selectionForeground"));
setBackgroundSelectionColor(UIManager.getColor("Tree.selectionBackground"));
}
if (value instanceof ProfileNode) {
ProfileNode node = (ProfileNode) value;
if (node.isUsed() && !sel) {
c.setForeground(Color.GRAY);
}
if (node.getIcon() != null) {
setIcon(node.getIcon());
}
}
}
내가 렌더러이 오프셋 적용됩니다, 그래서 사람이 얻을 수있는 방법을 제공 할 수있는 이유를 볼 수 없습니다
노드가 아이콘으로 완전히 선택 되었습니까? 트리 자체에 대한 SSCCE 코드는 다음과 같습니다.
public class Example extends JDialog {
public Example() {
JTree tree = new JTree(createModel());
tree.setCellRenderer(new ProfileTreeRenderer());
setLayout(new BorderLayout());
add(tree, BorderLayout.CENTER);
}
private TreeModel createModel() {
ProfileNode root = new ProfileNode("Profiles");
ProfileNode userA = new ProfileNode("Example User A");
ProfileNode userB = new ProfileNode("Example User B");
// You'll need to subsitute your own 16x16 icons here
userA.setIcon(ImageSet.USER_ICON);
userB.setIcon(ImageSet.USER_ICON);
root.add(userA);
root.add(userB);
return new DefaultTreeModel(root);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Example().setVisible(true);
}
});
}
}
ProfileNode 클래스 : 사전에
public class ProfileNode extends DefaultMutableTreeNode {
@Getter private String labelDisplay;
@Getter @Setter private ImageIcon icon;
@Getter @Setter private boolean isUsed = false;
public ProfileNode(String labelDisplay) {
this.labelDisplay = labelDisplay;
}
@Override
public String toString() {
return labelDisplay;
}
}
감사합니다.
: 당신은 다시 조정 ... 나 같은
뭔가 :-) SwingX 렌더러를 사용할 필요가? 어쨌든, 문제를 나타내는 SSCCE를 보여주십시오 :-) – kleopatra
아니요, 문제는 JXTree (그에 따라 업데이트 된 질문)에 맞지 않습니다. SSCCE가 추가되었습니다. – Ocracoke
코드가 "S"중 하나를 위반하고 있습니다 :-) 독립형으로 변경하십시오. – kleopatra