I have recreated the failure of a larger program into a simpler one. Thus, suppose the following four classes :
(1) To create the frame or window:
public class Marco2 extends JFrame{
public Marco2() {
setSize(800,600);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(200,200);
Lamina2 lamina = new Lamina2();
add(lamina);
}
}
(2) To create the sheet or panel:
public class Lamina2 extends JPanel implements ActionListener{
private JTextArea casilla;
public void paintComponent(Graphics g) {
casilla = new JTextArea();
casilla.setBounds(592, 95, 177, 400);
add(casilla);
JButton boton = new JButton("EJECUTAR");
boton.setBounds(592, 65, 177, 20);
add(boton);
boton.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Algoritmo2 alg = new Algoritmo2();
alg.algoritmo(casilla);
}
}
(3) The algorithm to execute:
public class Algoritmo2 {
public void algoritmo(JTextArea casilla) {
String resultado = "Hola\nMundo";
casilla.setText(resultado);
}
}
(4) The executable :
public class Ejecutable2 {
public static void main(String[] args) {
Marco2 miMarco = new Marco2();
miMarco.setVisible(true);
}
}
All classes are from the same package and have everything needed imported. When I run the program, I get this:
This is an empty window: it only has the textArea on the right and the RUN button just above it . When you press this button, the following happens:
It does not write in the defined area , but above and centered. Why? Thanks.
The problem seems to me to be that you overridden the method
paintComponent(Graphics)
, which will be called when the interface is "repainted", which happens when you click the button.I recommend you check out this Oracle tutorial on
paint()
andpaintComponent(Graphics)
.It does not write in the defined area, but above and centered. Why?
That is an interesting question. What happens is that
paintComponent()
it is invoked several times (and/or you clicked the button several times), counting them specifically is not easy for me, I am not an expert in java swing. But what I can tell you is that internally when there is an action that requires the component to update its display, the parent callspaint()
and/orrepaint()
which in turn calls (among other methods)paintComponent()
, and that makes you see the buttons repeated and the text areas. You can probably see this better if you try to gradually make the window bigger or smaller. The component's view must be updated when you do this, which will result in multiple calls topaintComponent()
.Regarding why it does not appear where you want, it has to do with the fact that
paintComponent()
you are creating the text area that you had before and even more, the reference of the previous one is being replaced with the new one (note: this in languages without GC would be a memory leak, java has GC and in this case it is not very "important", but that does not mean that we do not care about memory management). That and the sequence in which the update of the view and the writing in the text area are executed, give you the behavior you say: it does not write in the defined area, but above and centered .One way to fix it is to simply not add the components inside the
paintComponents()
. The Netbeans GUI builder does it in the methodinitComponents()
I hope I've helped.