zaa
zaa

Reputation: 79

MouseEntered and MouseExited not working

I am having trouble with mouseExited and mouseEntered events. They are not executed. And I know it is because of the setLayout. If I comment it, the problem dissapears.This is my code :

 public class Test{

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        JFrame frame = new JFrame();
        frame.setContentPane(new Pane());
        frame.getContentPane().setBackground(Color.GRAY);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setUndecorated(true); 
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public class Pane extends JPanel{
        public Pane(){
            ImageIcon start = new ImageIcon("Start.png");
            JLabel bStart = new JLabel(start);
            ImageIcon exit = new ImageIcon("Exit.png");
            JLabel bExit = new JLabel(exit);
            setLayout(new BorderLayout());

            add(bStart, BorderLayout.CENTER);
            bStart.addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e){
                    ImageIcon image = new ImageIcon("Start-Pressed.png");
                    bStart.setIcon(image);
                    Container parent = bStart.getParent();
                    parent.remove(bStart);
                    add(bExit);
                    parent.revalidate();
                    parent.repaint();
                }

                public void mouseEntered(MouseEvent arg0) {
                    ImageIcon image = new ImageIcon("Start-Hover.png");
                    bStart.setIcon(image);
                }


                public void mouseExited(MouseEvent arg0) {
                    ImageIcon image = new ImageIcon("Start.png");
                    bStart.setIcon(image);
                }
            });
            bExit.addMouseListener(new MouseAdapter() { 
                  public void mousePressed(MouseEvent e) { 
                      System.exit(0); 
                    } 

                  public void mouseEntered(MouseEvent e){
                      ImageIcon image = new ImageIcon("Exit-Hover.png");
                      bExit.setIcon(image);

                  }
                  public void mouseExited(MouseEvent e){
                      ImageIcon image = new ImageIcon("Exit.png");
                      bExit.setIcon(image);
                  }
             }); 
        }    
    } 
}

Upvotes: 1

Views: 1631

Answers (1)

camickr
camickr

Reputation: 324187

And i know it is because of the setLayout. If i comment it, the problem dissapears.

I doubt the layout is the problem. If the component appears, then the layout manager has nothing to do with how MouseEvents are generated.

From you code is looks like you are trying to provide rollover effects. Instead of using a JLabel for this you can use a JButton and set Icons for the different rollover effects:

button.setBorderPainted( false );
button.setRolloverEnabled( true );
button.setRolloverIcon( ... );
button.setRolloverSelectedIcon(...);
button.setSelectedIcon(...);

Then there is no need to manage the MouseListener as the UI will do it for you.

To handle a mousePressed you should then be using an ActionListener on the JButton. Read the section from the Swing tutorial on How to Write an ActionListener for more information.

Upvotes: 2

Related Questions