Ver Mensaje Individual
  #5 (permalink)  
Antiguo 10/03/2005, 18:18
COCOL
 
Fecha de Ingreso: marzo-2005
Mensajes: 27
Antigüedad: 19 años, 10 meses
Puntos: 0
Aqui esta el codigo... solamente es una prueba... no creas que es el programa en donde quiero implementarlo jajaja


import java.io.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;

public class TreeSave extends javax.swing.JFrame {

/** Creates new form TreeSave */
public TreeSave() {
initComponents();
jTree.getModel().addTreeModelListener(new TreeModelChangeEvent());
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
jTree = new javax.swing.JTree();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();

getContentPane().setLayout(null);

setDefaultCloseOperation(javax.swing.WindowConstan ts.DISPOSE_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

jTree.setEditable(true);
getContentPane().add(jTree);
jTree.setBounds(20, 10, 220, 200);

jButton1.setText("Save");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

getContentPane().add(jButton1);
jButton1.setBounds(30, 250, 57, 23);

jButton2.setText("Load");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

getContentPane().add(jButton2);
jButton2.setBounds(150, 250, 57, 23);

java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize ();
setBounds((screenSize.width-481)/2, (screenSize.height-441)/2, 481, 441);
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("C://Tree.stc"));
//TreeModel treeModel = jTree.getModel();

DefaultTreeModel loadedTreeModel = (DefaultTreeModel)ois.readObject();

jTree.setModel(loadedTreeModel);

ois.close();
}catch (IOException ioe){
System.out.println("error al cargar archivo "+ioe.getMessage());
}catch (ClassNotFoundException cnfe){
System.out.println("Clase no encontrada "+cnfe.getMessage());
}

}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("C://Tree.stc"));
DefaultMutableTreeNode root = ((DefaultMutableTreeNode)jTree.getModel().getRoot( ));

root.add(new DefaultMutableTreeNode(new Objeto("un objeto")));

((DefaultTreeModel)jTree.getModel()).reload();

oos.writeObject((DefaultTreeModel)jTree.getModel() );
oos.flush();
oos.close();
jTree.updateUI();

}catch(IOException ioe){
System.out.println("error al salvar archivo "+ioe.getMessage());
}
}

/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new TreeSave().show();

}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JTree jTree;
// End of variables declaration


class Objeto implements Serializable{
public String Nombre;
public int Edad = 39;
public char sexo = 'm';

public Objeto(String Nombre){
this.Nombre = Nombre;
}
public String toString(){
return Nombre;
}
}
}