I need to do paint a graphics into a nested panel. I am able to paint in a frame but when I add the class for painting into another JPanel, I see nothing.

I know how to create nested panels but when I do it with a panel containing my paint method, I dont see any picture.
For eg I add my panel for painting this way because its supposed to be nested in mypanel. All other nested panels without a paint works perfectly.
mypanel.add(new paintHang());

This is my main frame with a call to the paint. It is able to paint in a frame but when I nest it like above, I see no picture

public class MainFrame extends JFrame {
    
     public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable(){
            public void run(){
                new MainFrame();

            }});
  }

     public MainFrame(){
         
        super("Hangman Game");

        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(600,600));
        setResizable(false);
        setAlwaysOnTop(true);

        
        add(new paintHang());//able to paint
        //add(new myPanel()); unable to paint thru nested

        pack();
        setVisible(true);

     }

This is my painting class.

public class paintHang extends JPanel{

    public paintHang(){
        
        setBorder(BorderFactory.createLineBorder(Color.black));
    }
    public void paintHang(Graphics g){
    g.fillRect(10, 250, 150, 20);
    g.fillRect(40,70,10,200);
    g.fillRect(40,70,60,10);
    g.setColor(Color.yellow);
    g.fillRect(95,70,5,25);
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawString("This is my custom Panel!",10,20);
        paintHang(g);
    }

}

I don't think I understand. There is no myPanel class (presumably it extends JPanel and you haven't posted it). I see no difference between the two lines below other than the fact that the two classes have different names. There must be something in myPanel that is different from paintHang, but since you haven't posted it, I can't speculate what the problem is. The code you posted works, and it worked for you. Please post the myPanel class so we can see what DOESN'T work. I also am unclear what you are referring to by "nested". What is nested in what?

add(new paintHang());//able to paint
        //add(new myPanel()); unable to paint thru nested

i did not post myPanel class because I know that it is working perfectly when I tested it. I cant possibly post my whole code here, unless someone wants to do my homework for me:). There is a difference.

add(new myPainthang());

calls directly the class which extends JPanel and does painting.

add(new myPanel());

calls the panel where I nest the class/panel which extends JPanel and does paiting

i did not post myPanel class because I know that it is working perfectly when I tested it. I cant possibly post my whole code here, unless someone wants to do my homework for me:). There is a difference.

add(new myPainthang());

calls directly the class which extends JPanel and does painting.

add(new myPanel());

calls the panel where I nest the class/panel which extends JPanel and does paiting

They both call constructors of classes which extend JPanel. I don't understand the nesting part. Are you trying to add an instance of myPaintHang to an instance of myPanel, then add that instance of myPanel to an instance of MainFrame? So you have a JPanel within a JPanel within a JFrame? Is that the goal?

yes I knida want to have a Jpanel which paints inside a Jpanel in a JFrame. The painting works when inside just a JFrame but doesnt when I put it inside a JPanel

yes I knida want to have a Jpanel which paints inside a Jpanel in a JFrame. The painting works when inside just a JFrame but doesnt when I put it inside a JPanel

Perhaps have something like this:

myPanel mypanel = new myPanel ();
        mypanel.setLayout(new GridLayout (1,1));
        mypanel.add (new paintHang ());
        this.getContentPane().add (mypanel);

instead of this?

add(new paintHang());
        //add(new myPanel());

If the paintHang object is supposed to be inside of the myPanel object, add it to the myPanel object rather than the MainFrame object.

I guess I gotta post my whole code to let u guys understand....

Main Frame code:

public class MainFrame extends JFrame {
    
     public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable(){
            public void run(){
                new MainFrame();

            }});
  }

     public MainFrame(){
         
        super("Hangman Game");

        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(600,600));
        setResizable(false);
        setAlwaysOnTop(true);

        add(new MainPanel());
        pack();
        setVisible(true);

     }
     
     
}

Main Panel Code:

public class MainPanel extends JPanel implements ActionListener{
    private Button a[];
    private JButton newGame, loadFileDir, quit;
    private JRadioButton onePlayer, twoPlayer;
    private JComboBox diff;

    private paintHang draw = new paintHang();
          
