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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:47:15+00:00 2026-05-26T20:47:15+00:00

I have a problem making an inner class that extends from JPanel to draw

  • 0

I have a problem making an inner class that extends from JPanel to draw anything on it. I overrided the paintComponent method, and whatever I set to draw from here works fine, but using another method to draw does not work.

Here is my inner class code:

private class Plot extends JPanel {

    public Plot() {
        this.setBackground(Color.WHITE);
    }

    @Override
    public void paintComponent(Graphics graphic) {
        super.paintComponent(graphic);
        Graphics2D graphic2d = (Graphics2D) graphic;

        graphic2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        graphic2d.drawOval(0, 0, this.getWidth() - 1, this.getHeight() - 1);
    }

    public void drawTitle(final String title) {
        Graphics2D graphic2d = (Graphics2D) this.getGraphics();
        graphic2d.setColor(Color.red);
        graphic2d.drawString(title, 1, 10);
    }
}

Notice the drawTitle method. I just want a custom text to be shown. In my outer class which extends from a JFrame I create an instance of this inner class like this:

private Plot plot;

/** Creates new form GraphicsView */
public GraphicsView() {
    initComponents();
    plot = new Plot();
    this.add(plot, BorderLayout.CENTER);
}

public void drawTitle(final String title) {
    this.plot.drawTitle(title);
}

I even create a convenient method to call the inner class drawTitle method (with the same name). I do this because I want this JFrame outer class to be visible on button click, once it is visible (which ensures the init of the graphics) I call the outer class drawTitle which in turn calls the inner class method with the same name and where the string show be drawn… but this does not work, I can’t see it on the panel. Here is my button click event:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    GraphicsView view = new GraphicsView();
    view.setVisible(true);
    view.drawTitle("Hello");
}

Thanks in advance, I will appreciate any help. 🙂

  • 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-05-26T20:47:16+00:00Added an answer on May 26, 2026 at 8:47 pm

    I overrided the paintComponent method, and whatever I set to draw from here works fine

    Well, there is the answer to the question. Do all your drawing from the paintComponent() method.

    but using another method to draw does not work.

    Don’t use the getGraphics() method. You should only ever use the Graphics objects passed to the paintComponent() method.

    You can’t control when Swing repaints() a component. Therefore every time the component is repainted the paintComponent() method is invoked and your other custom painting code will be lost.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Activity Class with an inner protected class that extends AsyncTask. I
Here is my problem. I have created a pretty heavy readonly class making many
please i have problem in making flip animation when going from a view to
I have a problem with whether making class members constant or not. Let me
I have a problem with making changes to an object from within another model
I have a problem that is surely familiar to many: I'm making a call
I have a problem that is making me crazy. On my page I have
My only problem is making them line up three-across and have equal spacing. Apparently,
I am making an application with Java Swing and i have a problem. I
I have a very big problem. I am making a CRM (Costumer Relationship Management)

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.