Eclipse Windowbuilder, Overlapping JPAnels

Eclipse WindowBuilder, overlapping JPanels

You might also want to look at OverlayLayout, seen here. It's not included in the conventional gallery, but it may be of interest.

Overlay Sample

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.OverlayLayout;

/** @see http://stackoverflow.com/a/13437388/230513 */
public class OverlaySample {

public static void main(String args[]) {
JFrame frame = new JFrame("Overlay Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new OverlayLayout(panel));
panel.add(create(1, "One", Color.gray.brighter()));
panel.add(create(2, "Two", Color.gray));
panel.add(create(3, "Three", Color.gray.darker()));
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

private static JLabel create(final int index, String name, Color color) {
JLabel label = new JLabel(name) {
private static final int N = 64;

@Override
public boolean isOpaque() {
return true;
}

@Override
public Dimension getPreferredSize() {
return new Dimension(index * N, index * N);
}

@Override
public Dimension getMaximumSize() {
return new Dimension(index * N, index * N);
}
};
label.setHorizontalAlignment(JLabel.RIGHT);
label.setVerticalAlignment(JLabel.BOTTOM);
label.setBackground(color);
label.setAlignmentX(0.0f);
label.setAlignmentY(0.0f);
return label;
}
}

Overlap JPanels with WindowBuilder for eclipse

I would like to have two Jpanels that perfectly overlap each other. I would then be able to toggle their visibilty based on the selection of a combox box

See: How to Use Card Layout for an example that does exactly this.

Overlapping two JButtons : Issue

What does layout manager have to do with it?

@Patricia Shanahan has pointed out the problem with overlapping the buttons. One solution is to let the layout do the work. In the example below, butonPanel has a centered FlowLayout by default. It adopts the preferred size of the buttons in any Look & Feel.

Addendum: Ah, you want the buttons to overlap but retain a stable z-order. Because rendering is controlled by the button's UI delegate, a subclass of ButtonUI, the effect is Look & Feel dependent. For example, both com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel and com.apple.laf.AquaLookAndFeelbehave as you want, while many others do not. Absent creating your own delegate, shown here, you might try to adapt the approach shown here, which hides the button's and uses an overlaid image. Depending on the enclosing container's layout, either FlowLayout or GridLayout(1, 0) should work.

As an aside, don't let the GUI designer dictate your GUI design!

image

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/** @see https://stackoverflow.com/a/14075990/230513 */
public class LayoutTest {

private void display() {
JFrame f = new JFrame("LayoutTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JPanel(){

@Override
public Dimension getPreferredSize() {
return new Dimension(300, 150); // content placeholder
}
}, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(); // default: FlowLayout, centered
buttonPanel.add(new JButton("Button1"));
buttonPanel.add(new JButton("Button2"));
f.add(buttonPanel, BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

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

@Override
public void run() {
new LayoutTest().display();
}
});
}
}

How to position two JPanels diagonal from each other in Java?

I would use the BoxLayout layout manager in combination with Box. But there are several other options. This is just my personal preference.

For more information, please see How to Use BoxLayout.

Resize JFrame to JPanels inside JLayeredPane

As suggested in How to Use Layered Panes: Laying Out Components in a Layered Pane, "Although a layered pane has no layout manager by default, you can still assign a layout manager to the layered pane." Use OverlayLayout, seen here, for overlapping panels.

Alternatively, use JInternalFrame, which does allow you to pack() the internal frames individually, as shown here and here.

How to put a component on top of others?

There's a JLayeredPane example here, but OverlayLayout, seen here, may be of interest. It's not included in the conventional gallery.

OverlayLayout

Also consider this example that paints on the scroll pane's JViewport.

viewport



Related Topics



Leave a reply



Submit