Hello and welcome everyone, this is my first question so I hope that it is a good one. I was exploring the swing API and I came across a question that popped in my head. I basically asked my self if I could build a program that could use a while()
loop and display multiple JTextArea's like you could in the console like this:
while(x<100){
System.out.println("This is the number: " + x)
x++;
}
I want this to print in the JFrame enter code here but I can't seem to figure out how. I'm attempting to use JTextArea's but I really don't think that is the right approach to it. I tried labels but that did not work at all. Here is the source code.
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Container;
public class MainFrame extends JFrame{
public static int x=0;
public static int y = 0;
MainFrame(String title){
super(title);
// Set Layout
setLayout(new BorderLayout());
while(x<100){
y++;
x++;
System.out.println(x);
Container pane= getContentPane();
JTextArea x = new TextArea("Wateva" + y);
JButton button= new JButton("Wateva man");
pane.add(button, BorderLayout.SOUTH);
pane.add(x);
}
}
}
In the console x is shown incrementing by 1 each time, meaning the loop runs correctly. The only explanation that I can make out as a beginner programer is that it creates the JTextArea but then it realizes that x has been updated and so it overrides the old JTextArea with a new one and it does this for every number until it gets to 100. I think I am using the wrong type of JComponent but that is why I'm here. So if anyone can give me tips or a solution on how to fix, this would be much appreciated.