1
C, C++, C#, Java, Visual Basic, HTML, PHP, CSS, Javascript, Ajax, Joomla, MySql y más / crear un jtree con una tabla recursiva en java?
« en: 13 de Junio 2014, 23:46 »
Saludos comunidad otra vengo con una duda, se que en este lugar podre solucionar , tengo lo sguiente tengo una bd con una tabla que es recursiva y quiero que realizando consultas al mismo me cargue al arbol, la tabla es la sigt
y esta es con la que dibujo el jtree en un jframe
.
esto en realidad deberia resultar
directorio
------+ general
------+ administrativo
----------- contabilidad
pero me dibuja todo en uno sin jerarquias
directorio
general
administrativo
contabilidad
Código: [Seleccionar]
CREATE TABLE estructura (
estructura_id int(11) NOT NULL,
nombre varchar(70) COLLATE utf8_bin NOT NULL,
descripcion varchar(70) COLLATE utf8_bin DEFAULT NULL,
depende_id int(11) NOT NULL,
PRIMARY KEY (estructura_id),
FOREIGN KEY (depende_id) REFERENCES estructura (estructura_id),
Ahora tengo un jtree donde realizando una consulta me carga todo los datos de la tabla y no asi creando padres y sus hijos, esta es la clase donde realiza la consultaCódigo: [Seleccionar]
public Object[][] select_normal(String table,String campo,String where,String condicion)
{
int registros =0;
String colunombre[]=campo.split(",");
String sql="SELECT "+campo+" from "+table;
String sql2="select count(*) as total from "+table;
if(where !=null || condicion !=null)
{ sql+=" where "+where+" group by "+condicion;
sql2+=" where "+where;
}
System.out.println(sql);
try
{ PreparedStatement presta=conex.prepareStatement(sql2);
ResultSet res=presta.executeQuery();
res.next();
registros=res.getInt("total");
res.close();
}catch(SQLException e)
{
System.out.print(e);
}
Object[][] data= new String[registros][campo.split(",").length];
try
{ PreparedStatement pst=conex.prepareStatement(sql);
ResultSet res=pst.executeQuery();
int i=0;
while(res.next())
{
for(int j=0;j<=campo.split(",").length-1;j++)
{
data[i][j]=res.getString(colunombre[j].trim());
}
i++;
}
res.close();
}catch(SQLException e)
{
System.out.print(e);
}
return data;
}
y esta es con la que dibujo el jtree en un jframe
Código: [Seleccionar]
public class Principal extends JFrame {
private conexion c=new conexion();
private DefaultTreeModel modelo;
public Principal() {
Object [][] query_nodos_principales=c.select_normal("estructura as e, estructura as e1"," e.nombre,e.estructura_id,e.depende_id ","e.estructura_id=e1.depende_id");
if(query_nodos_principales.length>0)
{
for(int i=0;i<query_nodos_principales.length;i++)
{
DefaultMutableTreeNode prueba =new DefaultMutableTreeNode(query_nodos_principales[i][0]);
Directorio.add(prueba);
modelo=new DefaultTreeModel(Directorio);
JTree tree = new JTree(modelo);
tree.setVisibleRowCount(15);
tree.setFont(new Font("Consolas", Font.BOLD | Font.ITALIC, 17));
}
}
}
espero y pueden colaborarme en ejemplo el siguiente.
estructura_id | | nombre | | depende_id | |
0 | | directorio | | 0 |
1 | | general | | 0 |
2 | | administrativo | | 1 |
3 | | contabilidad | | 2 |
directorio
------+ general
------+ administrativo
----------- contabilidad
pero me dibuja todo en uno sin jerarquias
directorio
general
administrativo
contabilidad