Wrap text in a text area using a checkbox

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) {
                    //To change body of implemented methods use File | Settings | File Templates.
                    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?

+3
source share
4 answers

Thread s. , Swing EDT. . Concurrency Swing.

chkSwing.addItemListener(
  new ItemListener() {
    @Override
    public void itemStateChanged(ItemEvent e) {
       keyField.setLineWrap( e.getStateChange() == ItemEvent.SELECTED );
    }
  } );

.

+5

. ( , , .)

TextAreaWrapChooser

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

class TextAreaWrapChooser {

    private JTextArea text = new JTextArea(8,30);

    TextAreaWrapChooser() {
        final JScrollPane scroll = new JScrollPane(text);

        JToolBar tb = new JToolBar();

        JCheckBox wrap = new JCheckBox("Line wrap", false);
        tb.add(wrap);
        wrap.addItemListener(new ItemListener() {
            // this method is called on the EDT
            public void itemStateChanged(ItemEvent ie) {
                boolean doWrap = ie.getStateChange() == ItemEvent.SELECTED;
                System.out.println("Wrap text: " + doWrap);
                text.setLineWrap( doWrap );
            }
        });

        // fill the text area
        try {
            File f = new File("TextAreaWrapChooser.java");
            FileReader fr = new FileReader(f);
            text.read( fr, f );
        } catch(Exception weTried) {
        }

        JPanel gui = new JPanel(new BorderLayout(2,2));
        gui.add(tb, BorderLayout.NORTH);
        gui.add(scroll, BorderLayout.CENTER);

        JOptionPane.showMessageDialog(null, gui);
    }

    public static void main(String[] args) {
        // construct/start the GUI on the EDT.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new TextAreaWrapChooser();
            }
        });
    }
}
+6

? SwingUtilities.invokeLater()?

+4

Swing , , EventDispatchThread.

, - SwingUtilities.invokeLater().

Swing, EventDispatchThread. , setLineWrap .

+4

All Articles