Here is a class that uses a TA the way u want. Note that i have declared the texarea public static . That way this is accessible all over the application like: TAFrame.outputTA.append("\r\nNew line in my report");
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TAFrame extends JFrame {
BorderLayout bLay1 = new BorderLayout();
JScrollPane textScroll = new JScrollPane();
public static JTextArea outputTA = new JTextArea(); // <<<
JPanel btnPanel = new JPanel();
JButton closeBtn = new JButton();
JButton clearBtn = new JButton();
public TAFrame() {
initialize();
}
public static void main(String[] args) {
TAFrame tf = new TAFrame();
tf.setBounds(20,20,400,250);
tf.setVisible(true);
}
private void initialize() {
this.getContentPane().setLayout(bLay1);
outputTA.setToolTipText("This is my output");
outputTA.setLineWrap(true); // <--------------- here
outputTA.setWrapStyleWord(true);
closeBtn.setText("Close");
clearBtn.setText("Clear");
this.getContentPane().add(textScroll, BorderLayout.CENTER);
this.getContentPane().add(btnPanel, BorderLayout.SOUTH);
btnPanel.add(closeBtn, null);
btnPanel.add(clearBtn, null);
textScroll.getViewport().add(outputTA, null);
closeBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
closeBtn_actionPerformed(e);
}
});
clearBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
clearBtn_actionPerformed(e);
}
});
}
void closeBtn_actionPerformed(ActionEvent e) {
System.exit(0);
}
void clearBtn_actionPerformed(ActionEvent e) {
this.outputTA.setText("");
}
}
Bookmarks