Java Swing, no puedo ver el error

I am at a very strange problem, I have a customized JPanel, which I want to draw a circle, but nothing happens ... here is my source, hope somebody sees the mistake, I can't find it.

import javax.swing.JPanel;

public class CircleView extends JPanel {

public CircleView() {}

    public void paintComponent(Graphics g) {
    g.drawOval(10, 10, 50, 50);

preguntado el 08 de noviembre de 11 a las 16:11

How are testing the CircleView? -

I am adding it to a layeredPane. I am able to add images to it, but as soon as I add the CircleView, nothing happens -

2 Respuestas

that not true at all

Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
        at KondorExport.Util.Helping.CustomComponent12.<init>(
        at KondorExport.Util.Helping.CustomComponent12$
        at java.awt.event.InvocationEvent.dispatch(
        at java.awt.EventQueue.dispatchEvent(
        at java.awt.EventDispatchThread.pumpOneEventForFilters(
        at java.awt.EventDispatchThread.pumpEventsForFilter(
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(
        at java.awt.EventDispatchThread.pumpEvents(
        at java.awt.EventDispatchThread.pumpEvents(
Caused by: java.lang.RuntimeException: Uncompilable source code - class CircleView is public, should be declared in a file named
        at KondorExport.Util.Helping.CircleView.<clinit>(
        ... 10 more

1) remove constructor

2) agregar super.paintComponent(g);

this one can run

class CircleView extends JPanel {

    private static final long serialVersionUID = 1L;

    public Dimension getPreferredSize() {
        return new Dimension(100, 100);

    public void paintComponent(Graphics g) {
        int margin = 10;
        Dimension dim = getSize();
        g.drawOval(margin, margin, dim.width - margin * 2, dim.height - margin * 2);

respondido 08 nov., 11:20

I read some howtos and I assumed, that what I wrote was enough, will try that ^^ - Markus

seems as if, the class works fine. Adding it to the layeredPane is the problem ... - Markus

It's because your component has no dimensions, hence why the sscce provided by @mKorbel uses some "magic" dimensions when defining the component's preferred size.

contestado el 23 de mayo de 17 a las 15:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.