I'm having problems adding a JPanel, which has a paintComponent in it, to a JFrame.
If this is the only thing I add to the frame, it works. But as soon as I add a layout manager and add other components to the JFrame, it no longer shows the panel with the painting!

To make this clearer ...

This is the code that works and the JPanel is successfully shown:

The panel that draws the sign (in reality i am not trying to paint hello, this is to simply the code here)

public class SignPanel2 extends JPanel {
public int hello;

public void paintComponent(Graphics comp) {
    Graphics g = (Graphics) comp;
    g.setColor(Color.LIGHT_GRAY);
    g.fillRect(70, 250, 150, 150);

    g.setColor(Color.BLACK); 

    if (hello > 0) 
        g.drawString("h",135, 270);

    if (hello > 1 ) 
        g.drawString("h e",135, 270);

    if (hello > 2) 
        g.drawString("h e l",135, 270);

    if (hello > 3) 
        g.drawString("h e l l",135, 270);

    if (hello > 4) 
        g.drawString("h e l l o",135, 270);
}

}

The frame i put the panel on:

public class SignFrame extends JFrame {

// the constructor method
public SignFrame () {

    super("This is the title of the Sign Frame");
    setSize(300,500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // make a container for the frame
    Container content = getContentPane();

    // call from the drawing panel
    SignPanel2 signpanel = new SignPanel2();
    // change class variable of SignPanel
    signpanel.hello = 5;
    signpanel.repaint();


    // add signpanel to container
    content.add(signpanel);

    setContentPane(content);

    setVisible(true);

}

}

The main class

public class TheSignMain {

public static void main (String[] args) {

    SignFrame signframe = new SignFrame();

}

}

The above works perfectly fine and gives me a frame with the desired drawing in it.

But if I add other components to the frame and add a layout manager, it no longer shows me the painting. even if I use repaint().
I have to include a layout manager, otherwise it adds the panel with the painting, but not the other components. This is how my frame class looks now, and this is where i'm having problems.

public class SignFrame extends JFrame {

   // the constructor method
   public SignFrame () {

    super("This is the title of the Sign Frame");
    setSize(300,500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // make a container for the frame
    Container content = getContentPane();


    // need a layout manager to decide the arrangement of the components on the container
    FlowLayout flo = new FlowLayout();
    // designate the layout manager to the container
    content.setLayout(flo);


    // make components
    JPanel buttons = new JPanel();
    JButton play = new JButton("Play");
    JButton pause = new JButton("Pause");
    JButton stop = new JButton("Stop");
    // add components to a panel
    buttons.add(play);
    buttons.add(pause);
    buttons.add(stop);
    // add panel to frame container
    content.add(buttons);

    // call from the drawing panel
    SignPanel2 signpanel = new SignPanel2();
    // change class variable of SignPanel
    signpanel.hello = 5;
    signpanel.repaint();


    // add signpanel to container
    content.add(signpanel);

    setContentPane(content);

    setVisible(true);

}

}

I am totally new to Java, so any help will be much appreciated. Sorry for all that code and thanks for your help!!

Layout managers are pretty fickle about using or ignoring your component's sizes. You could try setMinimumSize for your panel as well as setSize.

You're totally right about that.
Someone told me to use

signpanel.setPreferredSize(new Dimensions(width, height));

... which I did. And it worked momentarily. But then I changed the gridlayout to
GridLayout(3,1) because I wanted all components to be one under the other and the panle just vanished!! Any suggestions what I should do now?

In general with GridLayout its safer to have a zero number of rows because otherwize the number of columns can be ignored (see the GridLayout API doc for details)

When both the number of rows and the number of columns have been set to non-zero values, either by a constructor or by the setRows and setColumns methods, the number of columns specified is ignored. Instead, the number of columns is determined from the specified number of rows and the total number of components in the layout.

But anyway, all I can suggest is to set size, preferred size, and minimum size and hope your layout manager will respect one of them. OR take the plunge into GridBagLayout which has a steep learning curve but has never failed to let me do whatever I need.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.