Using mouse Motion Listener on JButton? - java

hii
I have used Image as JButton for set in to panel
but now i want to use mouse motion listener actions on that image
for that purpose what can i do
following is the code for image
JButton buttonForPicture = new JButton();
buttonForPicture.setBorder(new EmptyBorder(0, 0, 0, 0));
buttonForPicture.setOpaque(false);
buttonForPicture.setIcon(new ImageIcon("/Users/hussainalisyed/Documents/Images/pic9.jpg"));
panel5.add(buttonForPicture,BorderLayout.CENTER);
is there any another way to do that
or
...

I'm not sure exactly what you're asking?
Your button is like any other JButton:
buttonForPicture.addMouseMotionListener(new MouseMotionListener() {
#Override
public void mouseMoved(MouseEvent e) {
}
#Override
public void mouseDragged(MouseEvent e) {
}
});
That catches movement events for the whole button, not just the image.

Read the JButton API there are methods to change the icon on a mouse rollover, if thats what you are trying to do. Search the API for methods containing "icon" to see what your options are.
If you just want to know how to write a MouseMotionListener, then read the section from the Swing tutorail on How to Write a Mouse Motion Listener for a working example.

Related

How coud I create these kind of Button using Java Swing?

I want to create button without any borders or shadow, but an icon instead using java swing component. How can I accomplish this?
Real Button
JButton btnNewButton = new JButton("");
btnNewButton.setContentAreaFilled(false);
btnNewButton.setBorderPainted(false);
btnNewButton.setBorder(null);
btnNewButton.setIcon(new ImageIcon(path));
This will give you a real button without any borders around the given image to work with. Note that in this state the button doesn't have a "click animation" anymore. For such an animation you could use the .setSelectedIcon(selectedIcon);
Clickable Image
ImageIcon img = new ImageIcon(path);
JLabel button = new JLabel(img);
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
//Set pressed or something else
}
});
But this one just provide you a clickable Image and should only be used when a clickable Image without any other intentions is needed.
Note that this way is just a workaround.

Change ImageIcon in JLabel using Timer

I have two classes: logic and the JFrame. In frame I have a JLabel and a JButton, and I would like to:
When this button is clicked, the ImageIcon in the label changes after a determined time using a Swing Timer, like if it is flashing. To do it I loaded two images with different brightness (img1b and img1). I tried to make the timer change the image twice with different delays, but I was unsuccessful. I also put a listener in the button and implemented the actionPerformed as below:
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(btnImg1)) {
logic.piscaImagen(img1, lblImg1);
logic.piscaImagen(img1b, lblImg1);
In logic class:
public void piscaImagen(ImageIcon img, JLabel lbl) {
Timer timer = new Timer(1250, null);
timer.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt){
if(lbl.getIcon() != img){
lbl.setIcon(img);
}
}
});
timer.setRepeats(false);
timer.start();
}
But when I run it nothing changes in the logic.piscaImagen. Any tips?
logic.piscaImagen(img1, lblImg1);
logic.piscaImagen(img1b, lblImg1);
It looks to me like you are starting two Timers. So the first Timer fires and it changes the image, then the second timer fires and it restores the image so basically you only see the first image.
All you need is one Timer. Each time the Timer fires you change the image. So the basic code in your Timer would be:
if (lbl.getIcon() == img1)
lbl.setIcon(img1b);
else
lbl.setIcon(img1);
Or for a more flexible solution you can use the Animated Icon.
The Animated Icon will allow you to specify a List of Icons to display. Then when the Timer fires the next Icon in the List is displayed. You can set the Animated Icon for continuous display or you can control the number of cycles.
EDIT: answer inaccurate, repaint() not necessary - see comments.
You're missing the repaint() call that tells the program it needs to update the display.
#Override
public void actionPerformed(ActionEvent evt) {
if(lbl.getIcon() != img){
lbl.setIcon(img);
lbl.repaint();
}
}
(your if statement was also missing a closing brace, unsure what impact that would have / if it was a typo)

Mouse over events with JButton

I'm trying to create a custom mouse over event on a JButton. The reason being that my JButton is currently an image, so I had to remove all the borders and animations and what not. so I did this:
btnSinglePlayer.setOpaque(false);
btnSinglePlayer.setContentAreaFilled(false);
btnSinglePlayer.setBorderPainted(false);
And that works perfect to only display the image, and the button does in fact work. I want to know if there's any pre-built methods perhaps that can do this, or how I would go about learning to do what I want.
More specifically, what I want the image to do when I mouse over is for it to get just a bit bigger.
I have tried these so far, and did nothing:
btnSinglePlayer.setRolloverIcon(singlePlayerButton);
btnSinglePlayer.setPressedIcon(singlePlayerButton);
for Icon to use implemented methods in API
you can to use ButtonModel with ChangeListener
(by default) for JButtons JComponents there no reason to use Mouse(Xxx)Listener or its MouseEvent, all those events are implemented and correctly
As an alternative You can achieve this by registering MouseListener to the JButton and override mouseEntered() ,mouseExited() , mousePressed() and mouseReleased() method.For Example:
final ImageIcon icon1 = new ImageIcon("tray.gif");
final JButton button = new JButton(icon1);
final int width = icon1.getIconWidth();
final int height = icon1.getIconHeight();
button.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent evt)
{
icon1.setImage((icon1.getImage().getScaledInstance(width + 10, height,Image.SCALE_SMOOTH)));
//button.setIcon(icon1);
}
public void mouseExited(MouseEvent evt)
{
icon1.setImage((icon1.getImage().getScaledInstance(width , height,Image.SCALE_SMOOTH)));
}
public void mousePressed(MouseEvent evt)
{
icon1.setImage((icon1.getImage().getScaledInstance(width + 5, height,Image.SCALE_SMOOTH)));
}
public void mouseReleased(MouseEvent evt)
{
icon1.setImage((icon1.getImage().getScaledInstance(width + 10, height,Image.SCALE_SMOOTH)));
}
});
button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);

image does not open when button is pressed in actionListener in my GUI?

I am trying to get an image to open when the PlaySci button is pressed so I put the image in the PlaySci action listener, however it only opens when the exit button is pressed?
I have looked at it for hours and still dont understand why, I have tried to get rid of the exit button alltogether but then the image does not show at all.
I made the image into a JLabel at the top:
ImageIcon scis1 = new ImageIcon("scis.jpg");
private JLabel picture = new JLabel(scis1);
Here is the code for my PlaySci button ActonListener:
class PlaySciHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
String computerRand = sps.computerChoice();
txtComputerRand.setText(computerRand);
String result = sps.play(Sps.SCISSORS);
txtResult.setText(result);
picture.setBounds(60, 200, 400, 400);// this is the image I want displayed when the PlaySci button is pressed
panel.add(picture);
}
}
This is the exit button ActionListener (That for some reason is the only way to display the image):
class exitHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
int n = JOptionPane.showConfirmDialog(frame, //when this button is pressed the image comes up?
"Are you sure you want to exit?",
"Exit?",
JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION){
System.exit(0);
}
}
}
This is the code creating the button and adding the ActionListener:
btnPlaySci = new JButton ("Scissors!");
btnPlaySci.setBounds(180, 40, 110, 20);
btnPlaySci.addActionListener(new PlaySciHandler());
panel.add (btnPlaySci);
btnPlaySci.setForeground(Color.MAGENTA);
Any help would be appreciated.
You should repaint your panel after you add picture to it. See the code for PlaySciHandler actionPerformed method.
class PlaySciHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
String computerRand = sps.computerChoice();
txtComputerRand.setText(computerRand);
String result = sps.play(Sps.SCISSORS);
txtResult.setText(result);
picture.setBounds(60, 200, 400, 400);// this is the image I want displayed when the PlaySci button is pressed
panel.add(picture);
panel.repaint();//Must repaint the panel .
}
}
Note: As a side note I would suggest you to never use null Layout for JPanel.Use the inbuilt Layouts provided by Swing. You can get more information about Layouts usage at this official site. Another one is that always stick with java naming conventions. Class exitHandler should be written as ExitHandler instead.To know more have a look at this official site.
Don't add the JLabel in the class PlaySciHandler implements ActionListener block.Add it in your createForm() method and make it invisible: picture.setVisible(false);
and when you want to display after a button click, make it visible : picture.setVisible(true);

Drawing border around JLabel when selected, like the Buttons

I was trying to paint border around JLabel when it is clicked. Just like JButtons are painted.
I thought it would be easy but I failed to do the job.
I tried to figure out what happens to JButtons when clicked by putting breakpoints in source codes. But I got lost, however, I have a feeling that javax.swing.plaf and its subpackages are what I need.
Am I right? Is there a simpler way to do the job.
Here is an example:
You could add MouseListener to your label and setup a border in mousePressed/mouseReleased methods. Here is a simplified example:
label.addMouseListener(new MouseAdapter(){
#Override
public void mousePressed(MouseEvent arg0) {
label.setBorder(BorderFactory.createLineBorder(Color.black));
}
#Override
public void mouseReleased(MouseEvent arg0) {
label.setBorder(null);
}
});
Also, as an alternative you can make a button with a flat style that will look like a label. This answer can be useful.

Categories