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(String text, int rows, int columns) 

Source Link

Document

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

Usage

From source file:Main.java

public static void main(String[] args) {
    String text = "one\ntwo\nthree\nfour\nfive";
    JFrame frame = new JFrame("title");
    JTextArea textArea = new JTextArea(text, 1, 30); // shows only one line
    JScrollPane scrollPane = new JScrollPane(textArea);
    frame.add(scrollPane);//from   w ww  . j  a va  2s  . co  m
    frame.pack();
    frame.setVisible(true);

    final JViewport viewport = scrollPane.getViewport();

    textArea.addCaretListener(e -> {
        System.out.println("First : " + viewport.getViewPosition());
        System.out.println("Second: " + viewport.getViewPosition());
    });
    textArea.setCaretPosition(text.length());
}

From source file:Main.java

public static void main(String argv[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String testStr = "Paste text here.";
    JTextArea wrapArea = new JTextArea(testStr, 20, 40);
    wrapArea.setLineWrap(true);//from w w  w.  j av a 2  s  .  c  om
    wrapArea.setWrapStyleWord(true);
    wrapArea.setCaretPosition(testStr.length());
    frame.add(new JScrollPane(wrapArea));
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel gui = new JPanel(new BorderLayout());
    gui.setBorder(new EmptyBorder(2, 3, 2, 3));

    JPanel textPanel = new JPanel(new BorderLayout(5, 5));
    textPanel.add(new JScrollPane(new JTextArea("Top Text", 3, 20)), BorderLayout.PAGE_START);
    textPanel.add(new JScrollPane(new JTextArea("Main Text", 10, 10)));
    gui.add(textPanel, BorderLayout.CENTER);

    JPanel buttonCenter = new JPanel(new GridBagLayout());
    buttonCenter.setBorder(new EmptyBorder(5, 5, 5, 5));
    JPanel buttonPanel = new JPanel(new GridLayout(0, 1, 5, 5));
    for (int ii = 1; ii < 6; ii++) {
        buttonPanel.add(new JButton("Button " + ii));
    }//from ww w . ja v a  2 s  .c  om

    buttonCenter.add(buttonPanel);

    gui.add(buttonCenter, BorderLayout.LINE_END);

    JFrame f = new JFrame("Demo");
    f.add(gui);

    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    f.setLocationByPlatform(true);

    f.pack();

    f.setVisible(true);
}

From source file:FlowLayoutTest.java

public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("FlowLayout Test");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String text = "A JTextArea object represents" + "a multiline area for displaying text."
            + "You can change the number of lines" + "that can be displayed at a time.";
    JTextArea textArea1 = new JTextArea(text, 5, 10);
    textArea1.setPreferredSize(new Dimension(100, 100));
    JTextArea textArea2 = new JTextArea(text, 5, 10);
    textArea2.setPreferredSize(new Dimension(100, 100));
    JScrollPane scrollPane = new JScrollPane(textArea2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    textArea1.setLineWrap(true);// w  ww .jav a2s  .c o  m
    textArea2.setLineWrap(true);
    frame.add(textArea1);
    frame.add(scrollPane);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String text = "A JTextArea object represents a multiline area for displaying text. "
            + "You can change the number of lines that can be displayed at a time, "
            + "as well as the number of columns. You can wrap lines and words too. "
            + "You can also put your JTextArea in a JScrollPane to make it scrollable.";

    JTextArea textAreal = new JTextArea(text, 5, 10);
    textAreal.setPreferredSize(new Dimension(100, 100));
    JTextArea textArea2 = new JTextArea(text, 5, 10);
    textArea2.setPreferredSize(new Dimension(100, 100));
    JScrollPane scrollPane = new JScrollPane(textArea2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    textAreal.setLineWrap(true);/*from  w w w . ja  v  a  2  s  .  c  o  m*/
    textArea2.setLineWrap(true);
    frame.add(textAreal);
    frame.add(scrollPane);
    frame.pack();
    frame.setVisible(true);
}

From source file:EditabilityExample.java

public static void main(String[] args) {
    try {/*  w  w  w.  j  a  v  a  2s.c o m*/
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    JFrame f = new JFrame("Editability Example");
    f.getContentPane().setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));
    f.getContentPane().add(firstField);

    JTextField tf = new JTextField("A read-only text field", 20);
    tf.setEditable(false);
    f.getContentPane().add(tf);

    JTextArea ta = new JTextArea("An editable\ntext area", 2, 20);
    ta.setBorder(BorderFactory.createLoweredBevelBorder());
    f.getContentPane().add(ta);

    ta = new JTextArea("A read-only\ntext area", 2, 20);
    ta.setBorder(BorderFactory.createLoweredBevelBorder());
    ta.setEditable(false);
    f.getContentPane().add(ta);

    f.pack();
    f.show();

    if (args.length == 1 && args[0].equals("disable")) {
        // Toggle the enabled state of the first
        // text field every 10 seconds
        Timer t = new Timer(10000, new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                firstField.setEnabled(!firstField.isEnabled());
                firstField.setText(firstFieldText + (firstField.isEnabled() ? "" : " (disabled)"));
            }
        });
        t.start();
    }
}

From source file:Main.java

public static void showMessage(String title, String str) {
    JFrame info = new JFrame(title);
    JTextArea t = new JTextArea(str, 15, 40);
    t.setEditable(false);// w  w w  . ja va  2  s.co  m
    t.setLineWrap(true);
    t.setWrapStyleWord(true);
    JButton ok = new JButton("Close");
    ok.addActionListener(windowCloserAction);
    info.getContentPane().setLayout(new BoxLayout(info.getContentPane(), BoxLayout.Y_AXIS));
    info.getContentPane().add(new JScrollPane(t));
    info.getContentPane().add(ok);
    ok.setAlignmentX(Component.CENTER_ALIGNMENT);
    info.pack();
    //info.setResizable(false);
    centerFrame(info);
    //info.show();
    info.setVisible(true);
}

From source file:HelloInJapanese.java

public HelloInJapanese(String characters) {
    Font theFont = new Font("Bitstream Cyberbit", Font.PLAIN, 20);
    JTextArea area = new JTextArea(characters, 2, 30);
    area.setFont(theFont);//  w  w  w.  j av a  2 s  .c  om
    area.setLineWrap(true);
    JScrollPane scrollpane = new JScrollPane(area);
    add(scrollpane);
}

From source file:Main.java

public static final Component getLabelComponent(String text, int rows, int cols) {
    JTextArea txt = new JTextArea(text, 10, 80);
    //txt.setColumns(80);
    txt.setWrapStyleWord(true);//from   ww  w  . j  a  va  2s .co m
    txt.setLineWrap(true);
    txt.setOpaque(false);
    txt.setEditable(false);
    //txt.setEnabled(false);
    //txt.setFont(FontLoaderUtils.getOrLoadACompatibleFont(text,txt.getFont()));//UIManager.getFont("Label.font")));
    return new JScrollPane(txt);
}

From source file:Main.java

public Main() {
    super();//from  w w w. j  a v a2  s  .c om
    Container pane = getContentPane();
    pane.setLayout(new FlowLayout(FlowLayout.LEFT));
    pane.add(new JLabel("This is a test"));
    pane.add(new JButton("of a FlowLayout"));
    pane.add(new JTextField(30));
    pane.add(new JTextArea("This is a JTextArea", 3, 10));
    pane.add(new JLabel("This is a FlowLayout test with a long string"));
}