JPanel with SpringLayout in JScrollPane does not work
I don't know what I have done wrong... I will try and leave the irrelevant parts of my code outside.
What I am trying to do, it do build a repository of components. Something like JavaBeans: the repository view contains a visual representation of all components.
My repository is the UniversalView. My code follows the MVC pattern. Actual components are stored in an XML file. My UniversalView has a method modelPropertyChange(PropertyChangeEvent), that when a change happens to the model (the XML file containing all my components) this method gets called.
publicclass UniversalViewextends JFrame{
//Main panel of the gui application
private JScrollPane scroller;
private JPanel jPanel;
private SpringLayout springLayout;
public UniversalView(){
super("Component Repository");
initGUI();
this.setContentPane(scroller);
this.pack();
this.setSize(800, 600);
this.setVisible(true);
//Up to here, the window displays fine. Resizing the window places the
//scrollbars in the correct position. Apparently no component are displayed
//in the window
}
initGUI(){
jPanel =new JPanel();
//omitted a JMenu that is part of the jPanel
jPanel.setOpaque(true);
springLayout =new SpringLayout();
jPanel.setLayout(springLayout);
//The panel is inserted in a JSCrollPane
this.scroller =new JScrollPane(jPanel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroller.setOpaque(true);
}
//This method gets executed whenever a change happens to the
//XML file containing all components
publicvoid modelPropertyChange(PropertyChangeEvent arg0){
ModelChange m = ModelChange.valueOf(arg0.getPropertyName());
switch(m){
...
// loads all components from the XML file. This is where the problem
// is. After this code is executed, the scrollbar disappears, and two gray
// bars are displayed instead. Components are displayed OK but
// NOT the scrollbar.
case CURRENT_COMPONENT_LIST:
this.scroller.removeAll();
drawComponents((ArrayList<Element>) arg0.getNewValue());
this.scroller.add(this.jPanel);
this.repaint();
break;
case ....:
....
}//end of switch
}//end of ModelPropertyChange
// design all components into the JPanel
privatevoid drawComponents(ArrayList<Element> list){
//clear the panel from previous components
jPanel.removeAll();
int noOfComponents = list.size();
// this variable holds the size of the drawn area. It is calculated base on the number
// (and preffered size) of the inserted components. Actual calculations omitted.
Dimension d =new Dimension();
for(int i=0; i<noOfComponents; i++){
Element e = list.get(i);
//this is the class representing a view of a component. It is initialised
//with the XML element representing my component
ComponentView tempComponent =new ComponentView(e);
jPanel.add(tempComponent);
springLayout.putConstraint(SpringLayout.WEST, tempComponent,
hPosition,//this value is updated automatically based on the number of component
//inserted
SpringLayout.WEST, jPanel);
springLayout.putConstraint(SpringLayout.NORTH, tempComponent,
vPosition,//this value is updated automatically based on the number of component
//inserted
SpringLayout.NORTH, jPanel);
}
//Rebuilt the jPanel
this.jPanel.setPreferredSize(d);
this.jPanel.revalidate();
this.jPanel.repaint();
}
The code for ComponentView is:
/** Class for representing the view of a component
* as this will display in the component repository. */
publicclass ComponentViewextends JPanel{
publicvoid paintComponent(Graphics g){//properly overriden }
public Dimension getPreferredSize(){//properly overriden }
}
I hope this is clear. Please tell if otherwise.>

