2

My Mouse Released is not working. In my code there are two JPanels(p1 and p2) in another JPanel(p). and there are two Buttons named RED and GREEN. My Code should work like when someone click the Buttons, The Panels should be changed Dynamically . But unfortunately When I am running my program, the Button "RED" and "GREEN" is not responding. Here I have Added my codes. Thank you.

package animat;

import java.awt.Color;
import java.awt.event.*;

import javax.swing.*;



public class Animat extends JFrame{

Animat(){


JFrame j=new JFrame();

j.setSize(400,400);
j.setVisible(true);
JPanel p=new JPanel();

p.setSize(300,400);
p.setLayout(null);
p.setBackground(Color.BLACK);
p.setVisible(true);
j.add(p);

JPanel p1=new JPanel();
p1.setBounds(0,50,400,350);
p1.setBackground(Color.red);
p1.setVisible(true);
p.add(p1);

JPanel p2=new JPanel();
p2.setBounds(0,50,400,350);
p2.setBackground(Color.GREEN);
p2.setVisible(true);
p.add(p2);

JButton b=new JButton("RED");
b.setBounds(0,0,100,50);
b.setVisible(true);
p.add(b);

JButton b1=new JButton("GREEN");
b1.setBounds(100,0,100,50);
b1.setVisible(true);
p.add(b1);

b.addMouseListener(new MouseAdapter(){
public void MouseReleased(MouseEvent e){
p.removeAll();
p.repaint();
p.revalidate();
p.add(p1);
}
});

b1.addMouseListener(new MouseAdapter(){
public void MouseReleased(MouseEvent e){
p.removeAll();
p.repaint();
p.revalidate();
p.add(p2);
}
});
}
public static void main(String[] args) {
   new Animat(); 
   }

}

Shishir
  • 187
  • 1
  • 8
  • 2
    Instade of using a Mouselistener to a Button. You might wanna use a actionListener. b.addActionListener(new ActionListener(){}); – R. Suntjens Oct 27 '15 at 10:33
  • 1
    @Arc676 thanks for your suggestion. In case of MouseListener, all the action listeners like Mouse pressed, mouse Clicked, mouse Released etc are needed to be added, while Mouse Adapter helps me to pick just the action I need . – Shishir Oct 27 '15 at 10:39
  • Try `ActionListener` as suggested by the other commenter. That's what I meant. Sorry (I was thinking of detecting clicks on a canvas). – Arc676 Oct 27 '15 at 10:42
  • 1
    @R.Suntjens, ActionListener is used for clicking button only, that listener does not help to apply "Mouse Released" . I Wanted the buttons should work when I am releasing mouse After clicking the button. Thank you. – Shishir Oct 27 '15 at 10:42

1 Answers1

0

You are - by accident - not overriding the function you meant to.

With this code:

b1.addMouseListener(new MouseAdapter(){
   public void MouseReleased(MouseEvent e){
      p.removeAll();
      p.repaint();
      p.revalidate();
      p.add(p2);
   }
});

You are adding a new function, instead of overriding the mouseReleased function. Hence it is never called. Just change the function-name to start with lower-case and it will work.

Note #1: You can verify my statement below by adding the @Override annotation to your function. You will be told by your IDE that you are in fact not overriding anything (due to the typo) Note #2: You should be probably using mousePressed instead. It is more logical in this case (but your code works as well)

Gergely Bacso
  • 14,243
  • 2
  • 44
  • 64
  • 1
    Thank you so much. your suggestion worked ! Thank you again :) – Shishir Oct 27 '15 at 10:54
  • "mouseReleased" instead of "MouseListener" did the trick. Thank you for responding quickly. – Shishir Oct 27 '15 at 10:56
  • 1
    Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Oct 27 '15 at 11:17
  • @Gergely Bacso 1. p.revalidate(); and then p.repaint(); whould be last code lines, 2. it should be mouseReleased not MouseReleased – mKorbel Oct 27 '15 at 11:56
  • Would you mind reading the whole answer? I was copying his code where he made the mistake and then describing the problem you just pointed out. – Gergely Bacso Oct 27 '15 at 12:09