Q: Refreshing JPanel in Java I'm trying to draw the line via button.
public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    g2 = (Graphics2D) g;
    g2.setColor(Color.BLACK);
    g2.setStroke(new BasicStroke(2));
    for (int i=0;i<18;i++)
    {
        g2.draw(new Line2D.Double(2+i*20, 0, 2+i*20, 260));
        g2.draw(new Line2D.Double(0, 2+i*20, 360, 2+i*20));
    }
    g2.setColor(Color.RED);
}
public void drawDiagonallyLineDownLeft()
{
    int newXCoord=xDrawCoord+20;
    int newYCoord=yDrawCoord+20;
    g2.drawLine(xDrawCoord, yDrawCoord, newXCoord, newYCoord);
    xDrawCoord=newXCoord;
    yDrawCoord=newYCoord;
    repaint();
}

The drawDiagonallyLineDownLeft method paints Line. I tried to use it in constructor and it works ok.
Here is listener of JButton
public void actionPerformed(ActionEvent arg0) 
            {
                panel.drawDiagonallyLineDownLeft();
                panel.revalidate();
                panel.repaint();
            }

It also works ok. I printed message in Console in method, at it was printed ok. But there is no line, when I press the button. I think, that I should refresh it somehow, I used all methods I know, but it still doesn't works.

A: When you call repaint all drawing are erased, you have to paint in the method paintComponent(Graphics g)
paintComponent(Graphics g) is called automatically in some cases (window resize ect.)
