0

Is there any way to put a text box into a JFrame that the user can input text into? Kind of like the TextEdit or Notepad app.

alexsmbaratti
  • 47
  • 1
  • 2
  • 9

2 Answers2

2

The closest equivalent to Notepad is a JTextArea. Wrap it in a JScrollPane for scroll-bars. Vis.

Notepad

Editpad

Tip: Make it the native PLAF for an even closer lookalike.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class Editpad {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new BorderLayout());
                gui.setBorder(new EmptyBorder(2,3,2,3));

                // adjust numbers for a bigger default area
                JTextArea editArea = new JTextArea(5,40);
                // adjust the font to a monospaced font.
                Font font = new Font(
                        Font.MONOSPACED, 
                        Font.PLAIN, 
                        editArea.getFont().getSize());
                editArea.setFont(font);
                gui.add(new JScrollPane(editArea,
                        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                        JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS));

                JFrame f = new JFrame("Editpad");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

Use a JTextField, which is a single-line "text area", or a JTextArea, which can handle multiple lines. Both work similarly. Here's a simple example with JTextField:

import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JTextField;

public class Foo extends JFrame {

    public Foo() {
        setLayout(new FlowLayout());
        JTextField field = new JTextField(20);
        add(field);
        setSize(500, 500);
        setVisible(true);
    }

    public static void main(String[] args) {
        Foo foo = new Foo();
    }
}

To do anything useful with such a component, you need to also add an event/action listener to a component (say a JButton) to do something with the text in it (which you can get by calling getText() on the JTextField or JTextArea)

Martin Dinov
  • 8,757
  • 3
  • 29
  • 41