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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:52:13+00:00 2026-06-13T12:52:13+00:00

I have a MyFrame class which extends JFrame . I added components(Controls i.e buttons)

  • 0

I have a MyFrame class which extends JFrame . I added components(Controls i.e buttons) to this frame using the design options in NET BEANS. I have a MyCanvas class which extends JPanel with an overridden paintComponent() method . I am trying to add this component to MyFrame class. But when i add it and make it visible the canvas(JPanel) doesn’t show itself on the JFrame.
(First I was trying to add Mycanvas class extended by Canvas. But then i read a thread here to try and change it to JPanel. I didn’t work either. And for canvas i obviously use paint not paintcomponent())
My code is here below

MyCanvas Class

 public class MyCanvas extends javax.swing.JPanel{

 MyCanvas()
 {
      super();
 }

 @Override
 public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D graphics2D=(Graphics2D)g;

    graphics2D.drawRect(10, 10, 10, 10);

    graphics2D.drawOval(0, 0, 100, 100);
}

}

MyFrame

public class Myframe extends javax.swing.JFrame {

public Myframe() {

    initComponents();
  @SuppressWarnings("unchecked")
  +generatedcode by the designer

 private void RectangleActionPerformed(java.awt.event.ActionEvent evt) {
}

private void SquareActionPerformed(java.awt.event.ActionEvent evt) {
}

private void CircleActionPerformed(java.awt.event.ActionEvent evt) {
}

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
 java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Myframe().setVisible(true);
        }
    });
}
public void run() {
        new Myframe().setVisible(true);
    }
// Variables declaration - do not modify
private javax.swing.JButton Circle;
private javax.swing.JButton Rectangle;
private javax.swing.JButton Square;
// End of variables declaration
}

My Main Program

public static void main(String[] args) {

    MyCanvas c = new MyCanvas();
    Myframe f= new Myframe();//Works if used JFrame instead of MyFrame

    f.add(c);
    f.setVisible(true);
}

Basically i want to create a GUI in which i want Buttons which can trigger events and change what is drawn on a canvas. I tried it with an empty jframe. added the canvas/panel to the JFrame . It worked. Also I changed my Panel/Canvas and refresehed the frame. That also worked fine. But i am unable to do it like this.

  • 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-13T12:52:14+00:00Added an answer on June 13, 2026 at 12:52 pm

    The is that you are mixing creating JFrame with IDE and creating JPanel yourself, remember the IDE adds all components to JFrame in initComponents() which is ideally where you’d want to have your Canvas added.

    either create the JFrame and JPanel by yourself (without use of Netbeans GUI Builder)

    or

    You can use the Palette Manager of Netbeans to add your Component to the palette, then you can use it in the GUI builder as you would any other class:

    • How can I use a custom subclass of a Swing component?

    (Simply drag the Canvas class from the projects tree on to the JFrame in the GUI designer)

    To make sure your custom Canvas functions:

    • override getPrefferedSize(..) and return a size which fits

    Reference:

    • How to include custom panel with NetBeans GUI Builder?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class for my UI public class MyFrame extends JFrame{ JTextArea textArea;
Suppose I have this: class external { JFrame myFrame; ... class internal implements ActionListener
have written this little class, which generates a UUID every time an object of
I have created a class which name is Manager and I have a Frame
I need my JFrame to have a squared size. In the beginning my frame
Ok, I have the following code. public class MyProgressBar extends JPanel implements MyData, Serializable
I have a class let's say MyJFrame which represent the GUI of my application.
I have found a source code and i added it to my frame just
I have a data frame myframe: > myframe myframe exp obs 8 6 10
I have a JFrame on to which i have a JPanel, 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.