Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping text in text-area using a checkbox

I created a dialog box in Swing for editing data. It contains a JTextArea, two JButton instances (OK & Cancel) and a JCheckBox (Wrap Text). What I wanted to do is to have the text in the text area wrapped whenever the user clicks on the check-box. I initially have the text wrapped by 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);

The Problem

The problem is that once I deselect the check-box, the text gets unwrapped, but again checking the check-box does not wrap the text again. The console shows that the thread is being called. How to make the check-box work for setting/unsetting the word wrap behavior of the text-area?

like image 274
Sachin Verma Avatar asked Dec 02 '22 00:12

Sachin Verma


1 Answers

Here is working code. (With a cute animated screenshot with a comment that high-lights something that several answers missed pointing out.)

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();
            }
        });
    }
}
like image 151
Andrew Thompson Avatar answered Dec 04 '22 01:12

Andrew Thompson