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 9055605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:59:45+00:00 2026-06-16T13:59:45+00:00

I have a JFrame created with GUI builder of Netbeans, which contains a JPanel

  • 0

I have a JFrame created with GUI builder of Netbeans, which contains a JPanel only. I have created a method getPanel for getting a reference to this JPanel:

public class ShowDrawings extends JFrame {

    public ShowDrawings() {
        initComponents();
    }

    public JPanel getPanel(){
        return panel;
    }

    private JPanel panel;
}

In my main function I am doing:

public class Main {
    public static void main(String[] args){
        ShowDrawings sd = new ShowDrawings();
        sd.setSize(800, 600);
        Graphics g = sd.getPanel().getGraphics();
        g.setColor(Color.BLACK);
        g.drawOval(400, 300, 50, 50);
        sd.getPanel().paint(g);
        sd.repaint();
        sd.setVisible(true);
    }
}

But it does not draw anything. Please help me.
I have looked some related questions but they are all suggesting extending JPanel and overriding its paint method. But I did not want to do that way.
Thanks.

  • 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-16T13:59:46+00:00Added an answer on June 16, 2026 at 1:59 pm

    I have looked some related questions but they are all suggesting
    extending JPanel and overriding its paint method. But I did not want
    to do that way

    You should not override JPanel paint() method, rather paintComponent(..). This is best practice and should be done if you want code that will not produce anomalies. Also doing it in your current approach (as you have seen) makes creating persistent drawings a lot harder as they are wiped away on repaint()

    Rather extend JPanel and override paintComponent(Graphics g) not forgetting to call super.paintComponent(g) as first call in overridden paintComponent(..) method. Also dont forget to override getPreferredSize() of JPanel so that we can return correct dimensions and pack() may be called on JFrame (+1 to @mKorbels comment):

    Here is some example code:

    enter image description here

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class Test {
    
        public Test() {
            initComponents();
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Test();
                }
            });
        }
    
        private void initComponents() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel testPanel = new JPanel() {
                @Override
                protected void paintComponent(Graphics grphcs) {
                    super.paintComponent(grphcs);
    
                    Graphics2D g2d = (Graphics2D) grphcs;
    
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    
                    g2d.setColor(Color.GREEN);
                    //g2d.drawOval(10,10,100,100);//I like fill :P
                    g2d.fillOval(10,10,100,100);
    
                }
    
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(150, 150);
                }
            };
    
            frame.add(testPanel);
    
            frame.pack();
            frame.setVisible(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created some custom JPanel classes using the NetBeans GUI Builder. Next, I
I have created a JFrame form using netbeans GUI builder and place buttons on
I have created one GUI in which I have used a JFrame. How should
I have created a simple GUI which includes a JTable. This table may be
I have created jframe in which jpanels are added dynamically what i can't do
I have a GUI window that I've created using Netbeans. I then ported the
I've created GUI for my application using Netbeans' GUI Builder. I am trying to
I have a class which extends JFrame and forms the GUI of my program.
I created a array of JPanels which contains a common JLabel class gui {
I am using swing to create my GUI. J have a JFrame containing one

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.