当前位置: 技术问答>java相关
两棵树之间的同步大家做过吗?高分求知!
来源: 互联网 发布时间:2015-02-08
本文导语: 情况是我有两棵同样的树,我想在jTree1选择时同步选择jTree2的同样的结点。 所以我在jTree1的valueChange方法中处理, TreePath tp = jTree1.getSelectionPath(); jTree2.setSelectionPath(tp); jTree2.makeVisible(tp); 但是jTree2似乎一...
情况是我有两棵同样的树,我想在jTree1选择时同步选择jTree2的同样的结点。
所以我在jTree1的valueChange方法中处理,
TreePath tp = jTree1.getSelectionPath();
jTree2.setSelectionPath(tp);
jTree2.makeVisible(tp);
但是jTree2似乎一点变化也没有,如何做?请高手指点一下吧。
其实jTree2已选中了相应的结点,我在jTree2的选中事件中如果输出它当前被选中的结点,的确是与jTree1选中的一样。说明jTree2被改变了,但是界面上不会有变化,为什么?
我试过 expandSelectedPath,invalidate,等等方法,都没用。
|
try it
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
public class Learn extends JFrame {
JTree tree1 = new JTree();
JTree tree2 = new JTree();
public Learn(){
tree1.addTreeExpansionListener(new TreeExpansionAction1());
tree2.addTreeExpansionListener(new TreeExpansionAction2());
ScrollPane sPaneLeft = new ScrollPane();
ScrollPane sPaneRight = new ScrollPane();
this.getContentPane().add(sPaneLeft,BorderLayout.WEST);
this.getContentPane().add(sPaneRight,BorderLayout.EAST);
sPaneLeft.add(tree1);
sPaneRight.add(tree2);
this.pack();
this.setVisible(true);
}
class TreeExpansionAction1 implements TreeExpansionListener{
public void treeCollapsed(TreeExpansionEvent event) {
TreePath path = event.getPath();
System.out.println("tree 1 collapsed : " + path.toString());
}
public void treeExpanded(TreeExpansionEvent event) {
TreePath path = event.getPath();
System.out.println("tree 1 expanded : " + path.toString());
int row = tree1.getRowForPath(path);
tree2.expandRow(row);
}
}
class TreeExpansionAction2 implements TreeExpansionListener{
public void treeCollapsed(TreeExpansionEvent event) {
TreePath path = event.getPath();
System.out.println("tree 2 collapsed : " + path.toString());
}
public void treeExpanded(TreeExpansionEvent event) {
TreePath path = event.getPath();
System.out.println("tree 2 expanded : " + path.toString());
int row = tree2.getRowForPath(path);
tree1.expandRow(row);
}
}
public static void main(String[] args) {
new Learn();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
public class Learn extends JFrame {
JTree tree1 = new JTree();
JTree tree2 = new JTree();
public Learn(){
tree1.addTreeExpansionListener(new TreeExpansionAction1());
tree2.addTreeExpansionListener(new TreeExpansionAction2());
ScrollPane sPaneLeft = new ScrollPane();
ScrollPane sPaneRight = new ScrollPane();
this.getContentPane().add(sPaneLeft,BorderLayout.WEST);
this.getContentPane().add(sPaneRight,BorderLayout.EAST);
sPaneLeft.add(tree1);
sPaneRight.add(tree2);
this.pack();
this.setVisible(true);
}
class TreeExpansionAction1 implements TreeExpansionListener{
public void treeCollapsed(TreeExpansionEvent event) {
TreePath path = event.getPath();
System.out.println("tree 1 collapsed : " + path.toString());
}
public void treeExpanded(TreeExpansionEvent event) {
TreePath path = event.getPath();
System.out.println("tree 1 expanded : " + path.toString());
int row = tree1.getRowForPath(path);
tree2.expandRow(row);
}
}
class TreeExpansionAction2 implements TreeExpansionListener{
public void treeCollapsed(TreeExpansionEvent event) {
TreePath path = event.getPath();
System.out.println("tree 2 collapsed : " + path.toString());
}
public void treeExpanded(TreeExpansionEvent event) {
TreePath path = event.getPath();
System.out.println("tree 2 expanded : " + path.toString());
int row = tree2.getRowForPath(path);
tree1.expandRow(row);
}
}
public static void main(String[] args) {
new Learn();
}
}
|
感谢gtlang78()的提示,如下代码实现了利用model和treepath来完成同步的动作
注意构造函数的第一行,将两个tree的model设为相同,以后就很方便的实现了两棵树的同步。
我觉得这种方法才真正解决了sharetop(天生不笨)的问题,建议给gtlang78()加分
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
public class Tree extends JFrame {
JTree tree1 = new JTree();
JTree tree2 = new JTree();
public Tree(){
tree2.setModel(tree1.getModel());
tree1.addTreeExpansionListener(new TreeExpansionAction1());
tree2.addTreeExpansionListener(new TreeExpansionAction2());
ScrollPane sPaneLeft = new ScrollPane();
ScrollPane sPaneRight = new ScrollPane();
this.getContentPane().add(sPaneLeft,BorderLayout.WEST);
this.getContentPane().add(sPaneRight,BorderLayout.EAST);
sPaneLeft.add(tree1);
sPaneRight.add(tree2);
this.pack();
this.setVisible(true);
}
class TreeExpansionAction1 implements TreeExpansionListener{
public void treeCollapsed(TreeExpansionEvent event) {
TreePath path = event.getPath();
System.out.println("tree 1 collapsed : " + path.toString());
tree2.collapsePath(path);
}
public void treeExpanded(TreeExpansionEvent event) {
TreePath path = event.getPath();
System.out.println("tree 1 expanded : " + path.toString());
tree2.expandPath(path);
}
}
class TreeExpansionAction2 implements TreeExpansionListener{
public void treeCollapsed(TreeExpansionEvent event) {
TreePath path = event.getPath();
System.out.println("tree 2 collapsed : " + path.toString());
tree1.collapsePath(path);
}
public void treeExpanded(TreeExpansionEvent event) {
TreePath path = event.getPath();
System.out.println("tree 2 expanded : " + path.toString());
tree1.expandPath(path);
}
}
public static void main(String[] args) {
new Tree();
}
}
注意构造函数的第一行,将两个tree的model设为相同,以后就很方便的实现了两棵树的同步。
我觉得这种方法才真正解决了sharetop(天生不笨)的问题,建议给gtlang78()加分
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
public class Tree extends JFrame {
JTree tree1 = new JTree();
JTree tree2 = new JTree();
public Tree(){
tree2.setModel(tree1.getModel());
tree1.addTreeExpansionListener(new TreeExpansionAction1());
tree2.addTreeExpansionListener(new TreeExpansionAction2());
ScrollPane sPaneLeft = new ScrollPane();
ScrollPane sPaneRight = new ScrollPane();
this.getContentPane().add(sPaneLeft,BorderLayout.WEST);
this.getContentPane().add(sPaneRight,BorderLayout.EAST);
sPaneLeft.add(tree1);
sPaneRight.add(tree2);
this.pack();
this.setVisible(true);
}
class TreeExpansionAction1 implements TreeExpansionListener{
public void treeCollapsed(TreeExpansionEvent event) {
TreePath path = event.getPath();
System.out.println("tree 1 collapsed : " + path.toString());
tree2.collapsePath(path);
}
public void treeExpanded(TreeExpansionEvent event) {
TreePath path = event.getPath();
System.out.println("tree 1 expanded : " + path.toString());
tree2.expandPath(path);
}
}
class TreeExpansionAction2 implements TreeExpansionListener{
public void treeCollapsed(TreeExpansionEvent event) {
TreePath path = event.getPath();
System.out.println("tree 2 collapsed : " + path.toString());
tree1.collapsePath(path);
}
public void treeExpanded(TreeExpansionEvent event) {
TreePath path = event.getPath();
System.out.println("tree 2 expanded : " + path.toString());
tree1.expandPath(path);
}
}
public static void main(String[] args) {
new Tree();
}
}
|
可以让两棵树使用同一个选择模型,只需两行代码
TreeSelectionModel model = jTree1.getSelectionModel();
jTree2.setSelectionModel(model);
还有很多其他的Swing组件可以用类似的方法同步,例如JSlider,JProgressBar,JList等
TreeSelectionModel model = jTree1.getSelectionModel();
jTree2.setSelectionModel(model);
还有很多其他的Swing组件可以用类似的方法同步,例如JSlider,JProgressBar,JList等