Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why does my canvas not display in side a gridbaglayout?  RSS feed

 
Nicholas Turner
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hers my simple class
class ClockPicture extends Canvas {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;

g2.setPaint(new Color(255,0,0));
g2.fillOval(10,10, getWidth()-10,getHeight()-10);
}
}
When I add it straight to the main panel which is a gridlayout my circle appears just fine
But when I add it to a class that extends a panel only the JButton appears but not the canvas class. Its using a GridBagLayout
thanks,
nick
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ClockPicture should also override the getPreferredSize() method to return a Dimension object to tell containers what size it should be displayed as. GridLayout worked because all components inside a GridLayout get the size of the largest object in the entire grid. GridBagLayout uses the preferred size of the component, unless you specify otherwise in a GridBagConstraints object.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!