Reputation: 390
I'm writing a program that contains many buttons and when I click one of them, I want it to open a JTextArea with some txt file info in it. I tried to add the JTextArea to the button but it didn't open anything. I'm new in Java so I don't know if this is the correct approach to take.
I defined the button and JTextArea in the GUI
showListButton.add(jTextArea);
And here is the code I wrote inside of public void actionPerformed(ActionEvent e)
if(e.getSource() == showListButton){
try {
BufferedReader in = new BufferedReader(new FileReader("file.txt"));
String word;
while((word= in.readLine()) != null)
{
jTextArea.read( in, null );
jTextTextArea.requestFocus();
}
in.close();
}catch(IOException e1){
e1.printStackTrace();
}
}
Upvotes: 0
Views: 236
Reputation: 2347
To display something you need a root container like a Window
or a Jframe
. To
display your textarea you have add it in another frame that you will display on click or add it an already displayed panel.
Upvotes: 0