I created a dialog box in Swing for editing data. It contains JTextAreatwo copies JButton( OKand Cancel) and JCheckBox( Wrap Text). I would like to make the text in the text area be wrapped whenever the user clicks on this check box. Originally, my text is wrapped using setLineWrap(true).
I am using the following code:
Runnable r1=new Runnable() {
@Override
public void run() {
System.out.println("True");
keyField.setLineWrap(true);
keyField.requestFocus();
}
};
Runnable r2=new Runnable() {
@Override
public void run() {
System.out.println("FALSE");
keyField.setLineWrap(false);
keyField.repaint();
keyField.requestFocus();
}
};
final Thread t1=new Thread(r1) ;
final Thread t2=new Thread(r2);
final JCheckBox chkSwing = new JCheckBox("Word Wrap",true);
chkSwing.addItemListener(
new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
t1.start();
} else if (e.getStateChange() != ItemEvent.SELECTED){
t2.start();
}
}
});
panel.add(chkSwing);
Problem
The problem is that as soon as I uncheck the box, the text will be unpacked, but again checking the box does not complete the text. The console indicates that the thread is being called. How to configure a checkbox to enable / disable text wrapping in the text area?
source
share