1

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

as shown in figure

top.setLayout(new GridLayout(8,4,10,10));
top.add(emailid);
top.add(temailid);
top.add(address);
top.add(taddress);
add(top, BorderLayout.CENTER);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Abhinai
  • 1,082
  • 2
  • 13
  • 20

5 Answers5

5

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...

Form

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);
        }

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

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.

J4v4
  • 780
  • 4
  • 9
0

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)
Peter Walser
  • 15,208
  • 4
  • 51
  • 78
0

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

Abhinai
  • 1,082
  • 2
  • 13
  • 20
-2

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.

Irina Rapoport
  • 1,404
  • 1
  • 20
  • 37
  • Should be a comment, besides *"very intuitive and self-documenting"* - I would argue that, but I've only ever had 3 minutes to look at it ;) (and not the downvoter) – MadProgrammer Aug 27 '14 at 02:06
  • Did you look at this? http://www.miglayout.com/QuickStart.pdf The downside of making simple things easy and complex things possible is that someone might look at a complex thing first. – Irina Rapoport Aug 28 '14 at 23:00
  • Yep, always do, don't get me wrong, MigLayout is power, but it's `String` based constraints make it very hard to learn. Sure if I actually spent enough time with it, I'm sure I'd get use to, but saying it's *"very intuitive and self-documenting"* is like saying `GridBagLayout` is simple and easy to use. With power, comes complexity and time needed to learn it...just saying – MadProgrammer Aug 28 '14 at 23:58
  • Strings are not mandatory. It's entirely possible to do MigLayout without them; but I would not recommend it (far less readable, to me anyway). – Irina Rapoport Sep 06 '14 at 04:56