Where to put repaint
I am writing a GUI that consists of a JFrame with one JPanel inside its content pane. I want to display monospaced text inside the JPanel. In order to determine what size I require for the JPanel, I need to find out the width and height of a character of that font. In order to do this, I need to get a FontRenderContext object inside the JPanel's paint(Graphics) method. Here is a snippet of my code:
publicclass TextGUIextends JFrame{
privateint numRows;
privateint numCols;
privateint cellHeight;
privateint cellWidth;
private TextPanel panel;
privatestaticfinal Font FONT =new Font("monospaced", Font.PLAIN, 12);
public TextGUI(int numRows,int numCols){
this.numRows = numRows;
this.numCols = numCols;
panel =new TextPanel();
int pWidth = cellWidth * numRows;
int pHeight = cellHeight * numCols;
panel.setPreferredSize(new Dimension(pWidth, pHeight);// ****
}
// ... accessors and methods for printing text ...
class TextPanelextends JPanel{
public TextPanel(){
super();
repaint();// ****
}
publicvoid paint(Graphics g){
if(cellWidth == 0){
FontRenderContext frc = ((Graphics2D)g).getFontRenderContext();
Rectangle2D r = FONT.getStringBounds("X", frc);
LineMetrics lm = FONT.getLineMetrics("X", frc);
cellWidth = (int)r.getWidth();
cellHeight = (int)lm.getAscent() + (int)lm.getDescent();
}
// Now display text...
}
}
}
Everything works except that paint does not seem to be called when I invoke repaint() inside TextPanel's constructor, and thus cell dimensions are not determined. This means that when I set the preferred size of the TextPanel from inside TextGUI's constructor, the dimension used is (0, 0). Do I need to restructure my code, or do I just need to call repaint in a different place? Is there a better way to determine font size information? [In my real code, different fonts are possible]

