Java - Obtener datos de un JTextArea

 
Vista:
Imágen de perfil de John Bayron
Val: 5
Ha aumentado su posición en 4 puestos en Java (en relación al último mes)
Gráfica de Java

Obtener datos de un JTextArea

Publicado por John Bayron (3 intervenciones) el 06/05/2020 03:19:54
Buenos días/tardes/noches, he estado intentando definirle información a un JTextArea pero no he podido hacer que aparezca en la interfaz grafica. He usado tanto el .setText(), como el .append() pero por más que utilizo diferentes forma de definir texto, no logro hacer que aparezca en la pantalla. ¿Alguna ayuda o sugerencia?
Subire el código a través de este medio y también el proyecto. De ante mano muchas gracias.

CLASE main-------------------------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
package lector_y_escritor;
 
public class main {
 
    public static void main (String [] args){
 
 Marco marco = new Marco();
 marco.setVisible(true);
 
    }
}

CLASE Marco-------------------------------------------------------------------------------------
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
package lector_y_escritor;
 
import javax.swing.*;
import java.awt.*;
 
public class Marco extends JFrame {
 
    public JPanel panel;
    public JTextArea area;
    public JTextField escritor, lector;
    public JLabel etiquetaEscritor, etiquetaLector, etiquetaFondo;
    public JScrollPane barra;
    public JButton ejecutar, borrar;
 
    public Marco(){
      setSize(500,500);
      setLocationRelativeTo(null);
      setResizable(false);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setTitle("LECTORES Y ESCRITORES");
 
      interfaz();
    }
 
    public void interfaz(){
        panel();
        areaTexto();
        campos();
        etiqueta();
        botones();
        fondo();
 
    }
 
    public void panel(){
        panel = new JPanel();
        panel.setLayout(null);
        this.getContentPane().add(panel);
    }
 
    public void areaTexto(){
        area = new JTextArea();
        area.setFont(new Font("Arial",0,20));
        area.setEditable(false);
        barra = new JScrollPane(area);
        barra.setBounds(10,150,460,300);
        panel.add(barra);
    }
 
    public void etiqueta(){
        etiquetaEscritor = new JLabel("Ingresar la número de escritores");
        etiquetaEscritor.setBounds(10,0,300,50);
        etiquetaEscritor.setForeground(Color.WHITE);
        etiquetaEscritor.setFont(new Font("arial", 1,14));
        panel.add(etiquetaEscritor);
 
        etiquetaLector = new JLabel("Ingresar la número de lectores");
        etiquetaLector.setBounds(10,60,300,50);
        etiquetaLector.setForeground(Color.WHITE);
        etiquetaLector.setFont(new Font("arial", 1,14));
        panel.add(etiquetaLector);
    }
 
    public void campos(){
        escritor = new JTextField("");
        escritor.setForeground(Color.black);
        escritor.setFont(new Font("arial", 1,25));
        escritor.setBounds(10,35,50,40);
        panel.add(escritor);
 
        lector = new JTextField("");
        lector.setForeground(Color.black);
        lector.setFont(new Font("arial", 1,25));
        lector.setBounds(10,100,50,40);
        panel.add(lector);
    }
 
    public void botones(){
        ImageIcon botonCalcularImg = new ImageIcon("iniciar.png");
        ejecutar = new JButton();
        ejecutar.setBounds(260, 20, 92, 92);
        ejecutar.setIcon(botonCalcularImg);
        ejecutar.setContentAreaFilled(false);
        ejecutar.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        panel.add(ejecutar);
 
        ejecutar.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent eventoCalcular) {
                botonCalcularAccion(eventoCalcular);
            }
        });
 
        ImageIcon botonBorrarImg = new ImageIcon("borrar.png");
        borrar = new JButton();
        borrar.setBounds(360, 20, 92, 92);
        borrar.setIcon(botonBorrarImg);
        borrar.setContentAreaFilled(false);
        borrar.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        panel.add(borrar);
/*
        borrar.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent eventoCalcular) {
                botonBorrarAccion(eventoCalcular);
            }
        });*/
    }
 
    public void botonCalcularAccion(java.awt.event.ActionEvent eventoCalcular){
 
        int escritores = Integer.parseInt(escritor.getText());
        int lectores = Integer.parseInt(lector.getText());
 
        ControladorIngresoBD gestor = new ControladorIngresoBD();
        Escritor[] escritor = new Escritor[escritores];
        Lector[] lector = new Lector[lectores];
 
        for (int i = 0; i < escritor.length; i++){
            escritor[i] = new Escritor(gestor,i);
        }
        for (int i = 0; i < lector.length; i++){
            lector[i] = new Lector(gestor,i);
        }
 
        for (int i = 0; i < escritor.length; i++){
            escritor[i].start();
        }
        for (int i = 0; i < lector.length; i++){
            lector[i].start();
        }
    }
 
    public void fondo(){
        etiquetaFondo = new JLabel(new ImageIcon("fondo.png"));
        etiquetaFondo.setBounds(0, 0, 500, 500);
        panel.add(etiquetaFondo);
    }
 
    public void mostrar(String msg){
        area.setText(msg);
    }
}

