我正在尝试在JFrame中的JScrollPane上显示JTree。这是代码:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ext.DefaultHandler2;
public class TreeView {
private JFrame frame;
private JTree tree;
private DefaultMutableTreeNode root;
public TreeView() {
}
private void show() {
frame = new JFrame("TreeView");
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
JFrame.setDefaultLookAndFeelDecorated(true);
addComponents();
frame.setVisible(true);
}
private void addComponents() {
JDialog.setDefaultLookAndFeelDecorated(true);
JButton button = new JButton("Select File");
frame.add(button, BorderLayout.NORTH);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent ae) {
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter xmlfilter = new FileNameExtensionFilter(
"xml files (*.xml)", "xml");
fileChooser.setFileFilter(xmlfilter);
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
try {
showAsTree(selectedFile.getAbsolutePath());
tree = new JTree(root);
JScrollPane treeView = new JScrollPane(tree);
frame.add(treeView, BorderLayout.CENTER);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
}
});
}
private void showAsTree(final String file)
throws ParserConfigurationException, SAXException, IOException {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxparser = factory.newSAXParser();
saxparser.parse(file, new MyContentHandler());
}
public static void main(final String[] args) throws IOException {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TreeView treeView = new TreeView();
treeView.show();
}
});
}
private class MyContentHandler extends DefaultHandler2 {
@Override
public void startDocument() throws SAXException {
root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode abc = new DefaultMutableTreeNode(
"abc");
DefaultMutableTreeNode de = new DefaultMutableTreeNode(
"de");
root.add(abc);
root.add(de);
}
}
}
当我尝试向JTextPane
内的JScrollPane
添加文字时,一切正常。当我在添加按钮之前尝试在show()
方法或addComponents()
方法中显示树时,它也会显示。
我究竟做错了什么?
答案 0 :(得分:2)
在框架上调用revalidate
和repaint
和/或contentPane
。
showAsTree(selectedFile.getAbsolutePath());
tree = new JTree(root);
JScrollPane treeView = new JScrollPane(tree);
frame.add(treeView, BorderLayout.CENTER);
frame.revalidate();
frame.repaint();
就我个人而言,我会先将JTree
添加到框架中,然后再将其设为可见,然后在需要时更新TreeModel