0

I code the following basic Java Swing example which is a JPanel with blue background:

public class Chart1 extends JFrame {
    
    private MainPanel1 main;    
    
    public Chart1() throws InterruptedException {
        
        setSize(600,500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        // CREATE AND ADD MAIN PANEL
        main = new MainPanel1();
        this.getContentPane().add(main, BorderLayout.CENTER);
        
        // DRAW WINDOW
        this.setVisible(true);
                        
    }
    
}
public class MainPanel1 extends JPanel {        
    
    public MainPanel1() {
        
        setBackground(Color.BLUE);
        
    }
}

I get the following result:

Blue JPanel

So far, so good.

Now I add a paint() method. The source code is as follows:

public class MainPanel1 extends JPanel {        
    
    public MainPanel1() {
        
        setBackground(Color.BLUE);
        
    }
    
    public void paint(Graphics g) {
    }

}

Then even without doing anything in paint() I get a grey background, why? how can I fix this?

JPanel with paint

M.E.
  • 4,955
  • 4
  • 49
  • 128
  • 2
    You could put `super.paint(g)` inside your overridden paint method. That will go ahead with the normal paint code that fills in the background colour. Also usual advice is to override `paintComponent` rather than `paint`. – khelwood Sep 27 '20 at 22:26

2 Answers2

1

The answer is that paint (a java 1.0 - 1.1 method) calls paintBackground in JComponents. When you overrode it, it isn't calling all of the swing paint methods. But if you add super.paint(g), it will look like it did before.

Also - please note that the default ContentPane in JFrame is already a JPanel. Rather than replacing the ContentPane with your JPanel, you could just call:

((JPanel) myFrame.getContentPane()).setBackground(Color.blue);
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • Thanks, this clarifies. For the ContentPane/JPanel, this is a simplified version for SO, the actual code contains 3 panels so I need this JPanel. – M.E. Sep 27 '20 at 22:31
  • 1
    @M.E. Glad this has helped you. One piece of advice: In general (and when/where possible), it is better to use out of the box Swing / View components (no extending) and put the entire control of the GUI in its own class; it makes debugging easier. Though if you are creating reusable swing components (For instance ColorButton (A Swing extension for allowing the user to configure a color property, in a config window for example) here: https://sourceforge.net/p/tus/code/HEAD/tree/tjacobs/ui/ex/) it makes sense for the component itself to contain the logic – ControlAltDel Sep 27 '20 at 23:13
1

You should not be overriding paint but paintComponent. The problem would still occur so you need to invoke super.paintComponent(g)

So Change your paint method to the following.

public class MainPanel1 extends JPanel {        
    
    public MainPanel1() {
        setBackground(Color.BLUE);
    }
    
    public void paintComponent(Graphics g) {
         // The following statement ensures that
         // the background color will be applied
         // as well as other preparations for 
         // doing graphics.
 
         super.paintComponent(g);
         // If you have other graphics
         // code, add it here.
   }

}

And do not subclass JFrame in the Chart1 class. It is bad practice. Use an instance.

JFrame frame = new JFrame();
WJS
  • 36,363
  • 4
  • 24
  • 39
  • Helpful comment. I also saw this after reading your answer: https://stackoverflow.com/questions/12175174/paintcomponent-vs-paint-and-jpanel-vs-canvas-in-a-paintbrush-type-gui – M.E. Sep 28 '20 at 01:47