Instead of using a JTextField
, use a JTextArea
, which is designed to handle multiple lines of text.
See How to use text areas for more details.
In any case, you should be providing columns
(and in the case of JTextArea
) rows
values. The API has a mechanism for "guessing" the required amount of space it will need to fulfil those requirements based on the current font properties.
This allows the API to provide sizing hints back to the layout manager API which it can then use to determine how best to position/size the components.
To allow the text area to span across multiple columns, I'd recommend that you use a GridBagLayout
See How to use GridBagLayout for more details
While there are a number of layout managers, GridBagLayout
is probably the most flexible available in the core API (MigLayout would also be another consideration). This provides you with the most amount of control you will have to make determination about how each component should be positioned and sized.
For example...

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test1 {
public static void main(String[] args) {
new Test1();
}
public Test1() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTextField email;
private JTextField city;
private JTextArea address;
public TestPane() {
email = new JTextField(10);
city = new JTextField(10);
address = new JTextArea(5, 20);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(4, 4, 4, 4);
gbc.anchor = GridBagConstraints.WEST;
add(new JLabel("Email ID:"), gbc);
gbc.gridx++;
add(email, gbc);
gbc.gridx++;
add(new JLabel("City:"), gbc);
gbc.gridx++;
add(city, gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.anchor = GridBagConstraints.NORTHWEST;
add(new JLabel("Address:"), gbc);
gbc.gridx++;
gbc.gridwidth = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(new JScrollPane(address), gbc);
}
}
}