1

I'm encountering this issue in Java Swing:

Image of the problem

Both Magenta and Green components are JButtons. I'm using absolute layout for this. When hovering to Green, it overlaps Magenta even if no layout manager is applied nor JLayeredPane used.

Any reason for this behavior? How can I make sure the Magenta stays on top when hovering to Green?

Edit 2: Just to be clear with my goal, the idea for this is to make a UI similar to Android Notification Bar with Assistive Touch. Assume that the Notification Bar is a layer and the Assistive Touch is the topmost layer. The problem with using a transparent layer in JLayeredPane is that if a layer/panel occupies the whole frame even when set to transparent, the layers underneath it are not drawn.

edmandie
  • 181
  • 1
  • 1
  • 6
  • There's a method `setComponentZOrder`, but I'm not sure it will help in this case. Wouldn't it be better to position the buttons properly so that they don't overlap? – NeplatnyUdaj Feb 28 '15 at 21:47
  • To be clear with my goal, I'm aiming to create 3 JPanels that overlaps each other without using JLayeredPane. I noticed that when hovering to a button, it discards the initial Z-order. I'll check your suggested solution. Thanks, anyway. – edmandie Feb 28 '15 at 21:51
  • Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Mar 01 '15 at 00:05

1 Answers1

5

The answer is very simple: don't use absolute layout, use a real LayoutManager. In this case, it seems that BorderLayout will do the job just fine.

Demo

See this example:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class TestLayout {

    protected void initUI() {
        JFrame frame = new JFrame("test");
        Container cp = frame.getContentPane();
        cp.setLayout(new BorderLayout());
        cp.add(createColoredButton(Color.BLACK, Color.MAGENTA, "Hello World 1"), BorderLayout.NORTH);
        cp.add(createColoredButton(Color.BLACK, Color.GREEN, "Hello World 2"), BorderLayout.EAST);
        cp.add(createColoredButton(Color.WHITE, Color.BLUE, "Hello World 3"), BorderLayout.CENTER);
        // frame.pack();
        frame.setSize(600, 600);
        frame.setVisible(true);
    }

    private JButton createColoredButton(Color fgColor, Color bgColor, final String text) {
        final JButton button = new JButton(text);
        button.setBorderPainted(false);
        button.setFocusPainted(false);
        button.setForeground(fgColor);
        button.setBackground(bgColor);
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(button, "You just clicked: " + text);
            }
        });
        return button;
    }

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

            @Override
            public void run() {
                new TestLayout().initUI();
            }
        });
    }
}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117