I have a problem. I used Gridbaglayout in my JFrame. One of the component is JPanel.
I wanted to draw a gridline as a background for my JPanel. e.g in the program below, it supposed to produce 3 vertical and 3 horizontal lines, however it only shows 2 vertical and 2 horizontal line. the last line was not shown.
Another problem was that, it seems that the size of the JPanel was bigger that what I have set. I noticed this by the length of the line which is shorter that the JPanel white background.
public class drawLayout extends JComponent
{
public Dimension getPreferredSize() {
return new Dimension(600, 600);
}
public int getY() {
return 0;
}
public int getX() {
return 0;
}
@Override public void paintComponent(Graphics g)
{
g.setPaint(Color.GRAY);
for (int i = 0; i <= getSize().width; i += 300)
{
g2.drawLine(i, 0, i, getSize().height);
}
for (int i = 0; i <= getSize().height; i += 300)
{
g2.drawLine(0,i, getSize().width, i);
}
}
}
EDIT:
http://www.freeimagehosting.net/image.php?1af16edc28.jpg
The first problem solved (the gridlines were shown on JPanel). The other problem puzzled me. As you can see on the image attached, the size of the JPanel seems to be more than 600 when look at the length of the grid (marked as red box). How can I solve this so the gridline background look nice without any extra white space outside the gridline?