How to override the size of a JPanel in a componentResized method

Hi all

When the code below compiles,

mainly concerns the createpanel() method, the error is

"Cannot refer to a non-final variable Panel1 inside an inner class defined in a different method."

If I declared Panel1 final, then the override in componentResized(ComponentEvent e) does not work. I need to be able to override the size of Panel1 in the componentResized method everytime ScrollPane changes size.

I am new to Java Swing, would someone please help me out with this ?

What do i need to change to be able to override the size of Panel1.

publicfinalclass BuildingProcedureextends ProcdurePanel

{

privatestatic BuildingProcedure panel =null;

private BuildingProcedure()

{

super();

}

publicstatic ProcdurePanel getInstance()

{

if (panel ==null)

{

createPanel();

}

return panel;

}

privatestaticvoid createPanel()

{

panel =new BuildingProcedure();

JPanel Panel1 =new JPanel();

Panel1.setPreferredSize(new Dimension(800,800));

int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;

int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;

JScrollPane ScrollPane =new JScrollPane(Panel1,v,h);

for (int i = 0; i < 10; i++)

{

Panel1.add(new JLabel("This is just for testing"));

}

ScrollPane.addComponentListener(new ComponentListener()

{

publicvoid componentResized(ComponentEvent e)

{

Panel1.setSize(new Dimension(500,500));

}

publicvoid componentMoved(ComponentEvent e)

{

}

publicvoid componentHidden(ComponentEvent e)

{

}

publicvoid componentShown(ComponentEvent e)

{

}

});

panel.add(ScrollPane);

}

}

[3640 byte] By [va97a] at [2007-11-26 18:10:59]
# 1
1) first character of variable names are not upper cased:"ScrollPane" should be "scrollPane"2) You set the preferred size of the comonent, not the size. You may then need to revalidate() the panel to make sure the components are layed out correctly.
camickra at 2007-7-9 5:43:29 > top of Java-index,Desktop,Core GUI APIs...
# 2

camickr

I came across an old reply from you to a message titled:

"Re: setSize or setPreferredSize ? JComponent unresizable

Oct 20, 2005 9:12 AM (reply 3 of 5).

Now i understand the diff between setPreferredSize() and setSize().

Come to think of it, I want back to the code and I don't believe I need to implement the ComponentListener.

Panel1 is using the setPreferredSize() method and as the user shrinks or extands the frame size, the same behavior applies to the panel.

Thanks a lot

va97a at 2007-7-9 5:43:29 > top of Java-index,Desktop,Core GUI APIs...