如何在Java Swing中替换JTree组件?

时间:2011-07-23 16:54:19

标签: java swing jtree

我想知道如何在替换JTree组件时使用以下代码。目前我正在获得java.lang.NullPointerException

at RandomJTree$1.keyPressed(RandomJTree.java:64)
at java.awt.Component.processKeyEvent(Component.java:6221)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2801)
at java.awt.Component.processEvent(Component.java:6040)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1848)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:704)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:969)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:841)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:668)
at java.awt.Component.dispatchEventImpl(Component.java:4502)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

如何在Java Swing中替换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.JTextField;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;


public class RandomJTree extends JFrame {

    private final JTextField jtf = new JTextField(10);
    private JTree tree;
    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 RandomJTree() {
        setLayout(new BorderLayout());
        add(jtf, BorderLayout.NORTH);
        tree = buildTree(10);
        add(tree, BorderLayout.CENTER);

        jtf.addKeyListener(new KeyAdapter() {
                public void keyPressed(KeyEvent e) {
                    int key = e.getKeyCode();
                    if (key == KeyEvent.VK_ENTER) {
                        tree =  buildTree(Integer.getInteger(jtf.getText()));
                        tree.revalidate();
                        tree.repaint();
                    }
                }
            });

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

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

2 个答案:

答案 0 :(得分:2)

而不是Integer.getInteger(String)您要使用Interger.parseInt(String)

/ E1 使用此选项,树将随机化:

invalidate();
remove(tree);
tree = buildTree(Integer.parseInt(jtf.getText()));
add(tree, BorderLayout.CENTER);
validate();

答案 1 :(得分:0)

你正在寻找的错误在于:     tree = buildTree(Integer.getInteger(jtf.getText())); 你应该使用Integer.parseInt(String s) 因为这是将String解析为Int的那个。 getInteger(String s)处理系统属性的值。