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);
}
}

