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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:53:50+00:00 2026-06-10T15:53:50+00:00

I have a JFrame with 2 JPanel in it: a PaintPanel (with a paint()

  • 0

I have a JFrame with 2 JPanel in it: a PaintPanel (with a paint() method) and a ButtonPanel (with buttons). When I invoke the repaint() of the PaintPanel (but clicking the button) the button of the ButtonPanel is being painted in the PaintPanel! It isn’t clickable or anything, it is just there.

I tried to recreate the problem with this code:

public class Main {

    public static void main(String[] args) {
        JFrame frame = new JFrame("frame");
        frame.setSize(400,400);
        frame.setLayout(new GridLayout(2,1));
        PaintPanel paint = new PaintPanel();
        ButtonPanel buttons = new ButtonPanel(paint);
        frame.add(paint);
        frame.add(buttons);
        frame.setVisible(true);
    }
}

public class PaintPanel extends JPanel{
    public void paint(Graphics g){
        g.drawRect(10, 10, 10, 10);
    }
}

public class ButtonPanel extends JPanel implements ActionListener{

    private PaintPanel paintPanel;

    public ButtonPanel(PaintPanel paintPanel){
        this.paintPanel=paintPanel;
        JButton button = new JButton("button");
        button.addActionListener(this);
        add(button);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        paintPanel.repaint();           
    }
}

This sould recreate the problem I have (sorry for the odd code markings, can’t seem to get it right).

I really hope one of you knows what is happening here because i don’t…

  • 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-10T15:53:51+00:00Added an answer on June 10, 2026 at 3:53 pm

    First of all, you should override paintComponent() instead of paint(). It’s part of the best practices in Swing when it comes to do some panel customization.

    Secondly, here is the code that works for me (I don’t know why yours doesn’t though :S):

    public class Main {
    
        public static void main(String[] args) {
    
            JFrame frame = new JFrame("frame");
            frame.setSize(400, 400);
            // frame.setLayout(new GridLayout(2, 1));
            PaintPanel paint = new PaintPanel();
            ButtonPanel buttons = new ButtonPanel(paint);
            // frame.add(paint);
            // frame.add(buttons);
            frame.setVisible(true);
    
            JPanel pan = new JPanel(new BorderLayout());
            pan.add(paint);
            pan.add(buttons, BorderLayout.SOUTH);
            frame.add(pan);
    
        }
    }
    
    class PaintPanel extends JPanel {
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(new Color(new Random().nextInt()));
            g.drawRect(10, 10, 10, 10);
        }
    }
    
    class ButtonPanel extends JPanel implements ActionListener {
    
        private final PaintPanel paintPanel;
    
        public ButtonPanel(PaintPanel paintPanel) {
    
            this.paintPanel = paintPanel;
            JButton button = new JButton("button");
            button.addActionListener(this);
            add(button);
        }
    
        @Override
        public void actionPerformed(ActionEvent arg0) {
            if (getParent() != null) {
                getParent().repaint();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JFrame and a Jpanel over that in which various buttons are
I have JFrame , and I added my JPanel class with paintComponent() method. For
I have a Jframe window inside which there is a button. By clicking the
I have a JFrame(frm) in which I've added a JPanel (pnl1)..which in turn has
I have a JFrame with JScrollPane in it. I have JPanel inside a scrollPane.
I have a JFrame with an associated JPanel which fill the screen, both having
I have to views: MainWindowView (extends JFrame) ScanOptimisationView (extends JPanel) So, I have the
Scenario: I have one JFrame and JPanel added to that JFrame . on this
I have a JFrame with a JPanel on it ( JPanel is private in
So i have a Jframe with a JPanel in it, and there are some

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.