[Solved] Java graphs not appearing


I have learned that extending JFrame and JPanel is bad

But if you need to draw in the GUI, you often need to extend JPanel, and that’s exactly what you should do here. For example the code where you draw with a Graphics object should be inside of the JPanel’s protected void paintComponent(Graphics g) method. So your DrawGraph class should extend JPanel, and it should override paintComponent, complete with an @Override annotation on top, and should call the super method:

public class DrawGraph extends JPanel {
    // .... variables, constructor, methods here...

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // your painting code here
    }

Regarding,

guiframe.getContentPane().add(DrawGraph.paintComponent());

I have no idea what you’re trying to do here, but as you know, it’s wrong, delete that line, don’t even approach anything like it in the future. It almost looks like you’re trying to call a component’s paintComponent method directly, something that you will almost never want to do.

Regarding:

These lines below are my other class, “DrawGraph”. I’m not really good at coding, but I’m sure I did not call the class properly, just don’t know how…

but that’s exactly what the tutorials are for. If you’ve not gone through them yet, please do so now:

2

solved Java graphs not appearing