

As you see these picture, the background grid lines and black rectangle are not updated after the menu item is closed. How can I update it? In C# there is an event handler to make it updated automatically, but I am newbie to Java swing GUI application.
Here is the code:
public void paint(Graphics g) {
super.paintComponents(g);
MainDisplayForm mD = new MainDisplayForm();
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLACK);
int gridWidth = 1240;
int gridHeight = 400;
g2.fillRect(20, 50, gridWidth, gridHeight);
g2.setColor(Color.darkGray);
paintGrid(g2,gridWidth, gridHeight);
g2.setColor(Color.red);
Line2D line = new Line2D.Float(20, 50, 250, 260);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//g2.draw(line); //pending
g2.dispose();
}
private void paintGrid(Graphics g, int gridWidth, int gridHeight)
{
for(int i=20; i<gridWidth+20; i=i+10)
{
g.drawLine(i, 50, i, gridHeight+49);
}
for(int i=50; i<gridHeight+50; i=i+10)
{
g.drawLine(20, i, 1259, i);
}
}
//Thanks for comments!! Here is the event handler to add.
This must be added to every memu item drawn over the grid
private void jMenu2MenuDeselected(javax.swing.event.MenuEvent evt) {
repaint();
}
When the menu is closed call reprint to the window.
Don’t call
paint(getGraphics()). Instead callrepaint()because this informs the super component that it needs to redraw as well.