Jtree

Résolu
cs_zazou1 Messages postés 48 Date d'inscription mardi 28 décembre 2004 Statut Membre Dernière intervention 23 juillet 2010 - 9 avril 2006 à 23:22
cs_zazou1 Messages postés 48 Date d'inscription mardi 28 décembre 2004 Statut Membre Dernière intervention 23 juillet 2010 - 10 avril 2006 à 19:31
salut tous le monde,
voila mon probleme ,je vous direz realiser un jtree que je recuper tous le contenu de Jtree pour envoyer ce resultat sur le reseau pour avoir la meme view.
si quelque a une idée n ' hesitez pas de ma informer .
Merci d'avance.

4 réponses

cs_zazou1 Messages postés 48 Date d'inscription mardi 28 décembre 2004 Statut Membre Dernière intervention 23 juillet 2010
10 avril 2006 à 19:31
salut,
mais ça une seul fois donc voila mon code:
bon 1ere c'est le serveur:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;


////************************serveur****************************//
public class TreeEdit
{
public static void main(String[] args)
{
JFrame frame = new TreeEditF();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
TreeEditF.yako();
}
}


/**
A frame with a tree and buttons to edit the tree.
*/
class TreeEditF extends JFrame
{ private static ServerSocket serveur;
private static Socket connexion;
private static ObjectOutputStream sortie;
private static ObjectInputStream entree;
public static void yako()
{
try{
serveur=new ServerSocket(5555);
connexion=serveur.accept();

entree=new ObjectInputStream(connexion.getInputStream());
try{
while(true){

TreeModel ka =(TreeModel)entree.readObject();

tree.setModel(ka);
kaci=null;
}
}
catch( ClassNotFoundException j)
{}
}
catch(IOException e)
{
}






}



public TreeEditF()
{
setTitle("TreeEditTest");
setSize(WIDTH, HEIGHT);


// construct tree


TreeNode root = makeSampleTree();
model = new DefaultTreeModel(root);
tree = new JTree(model);
tree.setEditable(true);


// add scroll pane with tree to content pane


JScrollPane scrollPane = new JScrollPane(tree);
getContentPane().add(scrollPane, BorderLayout.CENTER);


makeButtons();
// make button panel

}


public TreeNode makeSampleTree()
{
DefaultMutableTreeNode root
= new DefaultMutableTreeNode("World");
DefaultMutableTreeNode country
= new DefaultMutableTreeNode("USA");
root.add(country);
DefaultMutableTreeNode state
= new DefaultMutableTreeNode("California");
country.add(state);
DefaultMutableTreeNode city
= new DefaultMutableTreeNode("San Jose");
state.add(city);
city = new DefaultMutableTreeNode("Cupertino");
state.add(city);
state = new DefaultMutableTreeNode("Michigan");
country.add(state);
city = new DefaultMutableTreeNode("Ann Arbor");
state.add(city);
country = new DefaultMutableTreeNode("Germany");
root.add(country);
state = new DefaultMutableTreeNode("Schleswig-Holstein");
country.add(state);
city = new DefaultMutableTreeNode("Kiel");
state.add(city);
return root;
}


/**
Makes the buttons to add a sibling, add a child, and
delete a node.
*/
public void makeButtons()
{
JPanel panel = new JPanel();
JButton addSiblingButton = new JButton("Add Sibling");
addSiblingButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
DefaultMutableTreeNode selectedNode
= (DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();


if (selectedNode == null) return;


DefaultMutableTreeNode parent
= (DefaultMutableTreeNode)
selectedNode.getParent();


if (parent == null) return;


DefaultMutableTreeNode newNode
= new DefaultMutableTreeNode("New");


int selectedIndex = parent.getIndex(selectedNode);
model.insertNodeInto(newNode, parent,
selectedIndex + 1);

// now display new node

TreeNode[] nodes = model.getPathToRoot(newNode);
TreePath path = new TreePath(nodes);
tree.scrollPathToVisible(path);
}
});
panel.add(addSiblingButton);


JButton addChildButton = new JButton("Add Child");
addChildButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
DefaultMutableTreeNode selectedNode
= (DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();


if (selectedNode == null) return;


DefaultMutableTreeNode newNode
= new DefaultMutableTreeNode("New");
model.insertNodeInto(newNode, selectedNode,
selectedNode.getChildCount());


// now display new node

TreeNode[] nodes = model.getPathToRoot(newNode);
TreePath path = new TreePath(nodes);
tree.scrollPathToVisible(path);
}
});
panel.add(addChildButton);


JButton deleteButton = new JButton("Delete");
deleteButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
DefaultMutableTreeNode selectedNode
= (DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();


if (selectedNode != null &&
selectedNode.getParent() != null)
model.removeNodeFromParent(selectedNode);
}
});
panel.add(deleteButton);
getContentPane().add(panel, BorderLayout.SOUTH);
}


private DefaultTreeModel model;
private static JTree tree;
private static final int WIDTH = 400;
private static final int HEIGHT = 200;
}

/**********************************le client********************/
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;


/**
This program demonstrates tree editing.
*/
public class client
{
public static void main(String[] args)
{
JFrame frame = new TreeEdi();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
TreeEdi.ya();
}
}


