I'm working on a project in swing, and I have a class that subclasses JComponent, but does not override any of its methods. Later, in a subclass of that class I call the following code:
System.out.println(fImage.getHeight() + " " + fImage.getWidth());
propertyFrontLabel = new JLabel(new ImageIcon(fImage));
propertyBackLabel = new JLabel(new ImageIcon(bImage));
propertyFrontLabel.setSize(fImage.getWidth(), fImage.getHeight());
Dimension d = propertyFrontLabel.getSize();
this.setPreferredSize(d);
this.setSize(d);
System.out.println(d.getSize());
System.out.println(this.getSize());
add(propertyFrontLabel);
Those print lines print out the following:
479 296
java.awt.Dimension[width=296,height=479]
java.awt.Dimension[width=0,height=0]
I don't have any idea what is going on. Why is the size of this still 0,0?
You've possibly using a null layout, or you're calling 'getSize()' on your component prior to rendering it or prior to calling 'pack()' on the enclosing top-level Window, or you're not overridding the component's getPreferredSize()
method of the JComponent... there are many possibilities here and to get better help, you'll need to tell us a lot more detail of this class, your GUI and your problem.