I am using an Icon with Java (Swing) JButton. Is it possible to change the icon when I take my mouse arrow over it?
I saw somewhere on Youtube that it is possible, but am unable to recall it.
You can take advantage of the JButton API which provides this kind of support.
Take a look at JButton#setRolloverIcon and JButton#setRolloverSelectedIcon
You will need to implement MouseListener like this:
public class YourClass extends JFrame implements MouseListener {
#Override
public void mouseEntered(MouseEvent e) { }
#Override
public void mouseExited(MouseEvent e) { }
#Override
public void mouseClicked(MouseEvent e) { }
#Override
public void mousePressed(MouseEvent e) { }
#Override
public void mouseReleased(MouseEvent e) { }
}
Add your function where needed.
You can override the mouseEntered() function by implementing a MouseListener and add the code to change the icon in that function.
If you're using an abstract button, you can just use setRolloverIcon() to set an image which will appear on rollOver.
Related
Kind of a noob question, but then again, I am a noob. I'm trying to implement a sort of "universal" mouse listener. That is, when I click any of the objects on screen, it runs a specific amount of code. I have the current solution below, but the code I want to run is the same for 10 different objects, so this gets rather tedious.
difference2 = new JLabel(new ImageIcon("transparent.png"));
difference2.setBounds(645,490,10,10); //left, top, width, height
contentPane.add(difference2);
difference2.setVisible(true);
difference2.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e) {
//code
}
});
I am aware I can create a separate method such as the following
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(null,"this would be nice");
}
But I can't figure out how to set up a mouse listener on every object for it. The JOptionPane currently does nothing.
I might have misread your question, but if you want the same mouselistener on various objects,you could store the instance of your listener in a variable once and then add it to whatever gui object you want it added to.
MouseListener ml = new MouseListener() {
#Override
public void mouseReleased(MouseEvent e) {//code}
#Override
public void mousePressed(MouseEvent e) {//code}
#Override
public void mouseExited(MouseEvent e) {//code}
#Override
public void mouseEntered(MouseEvent e) {//code}
#Override
public void mouseClicked(MouseEvent e) {//code}
};
JLabel j1 = new JLabel("Label1");
j1.addMouseListener(ml);
JLabel j2 = new JLabel("Label2");
j2.addMouseListener(ml);
You can create an instance of an anonymous class that extends MouseAdapter and assign it to a variable that you can reuse (myMouseListener in this case):
MouseListener myMouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(null,"this would be nice");
}
};
difference2.addMouseListener(myMouseListener);
aSecondObject.addMouseListener(myMouseListener);
aThirdObject.addMouseListener(myMouseListener);
...
Right now I am using a MouseListener to see if the mouse is pressed but it doesn't register when you press outside of an JFrame I would really need it to so how do I check for mouse events outside of a JFrame?
Right now I am using a MouseListener to see if the mouse is pressed
but it doesn't register when you press outside of an JFrame I would
really need it to so how do I check for mouse events outside of a
JFrame?
then JFrame lost Focus, you can test by using WindowFocusListener
Focus is asynchronous, then everything inside windowGainedFocus and windowLostFocus should be wrapped into invokeLater
Add a window listener
addWindowListener(new WindowListener() {
#Override
public void windowOpened(WindowEvent arg0) {
}
#Override
public void windowIconified(WindowEvent arg0) {
}
#Override
public void windowDeiconified(WindowEvent arg0) {
}
#Override
public void windowDeactivated(WindowEvent arg0) {
}
#Override
public void windowClosing(WindowEvent arg0) {
}
#Override
public void windowClosed(WindowEvent arg0) {
}
#Override
public void windowActivated(WindowEvent arg0) {
}
});
Try out all the methods (window...) and see which one works out best for you!
:)
I'm not telling you exactly what to do because to learn you cant just copy paste!
To know the status of mouse outside the window you can use:
Point point = MouseInfo.getPointerInfo().getLocation();
Unfortunatly java.awt.event.MouseMotionListener give you information about mouse movement inside your window.
I've extended a JList to provide two separate functionalities, toolTipText for items, and right-click options. Both work separately, but when I try to use them together, the MouseMoved events aren't being recognized? Below are the guts of my new listener methods. How should I be negotiating these various events?
public class JListTT extends javax.swing.JList {
public JListTT() {
super();
addMouseListener(new ttListener());
...
class ttListener extends MouseAdapter {
public void mouseMoved(MouseEvent e) {
String nodeID = bldItemNodeID();
theList.setToolTipText(nodeID);
}
public void mousePressed(MouseEvent ev) {check(ev); }
public void mouseReleased(MouseEvent ev) {check(ev); }
public void mouseClicked(MouseEvent ev) {check(ev); }
public void check(MouseEvent ev) {
if (ev.isPopupTrigger()) {
theList.setSelectedIndex(theList.locationToIndex(ev.getPoint()));
menu.show(theList, ev.getX(), ev.getY());
}
}
}
You add the ttListener object as a MouseListener, but I don't see you adding the ttListener object as a MouseMotionListener. For example:
ttListener myMouseadapter = new ttListener();
addMouseListener(myMouseadapter);
addMouseMotionListener(myMouseadapter);
I did not test this myself, but looking at the javadoc of JList the tooltip functionality is available out of the box. The javadoc of JList#getTooltipText clearly states
Overrides JComponent's getToolTipText method in order to allow the
renderer's tips to be used if it has text set.
So if your ListCellRenderer returns a Component in the getListCellRendererComponent method which has a tooltip it will be displayed by the JList without the need of a listener.
there's not necessarily a need for a low-level approach as a custom mouse-/motionListener:
as to a per-cell tooltip, see #Robin's answer
as to a context menu, JComonent has a property componentPopupMenu: using that will cope with opening the menu on keyboard short-cut automatically
"not necessarily" because you seem to rely on the cell being selected on right click. If so, you would still need a MouseListener to trigger the selection (after decade long debates, Swing doesn't - which seems to be unusual in current native apps ;-)
You can achieve it by using mouseDragged
YourClass extends JPanel implements MouseListener{
......
#Override
public void mouseDragged(MouseEvent e) {
//code go here
}
}
I am trying to change the background of a JButton after it is clicked. Currently my buttons are located in a GridLayout (3x3) and look like this:
tiles.add(new JButton(new AbstractAction("") {
#Override
public void actionPerformed(ActionEvent e) {
this.setIcon("foo.png");
}
}));
This does not work. How do I manipulate the background of the image from within the actionperformed?
A JToggleButton might be ideal for this, as shown on Swing JToolbarButton pressing
Note that you would need to add some code to ensure a button was only clickable once.
Alternately you might use a standard JButton and call AbstractButton.setDisabledIcon(Icon). Disable the button when clicked, and it will flip to the alternate icon.
You can create your own listener that implements the MouseListener. This way, you can control when the background of the button changes (when the mouse is released, pressed, etc). Here is an example
//Add the listener to the button
myButton.addMouseListener(new customActionListener());
//Create the listener
class customActionListener implements MouseListener {
public void mouseExited(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
Icon icon = new ImageIcon("icon.gif");
myButton.setIcon(icon);
}
public void mouseClicked(MouseEvent e) {
}
}
At whatever point you want to set the background back to its default, use:
myButton.setIcon(new ImageIcon());
This is the code I'm currently using:
#Override
public void mouseExited(MouseEvent e) {
System.out.println("detectado");
}
You can use addChartMouseListener() to add a ChartMouseListener to your ChartPanel. For example, in BarChartDemo1, add the following:
chartPanel.addChartMouseListener(new ChartMouseListener() {
public void chartMouseClicked(ChartMouseEvent e) {
System.out.println(e.getEntity());
}
public void chartMouseMoved(ChartMouseEvent e) {}
});
To listen for clicks, you must check the type of event.
In particular, you override the
public void mouseClicked(MouseEvent ev)
method, which is part of the interface for MouseListeners.
For a fill example see : this link