2017-09-18 9 views
0

내 응용 프로그램 용 JTree를 만들었습니다. 이제 일부 노드 또는 전체 트리 자체의 색상을 변경하고 싶습니다. 검색 및 사용자 지정 TreeCellRendererComponent 만들고 색을 업데이트하지만 그것은 나를 위해 작동하지 않는 것으로 나타났습니다. 어쩌면 내가 뭔가를 놓치고 있거나 내가 생각하는 바가 어쩌면 색상을 바꾸지 않는 JTree의 특정 속성을 업데이트하고있는 것일 수도 있습니다.JFrame에서 JTree 색상이 변경되지 않습니다.

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Component; 
import java.awt.EventQueue; 
import java.awt.Font; 

import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTree; 
import javax.swing.border.EmptyBorder; 
import javax.swing.tree.DefaultMutableTreeNode; 
import javax.swing.tree.DefaultTreeCellRenderer; 
import javax.swing.tree.DefaultTreeModel; 

public class TestClass2 extends JFrame { 

    private JPanel contentPane; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        TestClass2 frame = new TestClass2(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the frame. 
    */ 
    public TestClass2() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 450, 300); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     contentPane.setLayout(new BorderLayout()); 
     contentPane.add(prepareCommandTree(), BorderLayout.CENTER); 
    } 


    public static JTree prepareCommandTree() 
    { 
     // Root node name which is the name of the command e.g. Display Text 
     DefaultMutableTreeNode commandNode = new DefaultMutableTreeNode("Command Name"); 
     DefaultMutableTreeNode completeData = new DefaultMutableTreeNode("Complete Data"); 

     // Adding all branches under root branch 
     commandNode.add(completeData); 

     //create the tree by passing in the root node 
     JTree commandTree = new JTree(commandNode); 
     DefaultTreeModel model = (DefaultTreeModel)commandTree.getModel(); 
     DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot(); 
     model.reload(root); 

     // Setting JTree background 
     commandTree.setOpaque(false); 
     commandTree.collapseRow(0); 
     commandTree.setBorder(new EmptyBorder(5, 0, 0, 0)); 
     commandTree.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 13)); 

     // Adding image icon to the tree 
     commandTree.setCellRenderer(new DefaultTreeCellRenderer(){ 
     private static final long serialVersionUID = 1L; 

     public Component getTreeCellRendererComponent(final JTree tree,Object value, 
      boolean sel,boolean expanded,boolean leaf,int row,boolean hasFocus) 
     { 
      // Trying to change color of tree 
      setForeground(Color.RED); 
      JLabel label = (JLabel)super.getTreeCellRendererComponent(tree,value, 
                sel,expanded,leaf,row,hasFocus); 
       return label; 
     } 
     }); 

     // Setting adjustments to JTree properties 
     commandTree.putClientProperty("JTree.lineStyle", "None"); 
     commandTree.setAlignmentX(Component.LEFT_ALIGNMENT); 
     DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) commandTree.getCellRenderer(); 
     renderer.setLeafIcon(null); 
     renderer.setClosedIcon(null); 
     renderer.setOpenIcon(null); 

     return commandTree; 
    } 
} 

어떤 제안이나 수정이 도움이 될 것입니다. 감사. :-)

답변

5

렌더러가 전경을 설정합니다. Color 전에 super 호출이 수행되었습니다.

또한 super 메서드는 사용자가 설정 한 전경을 대체 할 전경 Color을 설정합니다.

전화를 걸어서 Color을 적용하면됩니다.

public Component getTreeCellRendererComponent(final JTree tree, final Object value, 
      final boolean sel, final boolean expanded, final boolean leaf, final int row, 
      final boolean hasFocus) { 
     // Trying to change color of tree 

     JLabel label = (JLabel) super.getTreeCellRendererComponent(tree, value, 
       sel, expanded, leaf, row, hasFocus); 
     setForeground(Color.RED); 
     return label;// Or "return this", since the method actually returns the renderer component itself 
    } 
}); 
+2

추가 설명 :'super.getTCRC ... (...)'는'this'를 JLabel로 반환합니다. 'label.setForeground (Color.RED);'더 명확 할 것입니다. –

+0

해결책에 대해 Berger에게 감사하고 더 중요한 이유가 있습니다. :-) – Aman

+0

그리고 더 많은 명확성을 가져 오기위한 @JoopEggen에 감사드립니다. :) – Berger