0

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

Mahdi_Nine
  • 14,205
  • 26
  • 82
  • 117
  • see also: [Need help! how can i resize a JTextField?](http://stackoverflow.com/questions/2442111/need-help-how-can-i-resize-a-jtextfield) – MikeM Mar 30 '11 at 14:09

5 Answers5

4

use setPreferredSize()

also setMinimumSize(), setMaximumSize()

MikeM
  • 27,227
  • 4
  • 64
  • 80
  • Interesting... see [Difference between the setPreferredSize() and setSize() methods in components](http://stackoverflow.com/questions/1783793/java-difference-between-the-setpreferredsize-and-setsize-methods-in-componen) for more. – Pops Mar 30 '11 at 14:22
0

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

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

Riggy
  • 1,347
  • 1
  • 14
  • 26
0

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.

Pops
  • 30,199
  • 37
  • 136
  • 151
  • Wait, what are you setting it on? Are you setting it on the array? That shouldn't even compile, let alone actually resize a specific field. – Pops Mar 30 '11 at 14:05
0

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

Herrad
  • 63
  • 1
  • 6