Reputation: 23
My JTextArea has text appended to it then it is updated with
jTexaArea.update(jTextArea.getGraphics());
But while appending I cannot select or edit text nor scroll. NetBeans system output allows for these features, how do I include them in my program?
Upvotes: 0
Views: 93
Reputation: 2453
While appending text if it is triggered by a JButton
click event, the UI becomes unresponsive till the event is finished. This is because the event dispatch thread waits for the event to get finished before proceeding with next event.
If your requirement is to work on Text Area when an event is getting processed you can choose following approaches:
Following is a sample lazy appending example:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import java.awt.Font;
import javax.swing.JScrollPane;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Date;
import java.util.List;
import javax.swing.Timer;
import javax.swing.text.DefaultCaret;
public class LazyAppender extends JFrame implements ActionListener {
/**
* Demonstrates two different ways of appending text into TextArea
*/
private static final long serialVersionUID = 1L;
JTextArea textArea1;
JTextArea textArea2;
public LazyAppender() {
initUI();
}
public final void initUI() {
GridLayout experimentLayout = new GridLayout(2,2);
JPanel panel = new JPanel();
getContentPane().add(panel);
panel.setLayout(experimentLayout);
//Button 1
JButton button1 = new JButton("Button 1");
button1.setToolTipText("Append JtextArea1 without using Swing Timer");
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
textArea1.append(getText());
textArea1.update(textArea1.getGraphics());
}
});
//Button 2
JButton button2 = new JButton("Button 2");
button2.setToolTipText("Lazy Append JtextArea2 using Swing Timer");
Timer timer2 = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
textArea2.append(getText());
}
});
timer2.setRepeats(false); //Execute only once
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
//Start timer, this will release the button immediately
timer2.start();
}
});
//TextArea 1
textArea1 = new JTextArea("This is an editable JtextArea1. " +
"A text area is a \"plain\" text component, " +
"which means that although it can display text " +
"in any font, all of the text is in the same font."
);
textArea1.setFont(new Font("Serif", Font.ITALIC, 16));
textArea1.setLineWrap(true);
textArea1.setWrapStyleWord(true);
//ScrollPane 1
JScrollPane areaScrollPane1 = new JScrollPane(textArea1);
areaScrollPane1.setViewportView(textArea1);
//TextArea 2
textArea2 = new JTextArea("This is an editable JtextArea2. " +
"A text area is a \"plain\" text component, " +
"which means that although it can display text " +
"in any font, all of the text is in the same font."
);
textArea2.setFont(new Font("Serif", Font.ITALIC, 16));
textArea2.setLineWrap(true);
textArea2.setWrapStyleWord(true);
//ScrollPane 2
JScrollPane areaScrollPane2 = new JScrollPane(textArea2);
areaScrollPane2.setViewportView(textArea2);
//Add Components into Panel
panel.add(button1);
panel.add(button2);
panel.add(areaScrollPane1);
panel.add(areaScrollPane2);
setTitle("Text Area Append Verify");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
}
public String getText(){
String stringToAppend = "This is your life. Do what you want and do it often." +
"If you don't like something, change it." +
"If you don't like your job, quit." +
"If you don't have enough time, stop watching TV." +
"If you are looking for the love of your life, stop; "+
"they will be waiting "+
"for you when you start doing things you love." +
"Stop over-analysing, life is simple." +
"All emotions are beautiful." +
"When you eat, appreciate every last bite." +
"Life is simple." +
"Open your heart, mind and arms to new things and people, "+
"we are united in our differences." +
"Ask the next person you see what their passion is and "+
"share your inspiring dream with them." +
"Travel often; getting lost will help you find yourself." +
"Some opportunities only come once, seize them." +
"Life is about the people you meet and the things you create "+
"with them, so go out and start creating." +
"Life is short, live your dream and wear your passion." +
"~ Holstee Manifesto, The Wedding Day ";
//Simulated delay
long start = new Date().getTime();
while(new Date().getTime() - start < 1000L){}
return stringToAppend;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
LazyAppender ex = new LazyAppender();
ex.setVisible(true);
}
});
}
}
Also, check this question JTextArea thread safe? for an in-depth discussion on whether its safe to JTextArea in multi-threaded environments.
Upvotes: 1