问一个关于树刷新的问题,请大家帮忙!在线等候佳音! 根据数据库建成了一个目录树,想通过点击界面上的刷新按钮来实现数据库改变了点刷新按钮就能生成新的目录树这样的功能.请问刷新按钮的事件应该怎么写呢?请高手帮帮忙!解决后立即结分! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class TreeDemo extends JFrame implements ActionListener{....... public TreeDemo() { super("个人新闻"); String s=null; s2="shyh"; Container con=getContentPane(); JPanel p1=new JPanel(); JPanel p2=new JPanel(); final Container container = this.getContentPane(); top = new DefaultMutableTreeNode("个人新闻"); createNodes(top, "0");//创建根节点 treeModel = new DefaultTreeModel(top); tree = new JTree(treeModel);tree.setEditable(true); tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); tree.addTreeSelectionListener(new TreeSelectionListener() {public void valueChanged(TreeSelectionEvent e) { DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); if (node == null) return; Object nodeInfo = node.getUserObject(); if (node.isLeaf()) { NewsNode book = (NewsNode) nodeInfo; createNodes(node, book.id); container.validate(); } else { NewsNode newsNode = (NewsNode) nodeInfo; createNodes(node, newsNode.id); container.validate(); } if (DEBUG) { System.out.println(nodeInfo.toString()); } } }); ...... Icon icon3=new ImageIcon("D:/pic/sx.gif"); button6=new JButton(icon3);//刷新按钮 p1.setLayout(new GridLayout(3,5)); p1.add(label1);p1.add(new Label());p1.add(new Label());p1.add(new Label());p1.add(new Label()); p1.add(menubar);p1.add(new Label());p1.add(new Label());p1.add(new Label());p1.add(new Label()); p1.add(button4);p1.add(button5);p1.add(button6);p1.add(button7);p1.add(button8); p2.add(button2);p2.add(button3); JScrollPane scrollpane=new JScrollPane(tree); htmlPane=new JEditorPane(); htmlPane.setEditable(false); JScrollPane htmlView = new JScrollPane(htmlPane); JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,scrollpane,htmlView); con.add(p1,BorderLayout.NORTH); con.add(splitPane,BorderLayout.CENTER); con.add(p2,BorderLayout.SOUTH); } public void actionPerformed(ActionEvent e) { if (e.getSource()==button6)//点刷新按钮 { 请问该加那段呢? } } public DefaultMutableTreeNode addObject(Object child) { DefaultMutableTreeNode parentNode = null; TreePath parentPath = tree.getSelectionPath(); if (parentPath == null) { parentNode = top; } else { parentNode = (DefaultMutableTreeNode) (parentPath.getLastPathComponent()); } return addObject(parentNode, child, true); } public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent, Object child) { return addObject(parent, child, false); } public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent, Object child, boolean shouldBeVisible) { DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child); if (parent == null) { parent = top; } treeModel.insertNodeInto(childNode, parent, parent.getChildCount()); if (shouldBeVisible) { tree.scrollPathToVisible(new TreePath(childNode.getPath())); } return childNode; } private void createNodes(DefaultMutableTreeNode top, String id) { .......} public static void main(String[] args) { JFrame frame = new TreeDemo(); ..... }} 把构造函数里面的东西提出来,新建一个init函数,在actionPerformed中调用init函数。 关于java当中string用法的一个小疑问 请各位小试牛刀 关于继承问题 java 泛型 面试题没答出来 求助socket java中如何把String转换成int? 那里有java的好的电子书下载?如wrox等 问一个动态对象的问题 byte 和 char 一样吗? 在线等,立即给分!如何判断自己在系统上的绝对路径 关于效率测试的一个疑问,欢迎讨论
{.......
public TreeDemo()
{
super("个人新闻");
String s=null;
s2="shyh";
Container con=getContentPane();
JPanel p1=new JPanel();
JPanel p2=new JPanel();
final Container container = this.getContentPane();
top = new DefaultMutableTreeNode("个人新闻");
createNodes(top, "0");//创建根节点
treeModel = new DefaultTreeModel(top);
tree = new JTree(treeModel);tree.setEditable(true);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.addTreeSelectionListener(new TreeSelectionListener()
{public void valueChanged(TreeSelectionEvent e)
{
DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if (node == null)
return;
Object nodeInfo = node.getUserObject();
if (node.isLeaf())
{
NewsNode book = (NewsNode) nodeInfo;
createNodes(node, book.id);
container.validate();
}
else
{
NewsNode newsNode = (NewsNode) nodeInfo;
createNodes(node, newsNode.id);
container.validate();
}
if (DEBUG)
{
System.out.println(nodeInfo.toString());
}
}
});
......
Icon icon3=new ImageIcon("D:/pic/sx.gif");
button6=new JButton(icon3);//刷新按钮
p1.setLayout(new GridLayout(3,5));
p1.add(label1);p1.add(new Label());p1.add(new Label());p1.add(new Label());p1.add(new Label());
p1.add(menubar);p1.add(new Label());p1.add(new Label());p1.add(new Label());p1.add(new Label());
p1.add(button4);p1.add(button5);p1.add(button6);p1.add(button7);p1.add(button8);
p2.add(button2);p2.add(button3);
JScrollPane scrollpane=new JScrollPane(tree);
htmlPane=new JEditorPane();
htmlPane.setEditable(false);
JScrollPane htmlView = new JScrollPane(htmlPane);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,scrollpane,htmlView);
con.add(p1,BorderLayout.NORTH);
con.add(splitPane,BorderLayout.CENTER);
con.add(p2,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==button6)//点刷新按钮
{
请问该加那段呢?
}
}
public DefaultMutableTreeNode addObject(Object child)
{ DefaultMutableTreeNode parentNode = null;
TreePath parentPath = tree.getSelectionPath();
if (parentPath == null)
{ parentNode = top; }
else
{ parentNode = (DefaultMutableTreeNode) (parentPath.getLastPathComponent()); }
return addObject(parentNode, child, true);
}
public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent, Object child)
{ return addObject(parent, child, false); }
public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent, Object child, boolean shouldBeVisible)
{ DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);
if (parent == null)
{ parent = top; }
treeModel.insertNodeInto(childNode, parent, parent.getChildCount());
if (shouldBeVisible)
{ tree.scrollPathToVisible(new TreePath(childNode.getPath())); }
return childNode;
}
private void createNodes(DefaultMutableTreeNode top, String id)
{ .......} public static void main(String[] args)
{
JFrame frame = new TreeDemo();
.....
}
}