In windows 8 when a user moves his mouse cursor to the right a control bar with some buttons is shown. So, can this be done in a java application? I want that when a user moves his mouse cursor to left a similar control bar pops-up with some buttons. Is something like that possible? Note: My java application fits the width and height of the screen(fullscreen application).
Asked
Active
Viewed 143 times
0
-
possible duplicate of [Get Mouse Position](http://stackoverflow.com/questions/1439022/get-mouse-position) – dotvav Aug 04 '15 at 08:59
-
1Check the current position of the mouse with a `MouseMotionListener`. If the cursor is inside the area you want, you can set a new pane (Glasspane, to make it easier) visible. On this glasspane you should add the buttons, or whatever. – Lukas Rotter Aug 04 '15 at 09:00
-
@dotvav alright so MouseInfo.getPointerInfo().getLocation() helped just half way the code but what about the bar that i wanted to display? should i be a jpanel or what? and what about the transition effect of the bar( the bar should show that it is moving out of the left corner)? – Nelson John Aug 04 '15 at 09:08
-
MouseMotionListener sends billions of events (every single pixel), when you use your programm. Try to avoid it. – Stefan Aug 04 '15 at 09:32
2 Answers
1
You can make use of a MouseMotionListener
on the contentPane
of the frame you are using. And within the MouseMotionListener
get the mouse position and compare with the value of the corner pixel.
You can do something like
getContentPane().addMouseMotionListener(new MouseMotionAdapter(){
@Override
public void mouseMoved(MouseEvent e) {
if(e.getX() == 0) //for left corner
charmsPanel.setVisible(true);
}
});

Vamshi Krishna Alladi
- 418
- 3
- 14
-
okay but the bar which i wanna setvisible should be what? a jpanel? – Nelson John Aug 04 '15 at 09:14
-
yeah a JPanel would be a good option so that you can internally organize the components and the view of the JPanel – Vamshi Krishna Alladi Aug 04 '15 at 09:20
-
Animation can only be achieved by writing it manually. we do not have default Java API for that I guess. – Vamshi Krishna Alladi Aug 04 '15 at 09:23
1
Here's an example based on @VamshiAlladi 's answer:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Example extends JFrame {
public Example() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int) screenSize.getWidth();
int height = (int) screenSize.getHeight();
JPanel glass = (JPanel) getGlassPane();
glass.setLayout(new BorderLayout());
JPanel bar = new JPanel();
bar.setLayout(new GridLayout(10, 1));
for (int i = 1; i <= 10; i++) {
bar.add(new JButton("Button " + i));
}
addMouseMotionListener(new MouseMotionListener() {
public void mouseMoved(MouseEvent e) {
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int x = (int) b.getX();
int y = (int) b.getY();
if (x > Example.this.getWidth() - 10) {
glass.setVisible(true);
} else {
glass.setVisible(false);
}
revalidate();
repaint();
}
public void mouseDragged(MouseEvent e) {
}
});
setSize(width, height);
glass.add(bar, BorderLayout.EAST);
setVisible(true);
}
public static void main(String[] args) {
new Example();
}
}
Edit:
Why should you use GlassPane
and not just a new JPanel
? - If you have components on your ContentPane
in the area where the bar will be shown, a (for example) glasspane "overlays" the contentpane. With a JPanel you would have to replace the components (which can cause problems).

Lukas Rotter
- 4,158
- 1
- 15
- 35