    public MainPanel (){
        
        super();
        
        //create menu panel
        JPanel menuPanel = new JPanel();
        menuPanel.setBorder(BorderFactory.createTitledBorder("Menu"));
        menuPanel.setBackground(Color.orange);
        menuPanel.setPreferredSize(new Dimension(150, 550));

        //create game display panel
        JPanel gamePanel = new JPanel();
        gamePanel.setBorder(BorderFactory.createTitledBorder("Game Display"));
        gamePanel.setBackground(Color.white);
        gamePanel.setPreferredSize(new Dimension(400, 550));
      
        //create game status panel
        JPanel statusPanel = new JPanel();
        statusPanel.setBorder(BorderFactory.createTitledBorder("Game Status"));
        statusPanel.setBackground(Color.yellow);
        statusPanel.setPreferredSize(new Dimension(150, 150));

        //main panel bg color
        setBackground(Color.black);

        //Text labels for game status
        JLabel difficulty = new JLabel("Difficulty: ");
        JLabel tries = new JLabel("Tries Remaining: ");
        JLabel hint = new JLabel("Hint: ");
        JLabel currLevel = new JLabel("Level: ");

        //create game buttons
        newGame = new JButton("New Game");
        loadFileDir = new JButton("Load Filedir..");
        quit = new JButton("Quit");
        newGame.setPreferredSize(new Dimension(100,20));
        newGame.setToolTipText("Start New Game");
        newGame.addActionListener(this);
        loadFileDir.setPreferredSize(new Dimension(100,20));
        loadFileDir.setToolTipText("Choose File dir to load words");
        loadFileDir.addActionListener(this);
        quit.setPreferredSize(new Dimension(100,20));
        quit.setToolTipText("Exit game");
        quit.addActionListener(this);

        //radio buttons for one/two player modes
        onePlayer = new JRadioButton ("One Player", true);
        twoPlayer = new JRadioButton ("Two Players");
        onePlayer.addActionListener(this);
        twoPlayer.addActionListener(this);

        //group radio buttons
        ButtonGroup group = new ButtonGroup();
        group.add (onePlayer);
        group.add (twoPlayer);

        //combo box for difficulty settings
        String[] level = {"Level 1", "Level 2", "Level 3", "AutoLevel"};
        diff = new JComboBox (level);
        diff.setSelectedIndex(-1);
        diff.setPreferredSize(new Dimension(20,20));
        diff.setSize(new Dimension(20,20));

        //create a button keyboard in game display
        a = new Button[26];

        StringBuffer buffer;
		for (int i = 0; i <26; i++) {
		    buffer = new StringBuffer();
			buffer.append((char)(i+65));
			a[i] = new Button(buffer.toString());
			a[i].addActionListener(this);
            gamePanel.add(a[i]);
            
        }

        //menu panel, buttons, box layout
        menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.PAGE_AXIS));
        menuPanel.add (Box.createRigidArea(new Dimension (0, 5)));
        menuPanel.add (newGame);
        menuPanel.add (Box.createRigidArea(new Dimension (0, 10)));
        menuPanel.add (loadFileDir);
        // menuPanel.add (Box.createRigidArea(new Dimension (0, 10)));
        // menuPanel.add (difficulty);
        // menuPanel.add (diff);
        menuPanel.add (Box.createRigidArea(new Dimension (0, 10)));
        menuPanel.add (onePlayer);
        menuPanel.add (twoPlayer);
        menuPanel.add (Box.createRigidArea(new Dimension (0, 10)));
        menuPanel.add (quit);
        menuPanel.add (Box.createRigidArea(new Dimension (0, 10)));
        
        //game status panel with labels, box layout
        statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.PAGE_AXIS));
        statusPanel.add (currLevel);
        statusPanel.add (Box.createRigidArea(new Dimension (0, 5)));
        statusPanel.add (hint);
        statusPanel.add (Box.createRigidArea(new Dimension (0, 5)));
        statusPanel.add (tries);
        statusPanel.add (Box.createVerticalStrut(320));

        gamePanel.add(draw);

        menuPanel.add(statusPanel);
        add(menuPanel);
        add(gamePanel);
        
    }

and finally the painting class:

public class paintHang extends JPanel{

    public paintHang(){
        
        setBorder(BorderFactory.createLineBorder(Color.black));
    }
    public void paintHang(Graphics g){
    g.fillRect(10, 250, 150, 20);
    g.fillRect(40,70,10,200);
    g.fillRect(40,70,60,10);
    g.setColor(Color.yellow);
    g.fillRect(95,70,5,25);
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawString("This is my custom Panel!",10,20);
        paintHang(g);
    }

}

So now that my whole code is here, someone tell me why I dont see any graphics painted in my nested panel.

It almost certainly has to do with the fact that you have not selected a LayoutManager for gamePanel and you have not selected a minimum size for hangPanel, so it's setting the size of hangPanel to be very small. Pick a layout manager for gamePanel. I'd split gamePanel in two: one panel for the buttons, one panel for painting what you want to paint (hangPanel). Use a BorderLayout and add the button panel to the top of gamePanel, then the larger bottom part of gamePanel would be hangPanel. You can set vertical sizes that way that will be respected by the LayoutManager. Not specifying a LayoutManager can cause default behavior that you don't want.

This article has been dead for over six months. Start a new discussion instead.