问题
I am wondering how to make the following code in replacing the JTree
component working. Currently I am getting a 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)
How to replace a JTree component in Java Swing?
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();
}
}
回答1:
Instead of Integer.getInteger(String)
you want to use Interger.parseInt(String)
.
/e1 Use this and the tree will randomize:
invalidate();
remove(tree);
tree = buildTree(Integer.parseInt(jtf.getText()));
add(tree, BorderLayout.CENTER);
validate();
回答2:
The mistake you were looking for is in the line: tree = buildTree(Integer.getInteger(jtf.getText())); You should have used Integer.parseInt(String s) As this the one which parses String into an Int. getInteger(String s) deals with values of system properties.
来源:https://stackoverflow.com/questions/6801900/how-to-replace-a-jtree-component-in-java-swing