JComponent into non empty JPanale

I'm going slightly mad. I just want to insert a JComponet (containing a graphic) into a mainPanel that has to contain other objects before it. mainPanel is a simple JPanel, added to a JFrame, with two panels inside (one like a form panel with BoxLayout - JLabels + JTextField + JButton - and one with GridLayout containing a set of JLabel to summarize some data). When I click to submit button (on the first panel) I call a method that creates the JComponent objectComp (depending on inserted data), I set it visible, and I add it to mainPanel. It is not shown. I also tried to:

  • create another panel to contain JComponent, graphPanel, with BorderLayout, and add it to mainPanel.
  • set explicit size to graphPanel and objectComp
  • add objectComp directly to mainPanel and set it explicit size
  • call repaint() to all panel/objects after adding them

Nothing works. I see the first two panel, but objectComp never appeares. The only working way is to add objectComp directly to JFrame, but in this way I loose all the other objects and I nedd to see them.

/**
 */
public class Draw extends JComponent {
// graphic
}

/**
 */
public class MainClass extends JFrame {
/**
 * main panel
 */
private JPanel mainPanel = new JPanel();
/**
 * graphic panel
 */
private JPanel graphPanel = new JPanel();

/**
 * 
 */
public MainClass() {
    super();
    // maximize
    setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);

    // create form panel and add it to mainPanel
    createFormPanel();
    // create summary panel  and add it to mainPanel
    createSummaryPanel();

    setContentPane(mainPanel);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    pack();
}

/**
 * create JComponent
 */
private void createJComp() {

    draw = new Draw(data);
    draw.setVisible(true);
    graphPanel.setLayout(new BorderLayout());
    graphPanel.add(draw, BorderLayout.SOUTH);
    graphPanel.setSize(500, 500);
    draw.setSize(500, 500);
    graphPanel.setBackground(Color.BLUE);
    mainPanel.add(graphPanel);
    graphPanel.setVisible(true);
    SwingUtilities.invokeLater(new Runnable(){
        public void run()
        {
            validate();
            draw.repaint();
            graphPanel.repaint();
            repaint();
        }
    }); 
}
How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum