Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes deadlocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "deadlocks" Watch "deadlocks" New topic
Author

deadlocks

Mariusz Malinowski
Greenhorn

Joined: Dec 15, 2004
Posts: 14
Hi!

I have got problem with deadlocks. In my application I want to change the contents of JPanel in dynamic way.

The application consists of two JFrames. The first frame contains JPanel with 40 JLabel components. After pressing a button attached to the first frame, the second JFrame appears. The second frame allows to add another 10 JLabel components to the first frame. After performing the 5th attempt of adding another 10 JLabel components there are big deadlocks.

Does any one knows how to avoid it?

I would be grateful for any suggestions.

Mariusz
///////////////////////////////////////////////////////
package frames;

import java.awt.*;
import java.awt.event.*;
import java.util.Vector;

import javax.swing.*;

/**
* A frame which contains JLabel components.
*/
public class FrameOne implements ActionListener {

JPanel componentPanel;

public FrameOne() {

JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

// one can change the value of the argument
panel.add(new JScrollPane(componentPanel(40)));

JButton button = new JButton("Add row of components");
button.addActionListener(this);
panel.add(button);

Container c = frame.getContentPane();
c.add(panel);
frame.pack();
frame.setVisible(true);
}

/**
* JPanel with JLabel componets
*
* @param x number of JLabel componets
* @return
*/
private JPanel componentPanel(int x) {
componentPanel = new JPanel();
componentPanel.setLayout(new SpringLayout());

for (int i = 0; i < x; i++) {
JLabel label = new JLabel("old");
componentPanel.add(label);
}

// 10 refers to the componentPanel columns number
int rowsCount = componentPanel.getComponentCount() / 10;

/**
* A 1.4 file that provides utility methods for creating form- or
* grid-style layouts with SpringLayout. These utilities are used by
* several programs, such as SpringBox and SpringCompactGrid.
*http://java.sun.com/docs/books/tutorial/uiswing/layout/example1dot4/SpringUtilities.java
*/

SpringUtilities.makeCompactGrid(componentPanel, rowsCount, 10, 10, 10,5, 5);
return componentPanel;
}

public void actionPerformed(ActionEvent arg0) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new FameTwo();
}
});
}

/**
* Adds a row of componets to the first frame.
*/
private class FameTwo implements ActionListener {

JComboBox combo;

JFrame frameTwo;

FameTwo() {
frameTwo = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(innerPanel());

JButton button = new JButton("Add");
button.addActionListener(this);
panel.add(button);

Container c = frameTwo.getContentPane();
c.add(panel);
frameTwo.pack();
frameTwo.setVisible(true);

}

// Inner panel of the second JFrame
private JPanel innerPanel() {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));

JLabel label = new JLabel("Select a row number.");
panel.add(label);

// 10 refers to the componentPanel columns number
int rowsCount = componentPanel.getComponentCount() / 10;

// stores row indexes for JComboBox
Vector vector = new Vector();

for (int i = 1; i <= rowsCount + 1; i++)
vector.addElement(Integer.toString(i));

// contains indexes which point out in which row the new JLabel components
// should be added
combo = new JComboBox(vector);
panel.add(combo);

return panel;

}

public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
int rowIndex = Integer.parseInt((String) combo.getSelectedItem()) - 1;
frameTwo.dispose();

for (int i = 0; i < 10; i++) {
JLabel label = new JLabel("new");
componentPanel.add(label, rowIndex * 10 + i);
}
int rowsCount = componentPanel.getComponentCount() / 10;
SpringUtilities.makeCompactGrid(componentPanel, rowsCount,10, 10,10, 5, 5);
}
});
componentPanel.revalidate();
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new FrameOne();
}
});
}
}

[ April 09, 2005: Message edited by: Mariusz Malinowski ]
[ April 09, 2005: Message edited by: Mariusz Malinowski ]
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
Hi Mariusz,

I would not be looking for deadlocks, but some step in your program whose execution time increases exponentially.

Eddie


The nice thing about Standards is that there are so many to choose from!
Mariusz Malinowski
Greenhorn

Joined: Dec 15, 2004
Posts: 14
Hi Eddie!

Thank you Eddie for your reply. I will take your hint into consideration.

Regards,

Mariusz
Mariusz Malinowski
Greenhorn

Joined: Dec 15, 2004
Posts: 14
Hi!

Finally I have found it. I have done "thread dump" and I realized that SpringLayout (or SpringUtilities.class) makes my app slower. Thus I have chosen GridLayout instead of SpringLayout and it works fine.

But for your help Eddie, I wouldn't have found the solution so quickly.
Once again thank you.

Best regards,

Mariusz
////////////////////

package frames;

import java.awt.*;
import java.awt.event.*;
import java.util.Vector;

import javax.swing.*;

/**
* A frame which contains JLabel components.
*/
public class FrameOne implements ActionListener {

JPanel componentPanel;

public FrameOne() {

JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

// one can change the value of the argument
panel.add(new JScrollPane(componentPanel(40)));

JButton button = new JButton("Add row of components");
button.addActionListener(this);
panel.add(button);

Container c = frame.getContentPane();
c.add(panel);
frame.pack();
frame.setVisible(true);
}

/**
* JPanel with JLabel componets
*
* @param x
* number of JLabel componets
* @return
*/
private JPanel componentPanel(int x) {
componentPanel = new JPanel();
//!!!
componentPanel.setLayout(new GridLayout(0, 10));

for (int i = 0; i < x; i++) {
JLabel label = new JLabel("old");
componentPanel.add(label);
}
/*
//10 refers to the componentPanel columns number
int rowsCount = componentPanel.getComponentCount() / 10;
*/
return componentPanel;
}

public void actionPerformed(ActionEvent arg0) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new FameTwo();
}
});
}

/**
* Adds a row of componets to the first frame.
*/
private class FameTwo implements ActionListener {

JComboBox combo;

JFrame frameTwo;

FameTwo() {
frameTwo = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(innerPanel());

JButton button = new JButton("Add");
button.addActionListener(this);
panel.add(button);

Container c = frameTwo.getContentPane();
c.add(panel);
frameTwo.pack();
frameTwo.setVisible(true);

}

private JPanel innerPanel() {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));

JLabel label = new JLabel("Select a row number.");
panel.add(label);

// 10 refers to the componentPanel columns number
int rowsCount = componentPanel.getComponentCount() / 10;

// stores row indexes for JComboBox
Vector vector = new Vector();

for (int i = 1; i <= rowsCount + 1; i++)
vector.addElement(Integer.toString(i));

// contains indexes which point out in which row the new components
// should be added
combo = new JComboBox(vector);
panel.add(combo);

return panel;

}

public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
int rowIndex = Integer.parseInt((String) combo.getSelectedItem()) - 1;
frameTwo.dispose();
for (int i = 0; i < 10; i++) {
JLabel label = new JLabel("new");
componentPanel.add(label, rowIndex * 10 + i);
}

}
});
componentPanel.revalidate();
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new FrameOne();
}
});
}
}
 
GeeCON Prague 2014
 
subject: deadlocks