当前位置: 技术问答>java相关
JBuilder高手请进,JTree中删除了一个结点然后聚焦在它的父结点上该怎么办?最好还能触发valueChanged事件,在线等待...?
来源: 互联网 发布时间:2015-07-07
本文导语: JBuilder高手请进,JTree中删除了一个结点然后聚焦在它的父结点上该怎么办?最好还能触发valueChanged事件? | 罪魁祸首就是 selparent.remove(selnd); 使用了这一条之后必须使用 treeModel.reload(); 来刷新...
JBuilder高手请进,JTree中删除了一个结点然后聚焦在它的父结点上该怎么办?最好还能触发valueChanged事件?
|
罪魁祸首就是
selparent.remove(selnd);
使用了这一条之后必须使用
treeModel.reload();
来刷新整个树
而这样selection和expand状态就完全改变了。
请参考下边的例子:
使用
_model.removeNodeFromParent(node);
来删除一个节点:
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame {
JTree _tree = new JTree();
DefaultTreeModel _model = (DefaultTreeModel)_tree.getModel();
TreeSelectionModel _selectionModel = _tree.getSelectionModel();
JButton _removeButton = new JButton("Remove selected node");
public Test()
{
Container contentPane = getContentPane();
_selectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
contentPane.add(new ControlPanel(), BorderLayout.NORTH);
contentPane.add(_tree, BorderLayout.CENTER);
_tree.addTreeSelectionListener(new TreeSelectionListener(){
public void valueChanged(TreeSelectionEvent e)
{
TreePath path = e.getNewLeadSelectionPath();
boolean nodesAreSelected = (path != null);
_removeButton.setEnabled(nodesAreSelected);
}
});
}
class ControlPanel extends JPanel
{
public ControlPanel()
{
_removeButton.setEnabled(false);
add(_removeButton);
_removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreePath path =
_selectionModel.getSelectionPath();
if(path.getPathCount() == 1) {
JOptionPane.showMessageDialog(ControlPanel.this,
"Can't remove root node!");
return;
}
MutableTreeNode node =
(MutableTreeNode)path.getLastPathComponent();
TreePath parentPath = path.getParentPath();
_model.removeNodeFromParent(node);
_tree.setSelectionPath(parentPath);
}
});
}
}
public static void main(String args[]) {
GraphicJavaApplication.launch(new Test(),
"_tree Model Example",300,300,450,300);
}
}
class GraphicJavaApplication extends WindowAdapter
{
public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h)
{
f.setTitle(title);
f.setBounds(x,y,w,h);
f.setVisible(true);
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.addWindowListener(new WindowAdapter()
{
public void windowClosed(WindowEvent e)
{
System.exit(0);
}
});
}
}
selparent.remove(selnd);
使用了这一条之后必须使用
treeModel.reload();
来刷新整个树
而这样selection和expand状态就完全改变了。
请参考下边的例子:
使用
_model.removeNodeFromParent(node);
来删除一个节点:
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame {
JTree _tree = new JTree();
DefaultTreeModel _model = (DefaultTreeModel)_tree.getModel();
TreeSelectionModel _selectionModel = _tree.getSelectionModel();
JButton _removeButton = new JButton("Remove selected node");
public Test()
{
Container contentPane = getContentPane();
_selectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
contentPane.add(new ControlPanel(), BorderLayout.NORTH);
contentPane.add(_tree, BorderLayout.CENTER);
_tree.addTreeSelectionListener(new TreeSelectionListener(){
public void valueChanged(TreeSelectionEvent e)
{
TreePath path = e.getNewLeadSelectionPath();
boolean nodesAreSelected = (path != null);
_removeButton.setEnabled(nodesAreSelected);
}
});
}
class ControlPanel extends JPanel
{
public ControlPanel()
{
_removeButton.setEnabled(false);
add(_removeButton);
_removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreePath path =
_selectionModel.getSelectionPath();
if(path.getPathCount() == 1) {
JOptionPane.showMessageDialog(ControlPanel.this,
"Can't remove root node!");
return;
}
MutableTreeNode node =
(MutableTreeNode)path.getLastPathComponent();
TreePath parentPath = path.getParentPath();
_model.removeNodeFromParent(node);
_tree.setSelectionPath(parentPath);
}
});
}
}
public static void main(String args[]) {
GraphicJavaApplication.launch(new Test(),
"_tree Model Example",300,300,450,300);
}
}
class GraphicJavaApplication extends WindowAdapter
{
public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h)
{
f.setTitle(title);
f.setBounds(x,y,w,h);
f.setVisible(true);
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.addWindowListener(new WindowAdapter()
{
public void windowClosed(WindowEvent e)
{
System.exit(0);
}
});
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。