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:UndoDemo.java

/** Construct a GUI that demonstrates use of UndoManager */
public UndoDemo() {

    Container cp = getContentPane();
    cp.add(ta = new JTextArea(20, 60), BorderLayout.CENTER);
    JPanel bp;/*from  w w  w .  j  ava2 s .co  m*/
    cp.add(bp = new JPanel(), BorderLayout.SOUTH);

    // Create a javax.swing.undo.UndoManager; this is an amazing class that
    // keeps a Stack of UndoableEdits and lets you invoke them;
    // by registering it as a Listener on the TextComponent.Document,
    // the Document will create the UndoableEdit objects and send them
    // to the UndoManager. Between them they do ALL the work!
    um = new UndoManager();
    ta.getDocument().addUndoableEditListener(um);

    // Create the buttons
    JButton cutButton, copyButton, pasteButton, undoButton, redoButton;
    bp.add(cutButton = new JButton("Cut"));
    cutButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ta.cut();
        }
    });

    bp.add(copyButton = new JButton("Copy"));
    copyButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ta.copy();
        }
    });

    bp.add(pasteButton = new JButton("Paste"));
    pasteButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ta.paste();
        }
    });
    bp.add(undoButton = new JButton("UnDo"));
    undoButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (um.canUndo()) {
                um.undo();
            } else {
                warn("Can't undo");
            }
        }
    });
    bp.add(redoButton = new JButton("ReDo"));
    redoButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (um.canRedo()) {
                um.redo();
            } else {
                warn("Can't redo");
            }
        }
    });
    pack();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

From source file:components.ToolBarDemo.java

public ToolBarDemo() {
    super(new BorderLayout());

    //Create the toolbar.
    JToolBar toolBar = new JToolBar("Still draggable");
    addButtons(toolBar);/*from www .j a  va2s .  co m*/

    //Create the text area used for output.  Request
    //enough space for 5 rows and 30 columns.
    textArea = new JTextArea(5, 30);
    textArea.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textArea);

    //Lay out the main panel.
    setPreferredSize(new Dimension(450, 130));
    add(toolBar, BorderLayout.PAGE_START);
    add(scrollPane, BorderLayout.CENTER);
}

From source file:SwingFileChooserDemo.java

public SwingFileChooserDemo() {
    super(new BorderLayout());

    //Create the log first, because the action listeners
    //need to refer to it.
    log = new JTextArea(5, 20);
    log.setMargin(new Insets(5, 5, 5, 5));
    log.setEditable(false);/*  w  ww . j  a v  a  2 s.  co  m*/
    JScrollPane logScrollPane = new JScrollPane(log);

    //Create a file chooser
    fc = new JFileChooser();

    //Uncomment one of the following lines to try a different
    //file selection mode. The first allows just directories
    //to be selected (and, at least in the Java look and feel,
    //shown). The second allows both files and directories
    //to be selected. If you leave these lines commented out,
    //then the default mode (FILES_ONLY) will be used.
    //
    //fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    //fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

    //Create the open button. We use the image from the JLF
    //Graphics Repository (but we extracted it from the jar).
    openButton = new JButton("Open a File...", createImageIcon("images/Open16.gif"));
    openButton.addActionListener(this);

    //Create the save button. We use the image from the JLF
    //Graphics Repository (but we extracted it from the jar).
    saveButton = new JButton("Save a File...", createImageIcon("images/Save16.gif"));
    saveButton.addActionListener(this);

    //For layout purposes, put the buttons in a separate panel
    JPanel buttonPanel = new JPanel(); //use FlowLayout
    buttonPanel.add(openButton);
    buttonPanel.add(saveButton);

    //Add the buttons and the log to this panel.
    add(buttonPanel, BorderLayout.PAGE_START);
    add(logScrollPane, BorderLayout.CENTER);
}

From source file:Main.java

public Main() {
    super(new BorderLayout());
    task = new LongTask();

    //Create the demo's UI.
    startButton = new JButton("Start");
    startButton.setActionCommand("start");
    startButton.addActionListener(this);

    taskOutput = new JTextArea(5, 20);
    taskOutput.setMargin(new Insets(5, 5, 5, 5));
    taskOutput.setEditable(false);// w  ww  .j  av  a  2 s  .  c  o m

    add(startButton, BorderLayout.PAGE_START);
    add(new JScrollPane(taskOutput), BorderLayout.CENTER);
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    //Create a timer.
    timer = new Timer(ONE_SECOND, new TimerListener());
}

From source file:ProgressMonitorDemo.java

public ProgressMonitorDemo() {
    super(new BorderLayout());
    task = new LongTask();

    //Create the demo's UI.
    startButton = new JButton("Start");
    startButton.setActionCommand("start");
    startButton.addActionListener(this);

    taskOutput = new JTextArea(5, 20);
    taskOutput.setMargin(new Insets(5, 5, 5, 5));
    taskOutput.setEditable(false);/*from w w  w .  j  a va2 s  . c  o m*/

    add(startButton, BorderLayout.PAGE_START);
    add(new JScrollPane(taskOutput), BorderLayout.CENTER);
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    //Create a timer.
    timer = new Timer(ONE_SECOND, new TimerListener());
}

From source file:misc.ActionDemo.java

