Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Overlapping panels: Button problem

800346Oct 17 2005 — edited Oct 19 2005
I have a mainPanel with null Layout and two overlapping SubPanels.
Each SubPanel has a Button. When I move the mouse pointer over
the SubPanels the Button of the hidden Panel is painted over
the visible Panel. Why ?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class DND_Demo_1 {
    public static void main(final String[] args) {
        final JPanel mainPanel = new JPanel();
        mainPanel.setLayout(null);
        
        SubPanel sub1 = new SubPanel("Panel 1");
        mainPanel.add(sub1);
        sub1.setSize(sub1.getPreferredSize());
        sub1.setLocation(100,100);
        
        SubPanel sub2 = new SubPanel("Panel 2");
        mainPanel.add(sub2);
        sub2.setSize(sub2.getPreferredSize());
        sub2.setLocation(85,85);
        
        final JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(mainPanel);
        f.setSize(400, 300);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}
class SubPanel extends JPanel {
    public SubPanel(final String name) {
        setPreferredSize(new Dimension(100, 100));
        setBorder(new TitledBorder(new LineBorder(Color.BLACK), name));
        JButton b1 = new JButton("b1 "+name);
        b1.setCursor(new Cursor(Cursor.HAND_CURSOR));
        add(b1);
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("b1 "+name);
            }
        });
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 16 2005
Added on Oct 17 2005
4 comments
519 views