0

I am working on an application and I am stuck in the incipient phase. I have a JTextField in a JPanel in a JFrame. JTextField isn't there. If I use

JPanel p0 = (JPanel) f.getContentPane();

it works. But Not with

JPanel p0 = new JPanel();
f.add(p0);

So the problems are:

  1. Why is not the field visible? (most important q)
  2. What's the difference between the 2 aforementioned approaches?

Code:

import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;

public class Main
{

    static Font fontDefault = new Font("arial", Font.PLAIN, 15);

    public static void main ( String [ ] args )
    {
        JFrame f = new JFrame("Liquid");
        f.setSize(new Dimension(840, 400));
        //f.setIconImage(image);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //JPanel p0 = (JPanel) f.getContentPane();// is it necessary?
        JPanel p0 = new JPanel();
        p0.setLayout(null);
        JPanel p1 = new JPanel();
        p1.setLayout(null);

        JTextField tfHostName = new JTextField("default text", 20);
        tfHostName.setBounds(50, 50, 200, 25);
        tfHostName.setFont(fontDefault);

        JButton bRequest = new JButton("request");
        JButton bReset = new JButton("reset");

        JTextArea taTest = new JTextArea("default text", 1, 20);
        p0.add(tfHostName);
        f.add(p0);

        f.add(p1);

        p0.add(taTest);
        //f.pack();
        f.setResizable(false);
        f.setVisible(true);

    }
}

As a reminder:

It works with JPanel p0 = (JPanel) f.getContentPane(); but why id doesn't with 2nd approach, which I'm more comfortable with? Plus, that way how do I add a second panel and how do I make components in each panel auto-arranged?

Update:

I realized that the code didn't work in the first place probably because I didn't specified any coordinates/position?...

Ivar
  • 6,138
  • 12
  • 49
  • 61
mireazma
  • 526
  • 1
  • 8
  • 20
  • 3
    for better help sooner post an [SSCCE](http://sscce.org/), short, runnable, compilable about JPanel p0 = new JPanel(); f.add(p0); – mKorbel Apr 26 '13 at 11:41
  • EDITED. Added code. I wanted to do this in the first place but it wouldn't let me, saying the question is not good by their standards... – mireazma Apr 26 '13 at 12:01
  • `What's the difference between the 2 aforementioned approaches?` - the first approach uses the default layot manger. The second approach doesn't use a layout manager. Don't use a null layout!!! Don't use setBounds()!!! Why do beginners always play with a null layout and then complain when it doesn't work? All the example code we post in this forum uses a layout manager. Where do you get this code that shows you to use a null layout? My advice is to ignore that website. – camickr Apr 26 '13 at 14:56
  • Man, I'm only searching for a straight forward way to place the component in the panel/frame. I didn't take a detail into account, though: how/where do I specify the actual position of the JTextField in the panel, given a layout manager? – mireazma Apr 26 '13 at 17:36

3 Answers3

3

first you have to add your panel to your layoutmangager.

sth like

add(p0);

and then you need a call to pack of the JFrame

pack();

if you want 2 panels, you have give them a position in your frame / layout manager.

f.add(pane1, BorderLayout.WEST);
f.add(pane2, BorderLayout.EAST);

you have 3 possibilites to set the size on your components:

setPreferredSize(Dimension D);
setMinimumSize(Dimension D);
setMaximumSize(Dimension D);
duffy356
  • 3,678
  • 3
  • 32
  • 47
  • it won't work either. I want the frame at the size I gave it not bigger, not smaller. With pack() it shrinks to like 100 by 10 pixels, instead of 840 x 400. – mireazma Apr 26 '13 at 12:03
  • You mean create layout manager object and then call smth like mgr.add(p0)? Then how to add it to the frame? I'm a beginner :) – mireazma Apr 26 '13 at 12:20
  • just call the add-method of your JFrame instance. – duffy356 Apr 26 '13 at 12:23
  • It works with f.add(p0, BorderLayout.WEST) but not with f.add(p0). Why? – mireazma Apr 26 '13 at 13:27
  • i don't know exactly, but here is a gneral good tutorial with a lot of infos for layout managers http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html pls accept answer ;) – duffy356 Apr 26 '13 at 19:44
0

Edit:

You havent set this :-

f.setLayout(new FlowLayout());

You need to set Layout to frame also and comment out

//p0.setLayout(null);
//p1.setLayout(null);

Do like this

JFrame jf=new JFrame();
Jpanel jp=new JPanel();
jp.add(new TextField());
jf.add(jp);

set jf.setVisible(true);
anshulkatta
  • 2,044
  • 22
  • 30
  • I guess it's equal to what I did in the snippet. – mireazma Apr 26 '13 at 12:05
  • Indeed. I checked and the default layout for a JFrame is "bordered" which for *some* reason it wouldn't show the JTextField. – mireazma Apr 26 '13 at 12:53
  • "default layout for a JFrame is "bordered" which for *some* reason it wouldn't show the JTextField." It is a very simple reason. 1) A `BorderLayout` has 5 areas which can each hold/display **one** component. 2) A component added to a `BorderLayout` without a constraint will be placed in the **`CENTER`**. 3) Add another component that same way and GOTO (1) for why it does not show up. – Andrew Thompson Apr 28 '13 at 08:45
0

Regarding the difference between the your 2 pieces of code: A JFrame has a contentPane which is actually the Panel where all the stuff in the window happens - the Frame has just this Panel and not others. By calling f.setContentPane(randomJPanel); you can actually set the contentPane to some Panel you want. I would highly recommend working in this contentPane with Layouts and not to do your stuff directly in the JFrame.

LionC
  • 3,106
  • 1
  • 22
  • 31