stack buttons

Does anyone know an easy way to stack two buttons , one on top of other, without using TableLayout ?

It appears that TableLayout is pretty powerful but it is not standard java API.

Jay

If you mean stacking one-above-the-other, try GridLayout. If you mean stacking one-behind-the-other, try CardLayout. Both standard Java since at least 1.1.

I meant overlap and having an intersection but both are visible.

I do not think that I can use CardLayout in which only one component is visible at a time, similar to JTabbedPane.

Try putting them in a JPanel with a null layout manager and setting the positions yourself with setBounds. In fact you can make your own mini layout manager for that panel that will change the bounds on the buttons when the panel is moved and resized by the layout manager of its parent container.

Yeah, probably best to drop the layout manager entirely in that case.

If your “TableLayout” is the same one I found at Sun with a quick Google, it doesn’t allow you to overlap components either. Layout managers are generally used to stop that from happening! :wink:

I can get it to happen with GridBagLayout :slight_smile: – overlapping when it isn’t supposd to that is… Just stick a GridBagLayout on a panel in the bottom half of a splitpane and set the minimum size of the panel to 0 so you can slide the splitter bar to hide it completely… soon as the panel gets to small for the components and starts to “slide out” at the same time the GridBagLayout tries to make the contained components BIGGER… so while it is nicely sliding outof view the buttons and stufff on the top row are growing vertically… neat effect, but unwelcome in my case :frowning:

Sorry I went off-topic again.
;D

Thanks guys. Yes, it works by creating a JPanel with null option and then placing the buttons wihtin the jpanel. But I have one little issue. I could not make one button always remaining behind the other one, even I set the button behind to none focusable. When I cliked the button behind, it shows up in front :

import java.awt.;
import java.awt.event.
;
import javax.swing.JPanel;
import javax.swing.JButton;

public class ButtonStack
{
public static void main (String args[])
{

  final JButton jb1,jb2;

  Frame frame = new Frame("Example of  Overlap Buttons");
  frame.setBounds (100, 100, 300, 300);

  jb1=new JButton();
  jb1.setBackground(Color.red);
  jb1.setFocusable(false);

  jb2=new JButton();
  jb2.setBackground(Color.blue);
  JPanel jPanel=new JPanel(null);
  
  jb1.setBounds(120,120,100,100);
  jb2.setBounds(140,140,100,100);
  jPanel.add(jb2);
  jPanel.add(jb1);
  frame.add(jPanel,BorderLayout.CENTER);
  frame.addWindowListener
        (new WindowAdapter()
            {
                public void windowClosing (WindowEvent e)
                {
                    System.exit (0);
                }
            }
        );
  frame.show();
}

}

By the way, there is an exmaple about using TableLayout to create overlap buttons:

http://java.sun.com/products/jfc/tsc/articles/tablelayout/Simple.html

There is no z-order. whatever draw last shows up in front. When you click on the button that makes it draw.

Experiment with javax.swing.JLayeredPane… I think that is the “right” way to do what you are after. I should have mentioned this in the first place, but it slipped my mind, and I had just recently needed to use a null layout for something else I was working on.

Read the Java Tutorial at java.sun.com

Trail: Creating a GUI with JFC/Swing
Lesson: Using Swing Components

“How to Use Layered Panes”

It’s linked from the JavaDoc for JLayeredPane.