public ActionDemo() {
    super(new BorderLayout());

    //Create a scrolled text area.
    textArea = new JTextArea(5, 30);
    textArea.setEditable(false);/*from   ww  w . j  a v a 2 s. c  om*/
    JScrollPane scrollPane = new JScrollPane(textArea);

    //Lay out the content pane.
    setPreferredSize(new Dimension(450, 150));
    add(scrollPane, BorderLayout.CENTER);

    //Create the actions shared by the toolbar and menu.
    leftAction = new LeftAction("Go left", createNavigationIcon("Back24"), "This is the left button.",
            new Integer(KeyEvent.VK_L));
    middleAction = new MiddleAction("Do something", createNavigationIcon("Up24"), "This is the middle button.",
            new Integer(KeyEvent.VK_M));
    rightAction = new RightAction("Go right", createNavigationIcon("Forward24"), "This is the right button.",
            new Integer(KeyEvent.VK_R));
}

From source file:ProgressBarDemo.java

public ProgressBarDemo() {
    super(new BorderLayout());
    task = new LongTask();

    //Create the demo's UI.
    startButton = new JButton("Start");
    startButton.setActionCommand("start");
    startButton.addActionListener(this);

    progressBar = new JProgressBar(0, task.getLengthOfTask());
    progressBar.setValue(0);//from www  . ja  v  a  2  s  .  c  om
    progressBar.setStringPainted(true);

    taskOutput = new JTextArea(5, 20);
    taskOutput.setMargin(new Insets(5, 5, 5, 5));
    taskOutput.setEditable(false);
    taskOutput.setCursor(null); //inherit the panel's cursor
                                //see bug 4851758

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

    add(panel, BorderLayout.PAGE_START);
    add(new JScrollPane(taskOutput), BorderLayout.CENTER);
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    //Create a timer.
    timer = new Timer(ONE_SECOND, new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            progressBar.setValue(task.getCurrent());
            String s = task.getMessage();
            if (s != null) {
                taskOutput.append(s + newline);
                taskOutput.setCaretPosition(taskOutput.getDocument().getLength());
            }
            if (task.isDone()) {
                Toolkit.getDefaultToolkit().beep();
                timer.stop();
                startButton.setEnabled(true);
                setCursor(null); //turn off the wait cursor
                progressBar.setValue(progressBar.getMinimum());
            }
        }
    });
}

From source file:components.ToolBarDemo2.java

public ToolBarDemo2() {
    super(new BorderLayout());

    //Create the toolbar.
    JToolBar toolBar = new JToolBar("Still draggable");
    addButtons(toolBar);// w w  w  .  j  a  v  a 2s .  c  o  m
    toolBar.setFloatable(false);
    toolBar.setRollover(true);

    //Create the text area used for output.  Request
    //enough space for 5 rows and 30 columns.
    textArea = new JTextArea(5, 30);
    textArea.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textArea);

    //Lay out the main panel.
    setPreferredSize(new Dimension(450, 130));
    add(toolBar, BorderLayout.PAGE_START);
    add(scrollPane, BorderLayout.CENTER);
}

From source file:components.ProgressMonitorDemo.java

public ProgressMonitorDemo() {
    super(new BorderLayout());

    //Create the demo's UI.
    startButton = new JButton("Start");
    startButton.setActionCommand("start");
    startButton.addActionListener(this);

    taskOutput = new JTextArea(5, 20);
    taskOutput.setMargin(new Insets(5, 5, 5, 5));
    taskOutput.setEditable(false);/*from w  w w  .  j  a va 2  s  .c  o m*/

    add(startButton, BorderLayout.PAGE_START);
    add(new JScrollPane(taskOutput), BorderLayout.CENTER);
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

}

From source file:CubaHSQLDBServer.java

private CubaHSQLDBServer() {
    Font monospaced = Font.decode("monospaced");

    statusArea = new JTextArea(2, 80);
    statusArea.setFont(monospaced);//from  w  w  w  .  jav  a2  s .c o m
    statusArea.setMargin(new Insets(5, 5, 5, 5));
    exceptionArea = new JTextArea(26, 80);
    exceptionArea.setFont(monospaced);
    exceptionArea.setMargin(new Insets(5, 5, 5, 5));
    JPanel exceptionWrapperContainer = new JPanel();
    exceptionWrapperContainer.setLayout(new BorderLayout(0, 0));
    exceptionWrapperContainer.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
    exceptionWrapperContainer.add(exceptionArea);
    JPanel statusWrapperContainer = new JPanel();
    statusWrapperContainer.setLayout(new BorderLayout(0, 0));
    statusWrapperContainer.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
    statusWrapperContainer.add(statusArea);
    addCopyPopup(statusArea);
    addCopyPopup(exceptionArea);

    exceptionBox = new JPanel();
    LayoutBuilder.create(exceptionBox, BoxLayout.Y_AXIS).addSpace(5).addComponent(exceptionWrapperContainer);

    LayoutBuilder.create(this.getContentPane(), BoxLayout.X_AXIS).addSpace(5).addContainer(BoxLayout.Y_AXIS)
            .addSpace(5).addComponent(statusWrapperContainer).addComponent(exceptionBox).addSpace(5)
            .returnToParent().addSpace(5);

    statusArea.setEditable(false);
    exceptionArea.setEditable(false);
    exceptionBox.setVisible(false);
    exceptionArea.setBackground(new Color(255, 255, 212));
    this.pack();
    this.setResizable(true);
    this.setTitle("HSQLDB Server");

    try {
        this.setIconImage(ImageIO.read(getClass().getResourceAsStream("/icons/database.png")));
    } catch (IOException e) {
        throw new IllegalStateException("Unable to find icon for HSQLDB window");
    }
}