Reputation: 1102
i have a text field like emailid, city and address,
i want to make the address textfield should be bigger as shown in image!
i use BorderLayout as default and GridLayout for the selected panel
top.setLayout(new GridLayout(8,4,10,10));
top.add(emailid);
top.add(temailid);
top.add(address);
top.add(taddress);
add(top, BorderLayout.CENTER);
Upvotes: 2
Views: 15480
Reputation: 1102
just by creating a panel inside as
top.add(emailid);
Panel p5 = new Panel();
p5.setLayout(new GridLayout(1,3,1,1));
p5.add(temailid);
p5.add(city);
p5.add(tcity);
top.add(p5);
top.add(address);
Panel p6 = new Panel();
p6.setLayout(new GridLayout());
p6.add(taddress);
top.add(p6);
as this occupies the entire space in the layout..
thanks for all ur answers, but i found the easiest way in my way
Upvotes: 0
Reputation: 1692
Use MiGLayout. That's the only layout I ever use these days. IMHO, it supersedes everything else that it's out there and is very intuitive and self-documenting.
Upvotes: -2
Reputation: 347204
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);
}
}
}
Upvotes: 5
Reputation: 15706
The size of a component is determined by the LayoutManager
, which takes the minimum, maximum and preferred size of a component (see the methods on JComponent
) into account.
Some layout managers can be given layouting hints for each component, those depend upon the layout manager used.
Components calculate their preferred size based on the information they display, along with decorative settings such as insets, borders, font metric, etc. It's generally bad practice to set the preferred size of a component, this may only be appropriate in special cases (scroll panes or images, for example).
Back to your case: the JTextField
allows to hint its preferred width in number of characters (or: columns, measuring with the broadest character, such as 'M'), calculating the actual preferred width using border and font metric information.
For this, you can use following constructors/methods:
JTextField(int columns)
JTextField(String text, int columns)
setColumns(int columns)
Upvotes: 0
Reputation: 810
Assuming that you want to increase the width of your text field, there are different approaches.
A very simple approach uses JTextField's constructor:
JTextField email = new JTextField(25); //indicate how many columns you need
However, the best approach is a BorderLayout where the address textfield is centered:
JPanel northPane = new JPanel(new BorderLayout(5, 5)); //add this panel right above your text area
JPanel eastPane = new JPanel(new BorderLayout(5, 5));
JTextField email = new JTextField(); //will take all the available space, so you don't need arguments
JLabel cityLabel = new JLabel("City:");
JTextField city = new JTextField(8); //should take a few characters
northPane.add(email, BorderLayout.CENTER);
northPane.add(eastPane, BorderLayout.EAST);
eastPane.add(cityLabel, BorderLayout.WEST);
eastPane.add(city, BorderLayout.CENTER);
This way, your address field will use all the available space.
Upvotes: 0