Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bug in SpringLayout ?

 
Mariusz Malinowski
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Once again I post the topic. This time with shortened code.

Whenever I press a button (see my code below) the execution time of my app increases (if run my app, please press the button at least 5 times). Has anyone encountered such a problem when using SpringLayout?

If I change layout the app works fine.
I would grateful for any suggestions.

Mariusz

package problem;
import java.awt.Container;
import java.awt.event.*;
import javax.swing.*;

public class SpringProblem implements ActionListener {

JButton button;
JPanel panel;
int rows;

private SpringProblem(){
JFrame frame = new JFrame();
Container c = frame.getContentPane();
c.add(new JScrollPane(labelPanel(40)), "Center");
c.add(button = new JButton("Add"), "South");
button.addActionListener(this);
frame.pack();
frame.setVisible(true);
}

private JPanel labelPanel(int labelCounter){
panel = new JPanel(new SpringLayout());
for (int i = 0; i < labelCounter; i++) panel.add(new JLabel("OLD"));
// 10 refers to the number of columns
rows = labelCounter/10;
//http://java.sun.com/docs/books/tutorial/uiswing/layout/example1dot4/SpringUtilities.java
SpringUtilities.makeCompactGrid(panel, rows, 10, 10, 10, 5, 5);
return panel;
}

public void actionPerformed(ActionEvent arg0) {
rows++;
SwingUtilities.invokeLater(new Runnable(){
public void run() {
for (int i = 0; i < 10; i++) panel.add(new JLabel("NEW"));
SpringUtilities.makeCompactGrid(panel, rows, 10, 10, 10, 5, 5);
panel.revalidate();
}});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new SpringProblem();
}});
}
}
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you press the button 5 times, there will be 90 labels. Why does it surprise you that laying out a container takes longer the more components are in it? Especially given that SpringLayout works by defining relationships between components, even quadratic performance in the number of components wouldn't be a big surprise.

Another layout manager like, say, GridLayout would handle this very differently: it forces all the components to be the same size, which it computes without asking any of them its preferred dimensions -- so it's obviously going to be much faster!
 
Mariusz Malinowski
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all thank you for your answer.

I checked what it would be if I change initial number of displayed labels:

c.add(new JScrollPane(labelPanel(140)), "Center");

In the result it turned out that panel with 150 labels (initial value 140) appears faster than panel with 90 labels (initial value 40).

That's the thing which makes me wonder.

Regards

Mariusz
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic