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

  • Home
  • SEARCH
  • 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 8351707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:43:25+00:00 2026-06-09T08:43:25+00:00

I’m having a problem overriding the paint() method in my JPanel subclass, ChordEditor. Even

  • 0

I’m having a problem overriding the paint() method in my JPanel subclass, ChordEditor. Even when I override the paint() method, add it to the frame, and call repaint() the paint() method is never called. The printout “entering paint function” does not print. Can anyone help me with this?

My ChordEditor class:

public class ChordEditor extends JPanel{

    ArrayList<Chord> chordArray = new ArrayList<Chord>();

    public ChordEditor() {
        this.repaint();
    }

    @Override
    public void paint(Graphics g) {
        System.out.println("entering paint function");
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        Toolkit.getDefaultToolkit().sync();
        System.out.println("drawing line");
        g2d.drawLine(10, 10, 40, 40);
        g.dispose();
    }
}

Where I add it to the JFrame:

ChordEditor ce = new ChordEditor();
m_frame.getContentPane().removeAll();
m_frame.add(ce);
m_frame.getContentPane().repaint();
  • 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-09T08:43:26+00:00Added an answer on June 9, 2026 at 8:43 am

    The fact that you issue a repaint immediately after you added the component suggests that you do so on an already visible frame. Perhaps you should validate the frame first. The following works for me:

    import javax.swing.*;
    import java.awt.*;
    
    class ChordEditor extends JPanel {
    
        public ChordEditor() {
            this.repaint();
        }
    
        @Override
        public void paint(Graphics g) {
            System.out.println("entering paint function");
            super.paint(g);
            Graphics2D g2d = (Graphics2D) g;
            Toolkit.getDefaultToolkit().sync();
            System.out.println("drawing line");
            g2d.drawLine(10, 10, 40, 40);
            //g.dispose();
        }
    
        public static void main(String[] args) {
            JFrame m_frame = new JFrame();
            m_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            m_frame.setSize(600, 400);
            m_frame.setVisible(true);
            ChordEditor ce = new ChordEditor();
            m_frame.getContentPane().removeAll();
            m_frame.add(ce);
            m_frame.getContentPane().repaint();
            m_frame.validate();
        }
    }
    

    As already stated in comments to your question, you should override paintComponent instead of paint, and you should not call dispose as you didn’t create this Graphics context.

    The calls to sync and repaint should be unneccessary as well, I guess you added them in an attempt to solve this issue here. So you might remove them once things work for you. You also might consider adding all components to the frame before making it visible. Doing so will cause an implicit validation and is the more common approach to windows with a fixed configuration of controls they contain.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I am currently running into a problem where an element is coming back from
I want to construct a data frame in an Rcpp function, but when I
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.