我得到了以下代码[减少到最小]:
public class MainWindow extends JFrame {
private static String StrBenutzer;
private JPanel contentPane;
TreeNode TreeNodeRoot = createInitTree();
JTree tree = new JTree(TreeNodeRoot);
static DefaultMutableTreeNode NodeRoot;
static DefaultMutableTreeNode NodeProjekte;
static DefaultMutableTreeNode NodeAllgemein;
/**
* Launch the application.
*/
public static void initWindow() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow frame = new MainWindow();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(false);
frame.setVisible(true);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainWindow() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1160, 679);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
SpringLayout sl_contentPane = new SpringLayout();
contentPane.setLayout(sl_contentPane);
sl_contentPane.putConstraint(SpringLayout.NORTH, tree, 0, SpringLayout.NORTH, contentPane);
tree.setEditable(true);
contentPane.add(tree);
JButton btnAdd = new JButton("Add");
sl_contentPane.putConstraint(SpringLayout.WEST, tree, 0, SpringLayout.WEST, btnAdd);
sl_contentPane.putConstraint(SpringLayout.SOUTH, tree, -11, SpringLayout.NORTH, btnAdd);
sl_contentPane.putConstraint(SpringLayout.WEST, btnAdd, 0, SpringLayout.WEST, contentPane);
sl_contentPane.putConstraint(SpringLayout.SOUTH, btnAdd, -10, SpringLayout.SOUTH, contentPane);
contentPane.add(btnAdd);
JButton btnDelete = new JButton("Delete");
sl_contentPane.putConstraint(SpringLayout.NORTH, btnDelete, 0, SpringLayout.NORTH, btnAdd);
sl_contentPane.putConstraint(SpringLayout.WEST, btnDelete, 6, SpringLayout.EAST, btnAdd);
contentPane.add(btnDelete);
JButton btnInfo = new JButton("Info");
sl_contentPane.putConstraint(SpringLayout.EAST, tree, 1, SpringLayout.EAST, btnInfo);
sl_contentPane.putConstraint(SpringLayout.NORTH, btnInfo, 0, SpringLayout.NORTH, btnAdd);
sl_contentPane.putConstraint(SpringLayout.WEST, btnInfo, 6, SpringLayout.EAST, btnDelete);
sl_contentPane.putConstraint(SpringLayout.EAST, btnInfo, 59, SpringLayout.EAST, btnDelete);
contentPane.add(btnInfo);
JPanel panel = new JPanel();
sl_contentPane.putConstraint(SpringLayout.NORTH, panel, 0, SpringLayout.NORTH, tree);
sl_contentPane.putConstraint(SpringLayout.WEST, panel, 16, SpringLayout.EAST, tree);
sl_contentPane.putConstraint(SpringLayout.SOUTH, panel, 565, SpringLayout.NORTH, contentPane);
sl_contentPane.putConstraint(SpringLayout.EAST, panel, 39, SpringLayout.EAST, contentPane);
contentPane.add(panel);
}
private static TreeNode createInitTree(){
NodeRoot = new DefaultMutableTreeNode("User");
NodeProjekte = new DefaultMutableTreeNode( "Projekte");
NodeAllgemein = new DefaultMutableTreeNode( "Allgemein" );
DefaultMutableTreeNode projectA = new DefaultMutableTreeNode("Projekt A");
DefaultMutableTreeNode projectB = new DefaultMutableTreeNode("Projekt B");
DefaultMutableTreeNode LinkProjectA = new DefaultMutableTreeNode("Öffnen");
DefaultMutableTreeNode LinkProjektB = new DefaultMutableTreeNode("Öffnen");
DefaultMutableTreeNode NotizProjektA = new DefaultMutableTreeNode("Notiz");
DefaultMutableTreeNode NoitzProjektB = new DefaultMutableTreeNode("Notiz");
DefaultMutableTreeNode Zeitstrahl = new DefaultMutableTreeNode("Zeitplan");
DefaultMutableTreeNode ToDo = new DefaultMutableTreeNode("ToDo");
NodeProjekte.add( projectA );
NodeProjekte.add( projectB );
projectA.add(LinkProjectA);
projectA.add(NotizProjektA);
projectB.add(LinkProjektB);
projectB.add(NoitzProjektB);
NodeAllgemein.add(Zeitstrahl);
NodeAllgemein.add(ToDo);
NodeRoot.add( NodeProjekte );
NodeRoot.add( NodeAllgemein );
return NodeRoot;
}
private static void addItem(String StrProjektLink, String StrProjektName){
DefaultMutableTreeNode NodeNewProject = new DefaultMutableTreeNode(StrProjektName);
DefaultMutableTreeNode NodeNewProjectLink = new DefaultMutableTreeNode("Link");
DefaultMutableTreeNode NodeNewProjectNotiz = new DefaultMutableTreeNode("Notiz");
NodeProjekte.add(NodeNewProject);
NodeNewProject.add(NodeNewProjectLink);
NodeNewProject.add(NodeNewProjectNotiz);
}
private static void deleteItem(){
}
private static void updateTree(){
}
private void addProjectFolder()
{
addItem("C:/Users/", "User-Directory"a);
}
}
现在我想在树中添加或删除子项后更新JTree模型。但Eclipse说,我的所有变量和函数都必须是静态的,所以我不能更新元素。什么是最简单的更新JTree的方法?
答案 0 :(得分:0)
您的问题与Swing或JTree无关,而与静态与实例字段和方法的使用和滥用有关。在我看来,最好的解决方案是将所有字段设置为私有实例字段,将所有方法设置在实例方法之上,除了您的initWindow()
方法。然后解决方案就会失效。
// !! make everything private and nothing static
// also follow Java naming conventions
private TreeNode treeNodeRoot = createInitTree();
private JTree tree = new JTree(treeNodeRoot);
private DefaultMutableTreeNode nodeRoot;
private DefaultMutableTreeNode nodeProjekte;
private DefaultMutableTreeNode nodeAllgemein;
private class AddAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String message = "Enter node name:";
String nodeName = JOptionPane.showInputDialog(MainWindow.this, message);
// ...
DefaultMutableTreeNode parentNode = null;
TreePath parentPath = tree.getSelectionPath();
// ....
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(nodeName);
int index = parentNode.getChildCount();
DefaultTreeModel treeModel = (DefaultTreeModel) tree.getModel();
treeModel.insertNodeInto(childNode, parentNode, index);
}
}