Mahdi_Nine
Mahdi_Nine

Reputation: 14751

How can I resize a Swing text field?

I wrote the code below:

public class Information extends JFrame{
private JComboBox comboBox1;
private JComboBox comboBox2;
private JTextField textField[];
private JTextField jtextField;
private JLabel label[];

public Information()
{
    setLayout(new flowlayout());
    Box box = Box.createVerticalBox();
    for(int i = 0; i < 20; i++)//textfield
    {
        textField = new JTextField[20];
        box.add(Box.createRigidArea(new Dimension(5,5)));
        textField[i] = new JTextField(2);

        textField[i].setAlignmentX(2f);
        textField[i].setAlignmentY(2f);

        box.add(textField[i]);
    }
    for(int i = 0; i < 22; i++ )//lable
    {

    }
    add(box);
}
}

I want that text field showed in my favorite size, but it fills the whole screen.

I used

 textField[i].setAlignmentX(2f);
 textField[i].setAlignmentY(2f);

and setcolum(), but it didn't resize the text field. How can I decrease my text fields' sizes?

I use from

setMaximumSize(new Dimension(80, 70));
setPreferredSize(new Dimension(50, 50));

their resize the textfield but change it's location but i don't want to change their

location.is there any manner than i change textfield position?

i use from setlocation but it didn't work!!.

Upvotes: 0

Views: 11337

Answers (5)

Mayank Jain
Mayank Jain

Reputation: 23

I think you want to Set a specific size for your textfield. To do this : Firstly set the layout as null using the object of Information class

information.setLayout(null);

And set the bounds of the JTextField using setBounds method :

textField.setBounds(0,0,100,100);

Upvotes: 0

Herrad
Herrad

Reputation: 63

Try setPreferredSize(), this (along with setMinimum/MaximumSize()) tend to be used by a lot of LayoutManagers including FlowLayout to appropriately size a container's components.

Also, as a side note, looks like you're re-creating your array of JTextAreas each iteration in that for loop, so only textField[19] would exist...

Upvotes: 0

MikeM
MikeM

Reputation: 27405

use setPreferredSize()

also setMinimumSize(), setMaximumSize()

Upvotes: 4

Pops
Pops

Reputation: 30818

The setAlignment methods change the alignment of a component in its container, not the component's actual size. I think you're looking for JTextField's setColumns() method. There's always the inherited setSize() method, too.

Upvotes: 0

Riggy
Riggy

Reputation: 1387

I believe you want to use setBounds which will take a width and height as parameters.

Upvotes: 0

Related Questions