Java - Llenar un jtree desde mysql

   
Vista:

Llenar un jtree desde mysql

Publicado por Fox (1 intervención) el 19/12/2014 13:41:55
Hola, este es mi primer post aca... estoy empezando a desarrollar una app en java swing, y estoy complicado tratando de colocar datos en un jtree. El problema lo tengo al querer hacer otro nodo en el arbol... esta es la vista de donde saco los datos.

1
2
3
4
ID	CP	Localidad 	"Cod Area"	Provincia       Pais           	"Cod DDI"
4	2000	Rosario     	0341	       "Santa Fe"	Argentina  	+054
5	2200	"San Lorenzo"	03471	       "Santa Fe"	Argentina       +054
6	5000	Cordoba	        0351        	Cordoba    	Argentina  	+054


Este codigo funciona, pero no se como agregar otro nodo que sea hijo de Provincia para agregar las localidades....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
public class PaisProvLoc_Informe extends javax.swing.JFrame {
 
    /**
     * Creates new form PaisProvLoc_Informe
     */
    Conexion conexion = new Conexion();
    Connection con = conexion.abrirConexion();
    Statement st = null;
 
    public PaisProvLoc_Informe() {
 
        initComponents();
        llena_Tree();
 
    }
 
    public final void llena_Tree() {
 
        try {
            ArrayList list = new ArrayList();
            list.add("Arbol Pais / Provincia / Localidades");
            st = con.createStatement();
            ResultSet rs = st.executeQuery("SELECT `Pais` FROM monarca.view_localidades;");
            while (rs.next()) {
                Object value[] = {rs.getString(1)};
                list.add(value);
            }
            Object hierarchy[] = list.toArray();
            DefaultMutableTreeNode root = processHierarchy(hierarchy);
            DefaultTreeModel treeModel = new DefaultTreeModel(root);
            jTree.setModel(treeModel);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,"Error"+ e.getMessage());
        }
 
    }
 
    public DefaultMutableTreeNode processHierarchy(Object[] hierarchy) {
 
        DefaultMutableTreeNode node = new DefaultMutableTreeNode(hierarchy[0]);
        try {
            int ctrow = 0;
            int i = 0;
            try {
                ResultSet rs1 = st.executeQuery("SELECT `Pais`, `Cod DDI` FROM monarca.view_localidades GROUP BY `Pais`, `Cod DDI`;");
                while (rs1.next()) {
                    ctrow = rs1.getRow();
                }
                String L1Pais[] = new String[ctrow];
                String L1CodDDI[] = new String[ctrow];
                ResultSet rs2 = st.executeQuery("SELECT `Pais`, `Cod DDI` FROM monarca.view_localidades GROUP BY `Pais`, `Cod DDI`;");
                while (rs2.next()) {
                    L1Pais[i] = rs2.getString("Pais");
                    L1CodDDI[i] = rs2.getString("Cod DDI");
                    i++;
                }
 
                DefaultMutableTreeNode Node1, Node2;
                for (int Node1Index = 0; Node1Index < L1Pais.length; Node1Index++) {
                    Node1 = new DefaultMutableTreeNode(L1Pais[Node1Index]+" "+L1CodDDI[Node1Index]);
                    node.add(Node1);
                    ResultSet rs5 = st.executeQuery("SELECT `Provincia` FROM monarca.view_localidades where Pais= '" + L1Pais[Node1Index] + "' "
                            + "GROUP BY `Provincia`");
                    while (rs5.next()) {
                        Node2 = new DefaultMutableTreeNode(rs5.getString("Provincia"));
                        Node1.add(Node2);
                    }
                }
            } catch (Exception e) { }
        } catch (Exception e) { }
        return (node);
    }
    /**
     * 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
 
        jPanel = new javax.swing.JPanel();
        jScrollPane = new javax.swing.JScrollPane();
        jTree = new javax.swing.JTree();
 
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Vista en Pantalla");
 
        jPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
 
        jScrollPane.setViewportView(jTree);
 
        javax.swing.GroupLayout jPanelLayout = new javax.swing.GroupLayout(jPanel);
        jPanel.setLayout(jPanelLayout);
        jPanelLayout.setHorizontalGroup(
            jPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 684, Short.MAX_VALUE)
        );
        jPanelLayout.setVerticalGroup(
            jPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelLayout.createSequentialGroup()
                .addComponent(jScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 6, Short.MAX_VALUE))
        );
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 56, Short.MAX_VALUE))
        );
 
        pack();
    }// </editor-fold>                        
 
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PaisProvLoc_Informe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PaisProvLoc_Informe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PaisProvLoc_Informe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PaisProvLoc_Informe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
 
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PaisProvLoc_Informe().setVisible(true);
            }
        });
    }
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder