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.
Asked
Active
Viewed 2.4k times
0
-
2Have you gone through any of the Swing tutorials yet? That's where I'd start as this sort of question is then easily and quickly answered. – Hovercraft Full Of Eels Jan 25 '14 at 22:54
-
2why dont you use google? – Simiil Jan 25 '14 at 22:54
-
possible duplicate of [Simple JFrame program but can't see JTextfield](http://stackoverflow.com/questions/13713435/simple-jframe-program-but-cant-see-jtextfield) – jww Jan 26 '14 at 05:23
2 Answers
2
The closest equivalent to Notepad is a JTextArea
. Wrap it in a JScrollPane
for scroll-bars. Vis.
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
-
You don't necessary need an event listener for the textfield itself; more likely a button or other event that gets and uses the text from the textfield – imulsion Jan 25 '14 at 22:59
-
-
2Note that a `JTextField` is for a single line of text, so it is not even close to the component we would use to recreate an app. like Notepad. – Andrew Thompson Jan 26 '14 at 00:01
-
How can I change the size of the JTextField to be bigger (enough to fill up most of the window)? – alexsmbaratti Jan 26 '14 at 00:20