CLASE ControladorIngresoBD-----------------------------------------------------------------------
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
package lector_y_escritor;
 
import java.sql.SQLOutput;
 
public class ControladorIngresoBD {
 
    private int numLectores =0;
    private boolean hayEscritor = false;
    private int numEscritores = 0;
    Marco marco = new Marco();
    String msg = "\n";
 
    public synchronized void openL(int id) throws InterruptedException{
        while (hayEscritor || numEscritores > 0){
 
            wait();
        }
        numLectores ++;
        msg += "Lector " +id+ " entra a la base de datos\n";
       // marco.mostraar(msg);
        marco.mostrar(msg);
 
    }
 
    public synchronized void closeL(int id){
        msg += "Lector " +id+ " sale de la base de datos\n";
        System.out.println(msg);
        numLectores--;
        if(numLectores == 0) notifyAll();
        //marco.mostraar(msg);
       // return msg;
    }
 
    public synchronized void openE(int id) throws InterruptedException{
        numEscritores ++;
        while (hayEscritor || numLectores > 0){
            wait();
        }
        hayEscritor = true;
        String msg ="Escritor " +id+ " entra a la base de datos";
        System.out.println(msg);
       // marco.mostraar(msg);
    }
    public synchronized void closeE(int id){
        numEscritores--;
        String msg = "Escritor " +id+ " sale de la base de datos";
        hayEscritor = false;
        notifyAll();
        System.out.println(msg);
     //   marco.mostraar(msg);
    }
 
}

CLASE Escritor---------------------------------------------------------------------------------------------------
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
package lector_y_escritor;
 
import java.util.*;
public class Escritor extends Thread{
 
    private ControladorIngresoBD gestor;
    private int id;
    private static Random rd = new Random();
    private final int DELAY=5000;
 
    public Escritor (ControladorIngresoBD gestor, int id){
        this.gestor = gestor;
        this.id = id;
    }
 
    public void run(){
        while (true){
 
            try {
                gestor.openE(id);
                Thread.sleep(rd.nextInt(5000));
                gestor.closeE(id);
                Thread.sleep(rd.nextInt(5000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
 
            //leyendo la base de datos
 
 
        }
    }
 
}

CLASE Lector------------------------------------------------------------------------------------
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
package lector_y_escritor;
import java.util.*;
public class Lector extends Thread{
 
    private ControladorIngresoBD gestor;
    private int id;
    private static Random rd = new Random();
 
 
    public Lector (ControladorIngresoBD gestor, int id){
        this.gestor = gestor;
        this.id = id;
 
    }
 
    public void run (){
        while (true){
 
            try {
                gestor.openL(id);
                Thread.sleep(rd.nextInt(5000));
                gestor.closeL(id);
                Thread.sleep(rd.nextInt(5000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
 
            //leyendo la base de datos
 
 
        }
    }
 
}
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
Imágen de perfil de Franklin
Val: 456
Bronce
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Obtener datos de un JTextArea

Publicado por Franklin (179 intervenciones) el 06/05/2020 14:45:35
Tienes tu PSVM( public static void main) en el cual tienes un objeto Marco y lo haces visible

1
2
3
4
5
6
7
8
public class main {
 
    public static void main (String [] args){
 
 Marco marco = new Marco();
marco.setVisible(true);
 
////////
y por otro lado la clase "ControladorIngresoBD" creas otro objeto diferente de la clase Marco


Por lo cual, la información recibida y enviada mediante la clase ControladorIngresoBD es otra instancia totalmente diferente a la de tu PSVM.

si gustas intenta lo siguiente para comprobar

En tu clase Marco en el metodo botonCalcularAccion(). Intenta hacer Visible el marco que está en ControladorIngresoBD, para ello debes definirlo como public.

Yo te recomendaría que cuando definas ControladorIngresoBD le envies al constructor el Objeto Marco que estas usando, por lo que trabajarías con la misma instancia.


Algo así:

1
2
3
4
5
6
7
8
9
10
11
12
public class ControladorIngresoBD {
 
    private int numLectores =0;
    private boolean hayEscritor = false;
    private int numEscritores = 0;
    Marco marco;
    String msg = "\n";
 
 
public ControladorIngresoBD (Marco marcox){
this.marco = marcox;
}


y en tu botonCalcularAccion ()

1
2
3
4
5
6
public void botonCalcularAccion(java.awt.event.ActionEvent eventoCalcular){
 
    int escritores = Integer.parseInt(escritor.getText());
    int lectores = Integer.parseInt(lector.getText());
 
    ControladorIngresoBD gestor = new ControladorIngresoBD(this); // para que utilices el marco el cual has hecho visible



espero se pueda entender :/
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