/**
A frame with a tree and buttons to edit the tree.
*/
class TreeEdi extends JFrame
{ ;
private static Socket client;
private static ObjectOutputStream sorti;
private static ObjectInputStream entre;
public static void ya()
{
try{
client =new Socket("127.0.0.1",5555);

sorti=new ObjectOutputStream( client.getOutputStream());
sorti.flush();
entre=new ObjectInputStream( client.getInputStream());
try{
TreeModel kaci =(TreeModel)entre.readObject();
tree.setModel(kaci);
}
catch( ClassNotFoundException j)
{}
}
catch(IOException e)
{
}






}



public TreeEdi()
{
setTitle("TreeEditTest");
setSize(WIDTH, HEIGHT);


// construct tree


TreeNode root = makeSampleTree();
model = new DefaultTreeModel(root);
tree = new JTree(model);
tree.setEditable(true);


// add scroll pane with tree to content pane


JScrollPane scrollPane = new JScrollPane(tree);
getContentPane().add(scrollPane, BorderLayout.CENTER);


makeButtons();
// make button panel

}


public TreeNode makeSampleTree()
{
DefaultMutableTreeNode root
= new DefaultMutableTreeNode("World");
DefaultMutableTreeNode country
= new DefaultMutableTreeNode("USA");
root.add(country);
DefaultMutableTreeNode state
= new DefaultMutableTreeNode("California");
country.add(state);
DefaultMutableTreeNode city
= new DefaultMutableTreeNode("San Jose");
state.add(city);
city = new DefaultMutableTreeNode("Cupertino");
state.add(city);
state = new DefaultMutableTreeNode("Michigan");
country.add(state);
city = new DefaultMutableTreeNode("Ann Arbor");
state.add(city);
country = new DefaultMutableTreeNode("Germany");
root.add(country);
state = new DefaultMutableTreeNode("Schleswig-Holstein");
country.add(state);
city = new DefaultMutableTreeNode("Kiel");
state.add(city);
return root;
}


/**
Makes the buttons to add a sibling, add a child, and
delete a node.
*/
public void makeButtons()
{
JPanel panel = new JPanel();
JButton addSiblingButton = new JButton("Add Sibling");
addSiblingButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
DefaultMutableTreeNode selectedNode
= (DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();


if (selectedNode == null) return;


DefaultMutableTreeNode parent
= (DefaultMutableTreeNode)
selectedNode.getParent();


if (parent == null) return;


DefaultMutableTreeNode newNode
= new DefaultMutableTreeNode("New");


int selectedIndex = parent.getIndex(selectedNode);
model.insertNodeInto(newNode, parent,
selectedIndex + 1);

// now display new node

TreeNode[] nodes = model.getPathToRoot(newNode);
TreePath path = new TreePath(nodes);
tree.scrollPathToVisible(path);
try{
sorti.writeObject( model );
sorti.flush();
}
catch(IOException e)
{}
}
});
panel.add(addSiblingButton);


JButton addChildButton = new JButton("Add Child");
addChildButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
DefaultMutableTreeNode selectedNode
= (DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();


if (selectedNode == null) return;


DefaultMutableTreeNode newNode
= new DefaultMutableTreeNode("New");
model.insertNodeInto(newNode, selectedNode,
selectedNode.getChildCount());


// now display new node

TreeNode[] nodes = model.getPathToRoot(newNode);
TreePath path = new TreePath(nodes);
tree.scrollPathToVisible(path);
try{
sorti.writeObject( model );
sorti.flush();
}
catch(IOException e)
{}
}
});
panel.add(addChildButton);


JButton deleteButton = new JButton("Delete");
deleteButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
DefaultMutableTreeNode selectedNode
= (DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();


if (selectedNode != null &&
selectedNode.getParent() != null)
model.removeNodeFromParent(selectedNode);
try{
sorti.writeObject( model );
sorti.flush();
}
catch(IOException e)
{}
}
});
panel.add(deleteButton);
getContentPane().add(panel, BorderLayout.SOUTH);
}


private DefaultTreeModel model;
private static JTree tree;
private static final int WIDTH = 400;
private static final int HEIGHT = 200;
}
3
Nicoschmeii Messages postés 16 Date d'inscription jeudi 6 avril 2006 Statut Membre Dernière intervention 30 juin 2008
10 avril 2006 à 00:43
Pour qu'on puisse t'aider, essaye de mieux te formuler parce que là, je ne comprend absolument rien.
0
bloofi Messages postés 388 Date d'inscription mercredi 1 octobre 2003 Statut Membre Dernière intervention 3 mai 2006 2
10 avril 2006 à 00:43
Coucou,

essaye de récuperer le treemodel, ou alors creer ton DefaultTreeModelque tu applique au jtree et c'est cet objet la que tu pourra envoyer sur le reseau.

a l'arrivée tu fait
jtree.setModel( monTreeModel) ;
0
bloofi Messages postés 388 Date d'inscription mercredi 1 octobre 2003 Statut Membre Dernière intervention 3 mai 2006 2
10 avril 2006 à 00:43
arf j'ai oublié de dire : Vive le MVC ^^
0
Rejoignez-nous