Problem with size of Panel
843807Sep 28 2007 — edited Sep 28 2007I have own class extends JPANEL , I added the JComponent to the panel and need to get size. I've been trying with getSize().widith and getSize().height but both return 0.
I am getting this message "addJComponent panel isn't sized". I have no idea why? Anyone can help? I posted my code in the below
Thanks
Karthik
public abstract class PrintablePage extends PrintablePanel {
JComponent theItem = null;
java.awt.Insets theInsets = null;
theInsets = new java.awt.Insets(0, 90, 0, 100);
Runnable doLayoutRunner = new Runnable() {
public void run() {
// add the JComponent to the panel
java.awt.GridBagConstraints gbconst = new java.awt.GridBagConstraints();
gbconst.gridx = 0;
gbconst.gridy = -1;
gbconst.fill = java.awt.GridBagConstraints.HORIZONTAL;
gbconst.anchor = java.awt.GridBagConstraints.CENTER;
gbconst.weightx = 1.0;
gbconst.insets = theInsets;
getJPanelUnits().add(theItem, gbconst);
getJPanelUnits().revalidate();
getJPanelUnits().repaint();
// get the height of the panel
double panelHeight = 0.0d;
double preferredHeight = 0.0d;
while (panelHeight < 1.0d) {
preferredHeight = getJPanelUnits().getPreferredSize().getHeight();
panelHeight = getJPanelUnits().getSize().getHeight();
if (panelHeight < 1.0d) {
System.out.println("addJComponent panelHeight : "+panelHeight+"\n");
System.out.println("addJComponent panel isn't sized");
}
}
}
};
SwingUtilities.invokeAndWait(doLayoutRunner);
public abstract JPanel getJPanelUnits();
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------
public class PrintablePanel extends JPanel implements Printable {
Image offscrImage = null;
private int pageNumber = 0;
private int numPages = 0;
/**
* PrintablePage constructor comment.
*/
public PrintablePanel() {
super();
initialize();
}
protected void debug (String txt)
{
DB2Model.debug (txt);
}
/**
* Insert the method's description here.
* Creation date: (2/1/2001 3:45:57 PM)
* @return int
*/
public int getPageNumber() {
return pageNumber;
}
/**
* Called whenever the part throws an exception.
* @param exception java.lang.Throwable
*/
private void handleException(java.lang.Throwable exception) {
/* Uncomment the following lines to print uncaught exceptions to stdout */
// System.out.println("--------- UNCAUGHT EXCEPTION ---------");
// exception.printStackTrace(System.out);
}
/**
* Initialize the class.
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private void initialize() {
try {
// user code begin {1}
// user code end
setName("PrintablePage");
setLayout(new java.awt.GridBagLayout());
setBackground(java.awt.Color.white);
setMaximumSize(new java.awt.Dimension(575, 756));
setPreferredSize(new java.awt.Dimension(575, 756));
setSize(575, 755);
setMinimumSize(new java.awt.Dimension(575, 756));
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
// user code begin {2}
// user code end
}
/**
* main entrypoint - starts the part when it is run as an application
* @param args java.lang.String[]
*/
public static void main(java.lang.String[] args) {
try {
JFrame frame = new javax.swing.JFrame();
PrintablePanel aPrintablePanel;
aPrintablePanel = new PrintablePanel();
frame.setContentPane(aPrintablePanel);
frame.setSize(aPrintablePanel.getSize());
frame.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
};
});
frame.show();
java.awt.Insets insets = frame.getInsets();
frame.setSize(frame.getWidth() + insets.left + insets.right, frame.getHeight() + insets.top + insets.bottom);
frame.setVisible(true);
} catch (Throwable exception) {
System.err.println("Exception occurred in main() of javax.swing.JPanel");
exception.printStackTrace(System.out);
}
}
public void performSubstitutions (String[] subts)
{
}
public int print (Graphics g, PageFormat format, int page)
{
return (printOnScr (g, format, page));
}
public int printOnScr (Graphics g, PageFormat format, int page)
{
page++;
pageNumber = page;
Graphics2D g2 = (Graphics2D) g;
g2.translate(format.getImageableX(), format.getImageableY());
synchronized (this)
{
try {
print (g2);
} catch (Exception exc)
{
exc.printStackTrace();
}
}
return Printable.PAGE_EXISTS;
}
/**
* Insert the method's description here.
* Creation date: (2/1/2001 3:45:57 PM)
* @param newPageNumber int
*/
public void setPageNumber(int newPageNumber, int newNumPages)
{
pageNumber = newPageNumber;
numPages = newNumPages;
}
}
Thanks
Karthik