Skip to main content

JXLayer's layout for a wrapped component?

6 replies [Last post]
patb1
Offline
Joined: 2008-10-21
Points: 0

Hello,

I'm curios how JXLayer handles it's wrapped component in terms of layout. Am I right, that JXLayer sets the wrapped component as it's child, because JXLayout is a container component itself?

If this is right, what layout uses JXLayer?

Currently I have some trouble with invisible wrapped components, which size was not set (component.setSize(...) means visible in my case, without that line, the component is invisible).

Thanks a lot

Regards,
Ben

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
psychostud
Offline
Joined: 2005-11-02
Points: 0

I suppose the wrapped component is overlapped with the glasspane similar to the Overlaylayout , wherein the child components automatically take up the available space.

patb1
Offline
Joined: 2008-10-21
Points: 0

Ok, I double checked it, and the behavior is the same - when I don't set a size of the wrapped component (an ordinary JPanel in my case), it size is 0,0, even when the size of the JXLayer is e.g. 800,600 pixels. When I set a size for the wrapped JPanel (lets say 500,400 pixels), it is visisible and has exactly the same size 500,400 and does not grow with the JXLayer.

Before that, I believed that the wrapped component inside the JXLayer is always the size of the JXLayer itself?

Am I doing anything wrong here?

Thanks a lot for your help!

pietblok
Offline
Joined: 2003-07-17
Points: 0

Hi Ben,

Please run the following code:

[code]
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import org.jdesktop.jxlayer.JXLayer;

public class TestSize {

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

@Override
public void run() {
new TestSize().createGUI();
}
});
}

private void createGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
final JXLayer layer = new JXLayer(panel);
frame.add(layer);
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
System.out.println("JXLayer: " + layer.getSize());
System.out.println("JPanel: " + panel.getSize());
}
});
}

}
[/code]

For me, it prints the correct size of both the JXLayer and the JPanel.
[code]
JXLayer: java.awt.Dimension[width=392,height=266]
JPanel: java.awt.Dimension[width=392,height=266]
[/code]

The size is not necessarily set directly after setVisible. Therefore, one should invokeLater to request the resulting size.

Hope this helps

Piet

patb1
Offline
Joined: 2008-10-21
Points: 0

Hello

thanks a lot for your answer and the example code. Yes, I know that I can't rely on getSize directly after setting it. But I don't do it that way in my program.

My code is rather complex, and it is possible that the error lies not in JXLayer directy. What I do is wrapping a JPanel, and after that adding a lot of other JPanels to it (these are my views that I can switch via a menu) and setting them to invisible. Switching the views means setting all JPanels to invisible and the desired one to visible, very easy.

My use case is know, that in some cases, some of the JPanels must be disabled, and some enabled. I thought JXLayer is just the right thing - I just wrap the "father JPanel" (whereas all views lay in) and then can use JXLayer to catch all input events - so a view can be completely disabled.

Now, some of my JPanels (views) just don't show up, even when I set them to visible and do a repaint. The cause is, that their size is 0,0, although the "father JPanel" has a BorderLayout and I add the views with the CENTER contraint.

I noticed then, that all visible views (JPanels) have a size (setSize(...)) and all those, that can't be shown do not set their size.

I just don't get it, I have no clue where the problem is.

I would be happy if you have any other ideas.

pietblok
Offline
Joined: 2003-07-17
Points: 0

What about posting some runnable code that shows your problem?

Piet

patb1
Offline
Joined: 2008-10-21
Points: 0

Hello pietblok,

thanks a lot for your help and patience. I found a solution. The problem was the BorderLayout of my inner JPanel container. With that layout, the views were not the size of their parent container (I'm not sure why). Switching the layout manager to "TableLayout" did the trick, now the layout is working fine.

Thanks a lot