Example usage for javax.swing JTextArea JTextArea

List of usage examples for javax.swing JTextArea JTextArea

Introduction

In this page you can find the example usage for javax.swing JTextArea JTextArea.

Prototype

public JTextArea(int rows, int columns) 

Source Link

Document

Constructs a new empty TextArea with the specified number of rows and columns.

Usage

From source file:Main.java

public Main() {
    JTextArea area = new JTextArea(5, 20);
    area.setText("this is a test.");
    String charsToHighlight = "aeiouAEIOU";
    Highlighter h = area.getHighlighter();
    h.removeAllHighlights();//from  www .  j a  v a 2s.  com
    String text = area.getText().toUpperCase();
    for (int i = 0; i < text.length(); i += 1) {
        char ch = text.charAt(i);
        if (charsToHighlight.indexOf(ch) >= 0)
            try {
                h.addHighlight(i, i + 1, DefaultHighlighter.DefaultPainter);
            } catch (Exception ble) {
            }
    }
    this.getContentPane().add(area);

}

From source file:Main.java

public Main() {
    setPreferredSize(new Dimension(200, 250));
    console = new JTextArea(15, 15);

    vertical = new JScrollPane(console);
    vertical.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    add(vertical);/*from www  . j  av a 2  s. c  om*/
}

From source file:Main.java

public Main() {
    JPanel textPanel = new JPanel();
    textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.PAGE_AXIS));

    firstTextArea = new JTextArea(10, 50);
    secondTextArea = new JTextArea(10, 50);

    textPanel.add(new JScrollPane(firstTextArea));
    textPanel.add(new JScrollPane(secondTextArea));

    testFrame.add(textPanel, BorderLayout.CENTER);

    copyTextButton = new JButton("Copy text");
    copyTextButton.addActionListener(e -> copyText());
    testFrame.add(copyTextButton, BorderLayout.SOUTH);
}

From source file:Main.java

/** 
 * Adds a <code>JTextArea</code> with the specified text as a label
 * appearing before the text field to the given <code>JComponent</code>. 
 * Assuming the <code>JComponent</code> has the MigLayout set, the label
 * and text field are both left aligned within the <code>JComponent</code>.
 * If the wrap parameter is set to true, MigLayout's "wrap" attribute will be
 * applied to the <code>JTextField</code>, meaning the next component added
 * will appear on the next line.//w w  w.ja  v  a 2s.co m
 * (Exception: Will not work if MigLayout's flowy layout constraint is applied,
 * but it is rarely used MigLayout feature and thus not a common concern; however
 * if you have set this layout constraint on your <code>JComponent</code> do not
 * attempt to use the wrap option of this method.)
 * @param label - the text to appear in front of the text field
 * @param rows - the length in rows of the text field 
 * @param cols - the length in columns of the text field
 * @param wrap - indicates is this component should be the last on the current line
 * @return the <code>JTextArea</code> added to the component
 */
public static JTextArea addLabeledTextArea(JComponent c, String label, int rows, int cols, boolean wrap) {
    JLabel l = new JLabel(label);
    c.add(l, "align left");

    JTextArea text = new JTextArea(rows, cols);
    if (wrap) {
        c.add(text, "align left, wrap");
    } else {
        c.add(text, "align left");
    }

    return text;
}

From source file:Main.java

void initUI() {
    JFrame frame = new JFrame(Main.class.getSimpleName());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    textArea = new JTextArea(24, 80);
    save = new JButton("Save to file");
    save.addActionListener(e -> saveToFile());
    frame.add(new JScrollPane(textArea));
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(save);// www . java 2  s  .  c  o m
    frame.add(buttonPanel, BorderLayout.SOUTH);
    frame.setSize(500, 400);
    frame.setVisible(true);
}

From source file:ImageLoader.java

public ImageLoader() {
    super("Image Loader");

    this.log = new JTextArea(4, 4);
    this.viewer = new JPanel();

    JButton start = new JButton("Start");
    start.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String[] files = new String[] { "Bodie_small.png", "Carmela_small.png", "Death Valley_small.png",
                    "Lake_small.png" };
            new ImageLoadingWorker(log, viewer, files).execute();
        }/*from   www  . j av  a 2  s.c om*/
    });

    add(new JScrollPane(log), BorderLayout.NORTH);
    add(new JScrollPane(viewer), BorderLayout.CENTER);
    add(start, BorderLayout.SOUTH);

    setSize(360, 280);
}

From source file:Main.java

public Main() {
    Box b = Box.createHorizontalBox();
    b.add(new JScrollPane(t1));
    copy.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            t2.setText(t1.getSelectedText());
        }//w  w  w  .j  a v a2s .  co  m
    });
    b.add(copy);

    t2 = new JTextArea(10, 15);
    t2.setEditable(false);
    b.add(new JScrollPane(t2));

    add(b);
    setSize(425, 200);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

From source file:Main.java

public Main() {
    JPanel jp = new JPanel();
    jp.setLayout(new BorderLayout());
    final JTabbedPane tb = new JTabbedPane();
    JButton btn = new JButton("push me !!!");
    btn.addActionListener(e -> {/*from   w ww  .j a v a 2  s .  c o m*/
        tb.setEnabledAt(1, true);
        tb.setEnabledAt(2, true);
        tb.setEnabledAt(3, true);
        tb.setEnabledAt(4, true);
    });
    JPanel pnl = new JPanel();
    pnl.add(btn);
    tb.add("Tab1", pnl);
    tb.add("Tab2", new JTextArea(10, 20));
    tb.add("Tab3", new JTextArea(10, 20));
    tb.add("Tab4", new JTextArea(10, 20));
    tb.add("Tab5", new JTextArea(10, 20));
    jp.add(tb, BorderLayout.CENTER);
    tb.setEnabledAt(1, false);
    tb.setEnabledAt(2, false);
    tb.setEnabledAt(3, false);
    tb.setEnabledAt(4, false);
    JFrame frame = new JFrame();
    frame.setLayout(new BorderLayout());
    frame.add(jp, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public Main() {
    super(new BorderLayout());
    JProgressBar progressBar = new JProgressBar(0, 100);
    progressBar.setValue(0);//ww w  .j a v a2 s  . c  om
    progressBar.setStringPainted(true);

    taskOutput = new JTextArea(5, 20);
    taskOutput.setEditable(false);

    startButton = new JButton("Start");
    startButton.setActionCommand("start");
    startButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            startButton.setEnabled(false);
            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

            final Task task = new Task();
            task.addPropertyChangeListener(new PropertyChangeListener() {
                @Override
                public void propertyChange(PropertyChangeEvent pce) {
                    if ("progress".equals(pce.getPropertyName())) {
                        int progress = (Integer) pce.getNewValue();
                        progressBar.setValue(progress);
                        taskOutput.append(String.format("Completed %d%% of task.\n", task.getProgress()));
                    }
                }
            });
            task.execute();
        }
    });

    JPanel panel = new JPanel();
    panel.add(startButton);
    panel.add(progressBar);

    add(panel, BorderLayout.PAGE_START);
    add(new JScrollPane(taskOutput), BorderLayout.CENTER);

}

From source file:Main.java

public Console() {
    JTextArea textArea = new JTextArea(24, 80);
    textArea.setBackground(Color.BLACK);
    textArea.setForeground(Color.LIGHT_GRAY);
    textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
    System.setOut(new PrintStream(new OutputStream() {
        @Override//from  ww  w.j  a  va 2  s.c o  m
        public void write(int b) throws IOException {
            textArea.append(String.valueOf((char) b));
        }
    }));
    frame.add(textArea);
}