I'm not able to see content on JFrame ,when JFrame shows up.

import java.awt.*;

import javax.swing.*;
import java.awt.event.*;
public class GraphicUse extends JPanel
{
public void PaintComponent(Graphics gr)
{   
    super.paintComponents(gr);
    this.setBackground(Color.BLUE);
    gr.setColor(Color.WHITE);
    gr.fillRect(50, 50, 100, 100);
    gr.setColor(new Color(139,38,190));
    gr.fillRect(50, 70, 100, 100);
    gr.setColor(Color.RED);
    gr.drawString("This is a new String", 50, 90);  
}

    public static void main(String[] args) 
    {
    }

}

And displaying it in another class which extends Frame

JFrame newframe = new JFrame("Color Frame");
             newframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         
             GraphicUse gu = new GraphicUse();           
             newframe.add(gu);
             newframe.setSize(250,300);
             newframe.setVisible(true);

Dani AI

Generated

As already flagged, the painting callback must be named exactly paintComponent (lowercase p, singular "Component") and the compiler will catch a wrong signature if you add @Override. Also avoid calling super.paintComponents(...) — the correct call inside your override is super.paintComponent(g). was right to suggest providing a preferred size and using frame.pack() rather than forcing sizes.

Quick checklist to fix the blank window:

  • Give the panel a proper paintComponent(Graphics g) override and annotate it with @Override.
  • Call super.paintComponent(g) first.
  • Move one-time setup (like setBackground) into the constructor and make the panel opaque.
  • Provide size via setPreferredSize(...) or override getPreferredSize() and call frame.pack().
  • Create the GUI on the Event Dispatch Thread with SwingUtilities.invokeLater.
  • Don’t mix AWT Frame with Swing JFrame — stick to Swing components.

A minimal, corrected example that follows these points:

import javax.swing.*;
import java.awt.*;

public class GraphicUse extends JPanel {
    public GraphicUse() {
        setBackground(Color.BLUE);
        setPreferredSize(new Dimension(250, 300));
        setOpaque(true);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g.create();
        try {
            g2.setColor(Color.WHITE);
            g2.fillRect(50, 50, 100, 100);
            g2.setColor(new Color(139, 38, 190));
            g2.fillRect(50, 70, 100, 100);
            g2.setColor(Color.RED);
            g2.drawString("This is a new String", 50, 90);
        } finally {
            g2.dispose();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame f = new JFrame("Color Frame");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new GraphicUse());
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        });
    }
}

If it still shows blank, check for runtime exceptions in the console, verify the panel is actually added before pack/setVisible, and confirm you did not accidentally extend java.awt.Frame elsewhere.

Recommended Answers

All 5 Replies

This is why the @Overrride annotation was invented!
If you prefix that annotation to your PaintComponent method you will discover that it doesn't override anything. Here's a hint: Java names are case sensitive.

The method PaintComponent(Graphics) of type GraphicUse must override or implement a supertype method

Exactly. It's not overriding because the superclass does not have a method called PaintComponent. Here's a hint: Java names are case sensitive.

+1 for Java Naming Conventions

  • and very important details don't to call, never super.paintComponents(gr); but super.paintComponent(gr);

  • paintComponents required little bit more effort and knowledge than simple paintComponent, then is easier to override paintChildren

__________________________

  • override getPreferredSize for public class GraphicUse extends JPanel, don't forget to add @Overrride annotation too, then all sizing is quite useless and contraproductive, use frame.pack() instead of code line newframe.setSize(250,300);

+1 for spotting the "s"

Here's an updated hint: Java names are case sensitive, and "Component" is not the same as "Components"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.