Question

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

Was it helpful?

Solution

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

OTHER TIPS

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.

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top