2011-07-24 66 views
0

這是後續問題my previous post。我正在嘗試使用以下代碼將JTree放入JScrollPane中。 JTree的初始化看起來很好,在JTextField中輸入100之後,然後回車也很順利。但是當我滾動垂直滑動窗口,調整窗口大小時,或試圖擴展新的JTree節點時,我發現樹只有10個節點回到原始狀態。感謝你的幫助。替換JScrollPane中的JTree後發生的奇怪行爲

import java.awt.BorderLayout; 
import java.awt.event.KeyAdapter; 
import java.awt.event.KeyEvent; 

import java.util.Random; 

import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTextField; 
import javax.swing.JTree; 
import javax.swing.tree.DefaultMutableTreeNode; 


public class RandomJTree2 extends JFrame { 

    private final JTextField jtf = new JTextField(10); 
    private JTree tree; 
    JScrollPane jsp; 
    private Random generator = new Random(); 

    private JTree buildTree(int nnodes) { 
     DefaultMutableTreeNode node = new DefaultMutableTreeNode(0); 
     DefaultMutableTreeNode cnode = node; 
     for (int i = 1; i <= nnodes; i++) { 
      System.out.println(i); 
      DefaultMutableTreeNode newn = new DefaultMutableTreeNode(i); 
      int j = generator.nextInt(2); 
      System.out.println("j=" + j); 
      if (j == 0 && i != 1) { 
       DefaultMutableTreeNode parent = (DefaultMutableTreeNode)cnode.getParent(); 
       if (parent != null) { 
        parent.add(newn); 
        cnode = parent; 
       } else { 
        node.add(newn); 
        cnode = newn; 
       } 
      } else { 
       cnode.add(newn); 
       cnode = newn; 
      } 
     } 
     return new JTree(node); 
    } 

    public RandomJTree2() { 
     setLayout(new BorderLayout()); 
     add(jtf, BorderLayout.NORTH); 
     tree = buildTree(10); 
     jsp = new JScrollPane(tree); 
     add(jsp, BorderLayout.CENTER); 

     jtf.addKeyListener(new KeyAdapter() { 
       public void keyPressed(KeyEvent e) { 
        int key = e.getKeyCode(); 
        if (key == KeyEvent.VK_ENTER) { 
         invalidate(); 
         //      jsp.remove(tree); 
         //      tree = buildTree(Integer.parseInt(jtf.getText())); 
         //      jsp.add(tree); 
         //      remove(jsp); 
         tree = buildTree(Integer.parseInt(jtf.getText())); 
         jsp = new JScrollPane(tree); 
         add(jsp, BorderLayout.CENTER); 
         validate(); 
        } 
       } 
      }); 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLocationRelativeTo(null); 
     pack(); 
     setVisible(true); 
    } 

    public static void main(String[] args) { 
     new RandomJTree2(); 
    } 
} 

回答

1

給你:

tree = buildTree(Integer.parseInt(jtf.getText())); 
remove(jsp); //<-------------------- 
jsp = new JScrollPane(tree); 
add(jsp, BorderLayout.CENTER); 
validate(); 

你需要投入新的之前刪除舊的滾動窗格

+0

謝謝!我以爲我嘗試過,但沒有奏效。無論如何,它現在工作正常。 :) –

+0

重新驗證()+重繪(),以防止任何可能的問題與Swing重繪+1 – mKorbel

+0

@mKorbel:你是說我不需要添加和刪除,只是重新驗證並重新繪製就足夠了? –

1

小小的額外的評論,你會如果你按下得到一個錯誤。當文本字段爲空(或其他格式無效的數字)時輸入。

try { 
    int yourNumer = Integer.parseInt(jtf.getText()); // pass this to buildTree() 
    if (key == KeyEvent.VK_ENTER) {// do your stuff} 
} catch (NumberFormatException e){ 
// show a dialog or somthing :D 
} 
相關問題