Oppure

Loading
01/07/12 11:50
Franck
Ciao sapete dirmi come posso realizzare una cosa del genere. java2s.com/Tutorial/JavaImages/…
aaa
01/07/12 13:46
Premoli
Ciao!!!

Ti basta usare un JTree.
aaa
01/07/12 14:17
Franck
Se faccio come dici tu devo partire sempre da un punto principale chiamato root.
Invece io non voglio partire da un punto principale.
aaa
01/07/12 16:10
Premoli
ah, allora non avevo capito cosa volevi fare (magari la prossima volta sii più esplicito)...

Comunque che io sappia i JTree possono avere una sola radice...
puoi, tuttavia, provare a simulare il comportamento che vuoi provando a nascondere la radice... con il comando
tree.setRootVisible(false);


in questo modo sono riuscito ad ottenere questo:

non so se è quello che volevi (altrimenti potresti provare a dare un occhio alle SWT)
aaa
01/07/12 17:23
Franck
Mi potresti postare il tuo esempio... ;)

P.s.
Come posso fare ad intercettare quando un utente prendendo esempio dal tuo programma clicca su "a" o su "dance".
aaa
01/07/12 17:58
Premoli
public Frame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setSize(800, 480);
		
	nodeText = new JTextField(15);
		
	contentPane = new JPanel();
	contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
	setContentPane(contentPane);
	contentPane.setLayout(new BorderLayout(0, 0));
		
	JTree tree = new JTree();
	tree.setRootVisible(false);
	tree.setModel(new DefaultTreeModel(
		new DefaultMutableTreeNode("") {
			{
				DefaultMutableTreeNode node;
				node = new DefaultMutableTreeNode("numeri");
					node.add(new DefaultMutableTreeNode("1"));
					node.add(new DefaultMutableTreeNode("2"));
					node.add(new DefaultMutableTreeNode("3"));
				add(node);
				node = new DefaultMutableTreeNode("lettere");
					node.add(new DefaultMutableTreeNode("a"));
					node.add(new DefaultMutableTreeNode("b"));
					node.add(new DefaultMutableTreeNode("c"));
				add(node);
				node = new DefaultMutableTreeNode("musica");
					node.add(new DefaultMutableTreeNode("dance"));
					node.add(new DefaultMutableTreeNode("classic"));
					node.add(new DefaultMutableTreeNode("jazz"));
				add(node);
			}
		}
	));
	contentPane.add(tree, BorderLayout.WEST);
	contentPane.add(nodeText, BorderLayout.NORTH);
		
	tree.addTreeSelectionListener(new TreeListener());
		
	setVisible(true);
}
	
public class TreeListener implements TreeSelectionListener {
	public void valueChanged(TreeSelectionEvent e) {
		nodeText.setText("Hai cliccato su: "+e.getNewLeadSelectionPath());
	}
}


Ma l'ho scritto solo per vedere il risultato non badare troppo al codice...
Ti ho messo anche un esempio di come intercettare gli eventi...
Ora modificalo per adattarlo alle tue necessità.
Comunque per queste cose potresti benissimo consultare la documentazione...
aaa
02/07/12 13:57
Franck
Ti spiego quello che voglio fare...
img201.imageshack.us/img201/9406/…
1)Vorrei togliere la cartella principale cioè root
2)Cliccando ad esempio sul nodo "Paolo Pietro" vorrei che avvenisse un evento del tipo aprire una finestra che già ho fatto ma non so come intercettare l'evento
3)Cliccando su Altro o su Girl non avendo nessun sotto nodo non avvenga niente


Ti posto il mio codice:
package chat;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.sql.*;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

public final class NewClass extends JFrame {
    
private Function function = new Function();
private JPanel p = new JPanel(null);
private JScrollPane pane = new JScrollPane();
private static String info2;
private Connection dbconn;
private ArrayList<Integer> id_group = new ArrayList<Integer>();
private ArrayList<DefaultMutableTreeNode> group = new ArrayList<DefaultMutableTreeNode>();
private int id = 1; 
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
JTree tree = new JTree(root);

public static void main(String[] args){
NewClass f = new NewClass();
f.setTitle("New");
f.setSize(600,600);
f.setMinimumSize(new Dimension(600,600)); 
f.setLocationRelativeTo(null);
f.setVisible(true);
}
    
    
public NewClass(){
try{
String url = "jdbc:odbc:chat";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
dbconn = DriverManager.getConnection(url);
info2 = "Connection successful\n";
}catch(ClassNotFoundException cnfex){
info2 = info2+"Connection unsuccessful\n" + cnfex.toString();
}catch(SQLException sqlex){
info2 = info2+"Connection unsuccessful\n" +sqlex.toString();
}catch(Exception excp){
info2 = info2+excp.toString();
}
      
try{
Statement statement = dbconn.createStatement();
String query = "SELECT * FROM group_s WHERE id_user = "+id+" ORDER BY name ASC";
ResultSet rs = statement.executeQuery(query);
while(rs.next()){
int code = rs.getInt("id");
id_group.add(code);
}
statement.close();
}catch(SQLException sqlex){
}

for(int i=0;i<id_group.size();i++){
group.add(new DefaultMutableTreeNode(function.Group(id_group.get(i))));

try{
Statement statement = dbconn.createStatement();
String query = "SELECT * FROM group_user WHERE id_group = "+id_group.get(i);
ResultSet rs = statement.executeQuery(query);
while(rs.next()){
int code = rs.getInt("id_user");
group.get(i).add(new DefaultMutableTreeNode(function.User_date(code,"name")+" "+function.User_date(code,"surname")));
}
statement.close();
}catch(SQLException sqlex){
}
root.add(group.get(i));
}

//tree.setRootVisible(false);
pane.getViewport().add(tree);
p.add(pane);
pane.setBounds(0,0,300,300);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(p,BorderLayout.CENTER);
}
    
}

Ultima modifica effettuata da Franck 02/07/12 14:00
aaa
02/07/12 14:42
Premoli
Scusa, ma ti ho spiegato come fare, devi semplicemente scrivere:
tree.setRootVisible(false);

per gli eventi ti ho fatto vedere un piccolo esempio ora si tratta solo di adattare il tutto a quello che vuoi fare.

Non posso mettermi a fare l'esercizio al posto tuo, anche perché tu poi cosa impareresti?
aaa