-2

Here is my code for creating a frame with a text field:

   static  void showteacherDel()
    {
    JFrame frame3 = new JFrame();
    frame3.setSize(640,480);
    JPanel pn = new JPanel();
    pn.setSize(640,480);
    frame3.add(pn);
    frame3.setResizable(false);

    JTextField tf = new JTextField();
    tf.setSize(300,500);
    Component add = pn.add(tf);

    //frame3.pack();
    frame3.setVisible(true);
 }
}

Now here is the problem. You see that I have created a panel and then added my text field to it.

The reason for that was that my text field, no matter what size I set, spawned whole of the frame.

So I created a panel and added the text field to it.

But no matter what size I give, it shows a point object, which is actually a text field.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • 2
    What is your "desired output"? – thodic Jun 06 '15 at 08:32
  • Are you trying to set the font size of the text in your JTextField? – almightyGOSU Jun 06 '15 at 08:39
  • @OhAuth iam actually trying to create a text feild...iam trying t set its size but it is not visible...by not visible i mean whatever size i set...it only shows a point object – Sky's On Ur Head Jun 06 '15 at 09:02
  • @Sky'sOnUrHead Isn't that because your JTextField has no content? You need to use setText to give it some content. – almightyGOSU Jun 06 '15 at 09:08
  • @OhAuth thats right but that will make my text field dependent on some text pre inputted...what i want is to give it a deafault height and width... i have used setBounds but still it is not visible...just showing a point text field – Sky's On Ur Head Jun 06 '15 at 09:18
  • 1
    The short answer is, don't. Use an appropriate layout manager and sizing hints (like `setColumns`) – MadProgrammer Jun 06 '15 at 09:37

3 Answers3

2

Try this code :

JTextField tf = new JTextField(10);

I have added 10 in bracket which will give it a size.

Programmer
  • 445
  • 4
  • 12
1

The major problem is you don't seem to understand how components are laid out via the layout management API, for example, if we take a look at the code we can see:

JFrame frame3=new JFrame();
frame3.setSize(640,480);
JPanel pn=new JPanel();
// Pointless, as the JFrame is using a `BorderLayout`
// so the component will be the available size of it's parent
// container...
//pn.setSize(640,480);
frame3.add(pn);
frame3.setResizable(false);

JTextField tf=new JTextField();
// Pointless, as pn is using a `FlowLayout` so the
// text field will be laid out using it's preferred size
//tf.setSize(300,500);
// Provide a sizing hint, in combination with the fields
// font...
tf.setColumns(25);
Component add = pn.add(tf);

//frame3.pack();
frame3.setVisible(true);

I'm not sure why you would want a JTextField to 500 pixels high, maybe you want a JTextArea instead?

Take a closer look at Laying Out Components Within a Container for more details.

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

you can use tf.setText(" Your Text ");

add That into your panel , then after that add that panel into your frame then setVisible(true) then it will definite visible

kashan
  • 9
  • 4
  • as i stated above..that will make my feild dependent on some text pre inputted...whereas i want a default height and width for it.. i dont know why is this happening i have done these things before too :( – Sky's On Ur Head Jun 06 '15 at 09:20