2016-08-02 2 views
1

그래서 나뭇잎과 노드의 변화하는 숫자를 가지고 J Tree Tree의 다른 부분에 JComboBoxes를 추가해야하는 JTree가 있습니다. 화면 위치를 사용해 보았습니다. 나뭇잎과 테두리 레이아웃이 달성하기 위해하지만 상자는 항상 끝나고 내가 나뭇잎이 많이있을 때 그것은 매우 나빠질뿐만 아니라 그들은 프레임의 1 위치에 국한되어 모든 새로운 콤보 박스와 함께 더 얇게 유지됩니다 덧붙였다. 어떻게 내가 무엇을 찾고 있는지 얻을 수 있을까?Jtrees 및 combobox 레이아웃

답변

2

나는 나무의 잎하지만

JTree에 직접 JComboBox를 통합하는 TreeCellRenderer 당신의 자신을 만드는 고려 트리의 다른 부분에 다음 JComboBoxes를 추가해야합니다. JComboBox의 편집을 허용하려면 편집 구성 요소와 편집이 완료되었을 때 해당 구성 요소에 발생하는 작업을 처리하는 TreeCellEditor도 구현해야합니다.

public class MyLeafNode extends DefaultMutableTreeNode{ 

    private String[] items; 
    private String selected; 

    public MyLeafNode(String name, String...items){ 
     super(name); 
     this.items = items; 
     this.selected = items[0]; 
    } 

} 
: 아래 MyLeafNode이 JComboBox에에게 특정 데이터를 저장하는 데 사용되는 사용자 정의 클래스 인 JTree

JFrame frame = new JFrame("Test"); 
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); 
DefaultTreeModel model = new DefaultTreeModel(root); 
final JTree tree = new JTree(model); 

//flyweight pattern components 
//editor 
final JComboBox editorComboBox = new JComboBox(); 
final JComboBox viewComboBox = new JComboBox(); 
final Box box = Box.createHorizontalBox(); 
final JLabel myLabel = new JLabel(); 
myLabel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 10)); 
box.add(myLabel); 
box.add(Box.createHorizontalGlue()); 
box.add(viewComboBox); 
//Custom Renderer 
DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer(){ 
    @Override 
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { 
     super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus); 
     if (leaf){ 
      if (value instanceof MyLeafNode){ 
       MyLeafNode node = (MyLeafNode)value; 
       viewComboBox.removeAllItems(); 
       myLabel.setText(value.toString()); 
       for (String item : node.items){ 
        viewComboBox.addItem(item); 
       } 
       viewComboBox.setSelectedItem(node.selected); 
       return box; 
      } 
     } 
     return this; 
    } 
}; 

//Custom Editor 
final DefaultTreeCellEditor editor = new DefaultTreeCellEditor(tree, renderer){ 

    final ActionListener actionListener = new ActionListener(){ 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      cancelCellEditing(); 
      tree.repaint(); 
     } 
    }; 

    @Override 
    public boolean isCellEditable(EventObject e){ 
     if (e.getSource() instanceof JTree){ 
      JTree tree = (JTree)e.getSource(); 
      if (tree.getLastSelectedPathComponent() == null){ 
       return false; 
      } 
      DefaultMutableTreeNode o = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); 
      return o.isLeaf(); 
     } 
     return false; 
    } 

    @Override 
    public void cancelCellEditing(){ 
     super.cancelCellEditing(); 
     DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); 
     if (node instanceof MyLeafNode) { 
      String sel = editorComboBox.getSelectedItem().toString(); 
      MyLeafNode mln = (MyLeafNode)node; 
      mln.selected = sel; 
      editorComboBox.removeActionListener(actionListener); 
      tree.repaint(); 
     } 
    } 

    @Override 
    public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) { 
     if (leaf){ 
      if (tree.getLastSelectedPathComponent() instanceof MyLeafNode){ 
       MyLeafNode o = (MyLeafNode)tree.getLastSelectedPathComponent(); 
       editorComboBox.removeAllItems(); 
       for (String item : o.items){ 
        editorComboBox.addItem(item); 
       } 
       editorComboBox.setSelectedItem(o.selected); 
       editorComboBox.addActionListener(actionListener); 
      } 
      return editorComboBox; 

     } 
     return super.getTreeCellEditorComponent(tree, value, isSelected, expanded, leaf, row); 
    } 

}; 
tree.setCellRenderer(renderer); 
TreePath path = new TreePath(new TreeNode[]{root}); 
tree.expandPath(path); 

for (int i = 0; i < 2; i++){ 
    DefaultMutableTreeNode p = new DefaultMutableTreeNode("P" + i); 
    model.insertNodeInto(p, root, i); 
    for (int j = 0; j < 2; j++){ 
     String[] items = {"Item 1", "Item 2", "Item 3", "Item 4"}; 
     MyLeafNode n = new MyLeafNode("N" + j, items); 
     model.insertNodeInto(n, p, j); 
    } 
    path = new TreePath(new TreeNode[]{root, p}); 
    tree.expandPath(path); 
} 
tree.setCellEditor(editor); 
tree.setEditable(true); 

JScrollPane scroller = new JScrollPane(tree); 
frame.add(scroller); 
frame.pack(); 
frame.setVisible(true); 

의 잎에서 JLabel 옆에 JComboBox을 배치 매우 간단한 예입니다