Topic: 如何还原 当前JTree的状态? |
Print this page |
1.如何还原 当前JTree的状态? | Copy to clipboard |
Posted by: k_k_grass Posted on: 2003-03-26 21:02 /*1.run the program,and double click the child01 node and rename it which takes with carriage return 2.click the save button to save it; 3.I wanna get back the previous information with click open button,even i try to get it back,but it won't response to the TreeSelectionEvent,that is also my concern here... thanks advance for any of your idea */ import java.util.*; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.tree.*; import javax.swing.*; import javax.swing.event.*; class Tree1 extends JFrame implements Serializable{ JTree tree; Hashtable h; JPanel panel; JScrollPane scroll; JButton btnOpen,btnSave; JLabel lblStatus; int count=0;//child tree node counts; String[] s1={"child"+count}; static Tree1 treeApp; public Tree1(){ //initialize the tree which constructed with hashtable h=new Hashtable(); h.put("root",s1); tree=new JTree; tree.setEditable(true); panel=new JPanel(); scroll=new JScrollPane(tree); btnOpen=new JButton("open"); btnSave=new JButton("save"); lblStatus=new JLabel(); tree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); if (node == null) return; Object nodeInfo = node.getUserObject(); lblStatus.setForeground(Color.red); lblStatus.setText(nodeInfo.toString()+"Selected"); validate();//re-layout & repaint repaint(); } }); btnOpen.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ scroll.remove(tree); ObjectInputStream is=null; try{ is=new ObjectInputStream(new FileInputStream(new File("os.txt"))); Hashtable hB=(Hashtable)is.readObject(); JTree treeB=(JTree)is.readObject(); scroll.add(tree; treeB.setRootVisible(true); is.close(); validate(); repaint(); JOptionPane.showConfirmDialog(null,"opened the previous' tree","open",JOptionPane.PLAIN_MESSAGE); } catch(Exception et){System.out.println("wrong opening object");} } }); btnSave.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ ObjectOutputStream os=null; try{ os=new ObjectOutputStream(new FileOutputStream(new File("os.txt"))); os.writeObject; os.writeObject(tree); JOptionPane.showConfirmDialog(null,"saved this tree","save",JOptionPane.PLAIN_MESSAGE); } catch(Exception e){System.out.println("error write object");} finally{ try{ os.close(); } catch(Exception e){System.out.println("error on close");} } } }); setContentPane(panel); panel.setLayout(new BorderLayout()); panel.add(scroll,BorderLayout.WEST); JPanel btnPanel=new JPanel(); btnPanel.add(btnOpen); btnPanel.add(btnSave); panel.add(btnPanel,BorderLayout.NORTH); panel.add(lblStatus,BorderLayout.SOUTH); setSize(300,400); show(); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public static void main(String[] args){ treeApp=new Tree1(); } } |
2.Re:如何还原 当前JTree的状态? [Re: k_k_grass] | Copy to clipboard |
Posted by: bujinwang Posted on: 2003-03-27 11:18 Hi, Attached please see the working version of your code. Some suggestions: 1) Be really careful with your code style; 2)Be really carefull with code reuse; Some pionts: 1) In order for the new tree(treeB) to work, you have to clearly remove what's on the scrollpane; 2) You have to initialize your tree again. Serialization won't save it's selection listener. If you have further question, let me know. [code] import java.io.File ; import java.io.FileInputStream ; import java.io.FileOutputStream ; import java.io.ObjectInputStream ; import java.io.ObjectOutputStream ; import java.io.Serializable ; import java.util.Hashtable ; import java.awt.BorderLayout ; import java.awt.Color ; import java.awt.event.ActionEvent ; import java.awt.event.ActionListener ; import java.awt.event.WindowAdapter ; import java.awt.event.WindowEvent ; import javax.swing.JButton ; import javax.swing.JFrame ; import javax.swing.JLabel ; import javax.swing.JOptionPane ; import javax.swing.JPanel ; import javax.swing.JScrollPane ; import javax.swing.JTree ; import javax.swing.event.TreeSelectionEvent ; import javax.swing.event.TreeSelectionListener ; import javax.swing.tree.DefaultMutableTreeNode ; class Tree1 extends JFrame implements Serializable { JTree tree ; Hashtable h ; JPanel panel ; JScrollPane scroll ; JButton btnOpen , btnSave ; JLabel lblStatus ; int count = 0 ; //child tree node counts; String[] s1 = {"child" + count} ; static Tree1 treeApp ; TreeSelectionListener listener = new TreeSelectionListener() { public void valueChanged ( TreeSelectionEvent e ) { DefaultMutableTreeNode node = ( DefaultMutableTreeNode ) tree.getLastSelectedPathComponent() ; if ( node == null ) return ; Object nodeInfo = node.getUserObject() ; lblStatus.setForeground( Color.red ) ; lblStatus.setText( nodeInfo.toString() + "Selected" ) ; validate() ; //re-layout & repaint repaint() ; } }; public Tree1 () { //initialize the tree which constructed with hashtable h = new Hashtable() ; h.put( "root" , s1 ) ; tree = new JTree() ; tree.setEditable( true ) ; panel = new JPanel() ; scroll = new JScrollPane( tree ) ; btnOpen = new JButton( "open" ) ; btnSave = new JButton( "save" ) ; lblStatus = new JLabel() ; tree.addTreeSelectionListener( listener); btnOpen.addActionListener( new ActionListener() { public void actionPerformed ( ActionEvent e ) { scroll.remove( tree ) ; ObjectInputStream is = null ; try { is = new ObjectInputStream( new FileInputStream( new File( "os.txt" ) ) ) ; JTree treeB = ( JTree ) is.readObject() ; scroll.removeAll(); scroll.add( treeB ) ; treeB.setRootVisible( true ) ; treeB.addTreeSelectionListener(listener); is.close() ; validate() ; repaint() ; JOptionPane.showConfirmDialog( null , "opened the previous' tree" , "open" , JOptionPane.PLAIN_MESSAGE ) ; } catch ( Exception et ) { et.printStackTrace(); } } } ) ; btnSave.addActionListener( new ActionListener() { public void actionPerformed ( ActionEvent event ) { ObjectOutputStream os = null ; try { os = new ObjectOutputStream( new FileOutputStream( new File( "os.txt" ) ) ) ; os.writeObject( tree ) ; JOptionPane.showConfirmDialog( null , "saved this tree" , "save" , JOptionPane.PLAIN_MESSAGE ) ; } catch ( Exception e ) { System.out.println( "error write object" ) ; } finally { try { os.close() ; } catch ( Exception e ) { System.out.println( "error on close" ) ; } } } } ) ; setContentPane( panel ) ; panel.setLayout( new BorderLayout() ) ; panel.add( scroll , BorderLayout.CENTER ) ; JPanel btnPanel = new JPanel() ; btnPanel.add( btnOpen ) ; btnPanel.add( btnSave ) ; panel.add( btnPanel , BorderLayout.NORTH ) ; panel.add( lblStatus , BorderLayout.SOUTH ) ; setSize( 300 , 400 ) ; show() ; this.addWindowListener( new WindowAdapter() { public void windowClosing ( WindowEvent e ) { System.exit( 0 ) ; } } ) ; } public static void main ( String[] args ) { treeApp = new Tree1() ; } } [/code] |
3.Re:如何还原 当前JTree的状态? [Re: k_k_grass] | Copy to clipboard |
Posted by: bujinwang Posted on: 2003-03-27 11:20
|
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |