Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8199035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:05:21+00:00 2026-06-07T06:05:21+00:00

When adding a JPanel that has graphics to a JFrame , it’s working fine.

  • 0

When adding a JPanel that has graphics to a JFrame, it’s working fine. But when I try to add a JPanel in which I have added another JPanel with graphics, its not showing in the JFrame. Please see code below

package sample;

import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JFrame{
    public static void main(String[] args) {
        new Main();
    }

    public Main(){
        setTitle("Sample");
        setVisible(true);
        setSize(500,500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(new SamplePanel2());
    }
}

class SamplePanel2 extends JPanel{
    public SamplePanel2(){
        add(new JButton("Hi"));
        add(new SamplePanel());
    }
}
class SamplePanel extends JPanel {
    public SamplePanel(){
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawString("HHHHHHHHHHHH", 100, 100);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:05:22+00:00Added an answer on June 7, 2026 at 6:05 am

    Please do watch the constructor of the Main Class, make this your habbit to follow the sequence as shown in this example. First add components to the JFrame then only make calls like pack(), setSize() or setVisible(...), not before that.

    Always make it your habbit, that whenever you override paintcomponent() method, override getPreferredSize() method as well.

    And always put calls like pack()/setVisible(...) inside the EDT – Event Dispatch Thread. Please read Concurrency in Swing, for more detail on the topic.

    import java.awt.Dimension;
    import java.awt.Graphics;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Main extends JFrame{
        public static void main(String[] args) {
            new Main();
        }
    
        public Main(){
            setTitle("Sample");        
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setContentPane(new SamplePanel2());
            pack();        
            setVisible(true);
        }
    }
    
    class SamplePanel2 extends JPanel{
        public SamplePanel2(){
            add(new JButton("Hi"));
            add(new SamplePanel());
        }
    }
    class SamplePanel extends JPanel {
        public SamplePanel(){
        }
    
        @Override
        public Dimension getPreferredSize()
        {
            return (new Dimension(300, 300));
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawString("HHHHHHHHHHHH", 100, 100);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working in Java, and I have a JPanel in a JFrame. In that
One of my classes is returning a JPanel which is added on a JFrame
I have a Layout class which extends JFrame . Later I am adding RoadPanel
I am adding images to a JPanel but the images are getting cut off.
Adding Exceptional Handling is a good practise, but I have a doubt, In our
Adding strings localization files in iPhone bundle is very simple and perfectly fine but
I have code that displays a menu and 4 JButtons in a JFrame. I
I have Java application which adds JTextFields @ runtime to JPanel. Basically user clicks
I have developed a desktop application. Now in that app I want to add
I have a JPanel that I have created a MouseListener for and I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.