Java - Llenar un jTextArea al presionar un boton

 
Vista:
sin imagen de perfil

Llenar un jTextArea al presionar un boton

Publicado por Alejandro (16 intervenciones) el 07/02/2018 01:19:21
Tengo este problema y no encuentro forma de solucionarlo, tengo un jframe con un textArea y lo que hago al presionar un boton, es generar una pila y pasarla a un arreglo, y quiero que dicho arreglo se muestre en el textArea, sin embargo no puedo hacerlo ya que los métodos son privados y no puedo llamar al arreglo generado. ¿Alguna idea de cómo hacerlo?

Este es el código del jFrame donde quiero mostrar el arreglo

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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
public class MenuPila extends javax.swing.JFrame {
 
    /**
     * Creates new form Menu
     */
    public MenuPila() {
        initComponents();
    }
 
    /**
     * 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() {
 
        jLabel1 = new javax.swing.JLabel();
        pilaarreglo = new javax.swing.JButton();
        pilasimple = new javax.swing.JButton();
        pilacircular = new javax.swing.JButton();
        pilacola = new javax.swing.JButton();
        pilalistade = new javax.swing.JButton();
        textopila = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        textotraslado = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        regresarpila = new javax.swing.JButton();
 
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 
        jLabel1.setFont(new java.awt.Font("Times New Roman", 3, 13)); // NOI18N
        jLabel1.setText("Se realizará un traslado desde una Pila a:");
 
        pilaarreglo.setText("Arreglo");
        pilaarreglo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pilaarregloActionPerformed(evt);
            }
        });
 
        pilasimple.setText("Lista Simple");
 
        pilacircular.setText("Lista Circular");
 
        pilacola.setText("Cola");
 
        pilalistade.setText("Lista Doblemente Enlazada");
 
        textopila.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textopilaActionPerformed(evt);
            }
        });
 
        jLabel2.setText("Pila original");
 
        jLabel3.setText("Traslado");
 
        regresarpila.setText("REGRESAR");
        regresarpila.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                regresarpilaActionPerformed(evt);
            }
        });
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(58, 58, 58)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(pilalistade)
                            .addComponent(pilacola)
                            .addComponent(pilacircular)
                            .addComponent(pilasimple)
                            .addComponent(pilaarreglo))
                        .addGap(99, 99, 99)
                        .addComponent(textopila, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(textotraslado, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(29, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(73, 73, 73)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3)
                        .addGap(74, 74, 74))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(regresarpila)
                .addGap(270, 270, 270))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(30, 30, 30)
                            .addComponent(jLabel1))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel2)))
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pilaarreglo)
                        .addGap(18, 18, 18)
                        .addComponent(pilasimple)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(pilacircular)
                        .addGap(18, 18, 18)
                        .addComponent(pilacola)
                        .addGap(18, 18, 18)
                        .addComponent(pilalistade))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(textotraslado)
                            .addComponent(textopila))))
                .addGap(50, 50, 50)
                .addComponent(regresarpila)
                .addContainerGap(51, Short.MAX_VALUE))
        );
 
        pack();
    }// </editor-fold>                        
 
    public void mostrarmenu() {
        Interfaz i = new Interfaz();
        i.setVisible(true);
        i.setLocationRelativeTo(null);
    }
    private void regresarpilaActionPerformed(java.awt.event.ActionEvent evt) {
        mostrarmenu();
        this.setVisible(false);
 
    }
    Traslados t = new Traslados();
    Nodo n = new Nodo();
    private void pilaarregloActionPerformed(java.awt.event.ActionEvent evt) {
 
        n = t.insercionInicioListaDE(n);
        int[] arreglo = t.pilaArreglo(n);
        textopila.setText("");
 
 
    }
 
    private void textopilaActionPerformed(java.awt.event.ActionEvent evt) {
            textopila.setText(arreglo); //Aquí me sale error porque no encuentra la variable arreglo
 
    }
 
    /**
     * @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(MenuPila.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MenuPila.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MenuPila.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MenuPila.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
 
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MenuPila().setVisible(true);
            }
        });
    }
 
    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JButton pilaarreglo;
    private javax.swing.JButton pilacircular;
    private javax.swing.JButton pilacola;
    private javax.swing.JButton pilalistade;
    private javax.swing.JButton pilasimple;
    private javax.swing.JButton regresarpila;
    private javax.swing.JTextField textopila;
    private javax.swing.JTextField textotraslado;
    // End of variables declaration                   
}


Y este es el código que genera el arreglo a partir de la pila

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public int[] pilaArreglo(Nodo pila) {
    Nodo temp2 = pila;
    int n = 0;
    while (temp2 != null) {
        n = n + 1;
        temp2 = temp2.sig;
    }
    int arreglo[] = new int[n];
    temp2 = pila;
    System.out.println("El arreglo generado a partir de la pila es: \n");
    for (int i = 0; i < n; i++) {
        arreglo[i] = temp2.num;
        System.out.println(arreglo[i] + "");
        temp2 = temp2.sig;
    }
    System.out.println("");
    return arreglo;
}
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
sin imagen de perfil

Llenar un jTextArea al presionar un boton

Publicado por Juan Francisco (58 intervenciones) el 07/02/2018 09:45:21
No pero podrias pobra en vez de declararte la variable dentro del metodo ..declaratela fuera como haces ..

1
2
3
4
5
6
7
8
9
10
Traslados t = new Traslados();
Nodo n = new Nodo();
asi ->int[] arreglo;
private void pilaarregloActionPerformed(java.awt.event.ActionEvent evt) {
 
    n = t.insercionInicioListaDE(n);
    arreglo = t.pilaArreglo(n);->Asi podre usarla desde cualquier metodo de mi clase y desde fuera por que sino lo pones modificador son publicas por defecto
    textopila.setText("");
 
}

Y creo que podras acceder depues en este metodo a ella

private void textopilaActionPerformed(java.awt.event.ActionEvent evt) {
textopila.setText(arreglo); //Aquí esta variable ya si que existe o es de ambito global , de la otra forma es solo de ambito local al acabar el metodo ya no existe..

}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar