//Set up the content pane, where the "main GUI" lives.
Container contentPane = frame.getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(changeButton);
contentPane.add(new JButton("Button 1"));
contentPane.add(new JButton("Button 2"));
//Set up the menu bar, which appears above the content pane.
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Menu");
menu.add(new JMenuItem("Do nothing"));
menuBar.add(menu);
frame.setJMenuBar(menuBar);
//Set up the glass pane, which appears over both menu bar
//and content pane.
myGlassPane = new MyGlassPane(changeButton, menuBar,
frame.getContentPane());
frame.setGlassPane(myGlassPane);
frame.pack();
frame.setVisible(true);
}
}
/**
* We have to provide our own glass pane so that it can paint.
*/
class MyGlassPane extends JComponent {
Point point;
public MyGlassPane(AbstractButton aButton,
JMenuBar menuBar,
Container contentPane) {
CBListener listener = new CBListener(aButton, menuBar,
this, contentPane);
addMouseListener(listener);
addMouseMotionListener(listener);
}
}
/**
* Listen for all events that our check box is likely to be
* interested in. Redispatch them to the check box.
*/
class CBListener extends MouseInputAdapter {
Toolkit toolkit;
Component liveButton;
JMenuBar menuBar;
MyGlassPane glassPane;
Container contentPane;
boolean inDrag = false;
public void mouseMoved(MouseEvent e) {
redispatchMouseEvent(e, false);
}
/*
* We must forward at least the mouse drags that started
* with mouse presses over the check box. Otherwise,
* when the user presses the check box then drags off,
* the check box isn't disarmed -- it keeps its dark
* gray background or whatever its L&F uses to indicate
* that the button is currently being pressed.
*/
public void mouseDragged(MouseEvent e) {
redispatchMouseEvent(e, false);
}
public void mouseClicked(MouseEvent e) {
redispatchMouseEvent(e, false);
}
public void mouseEntered(MouseEvent e) {
redispatchMouseEvent(e, false);
}
public void mouseExited(MouseEvent e) {
redispatchMouseEvent(e, false);
}
public void mousePressed(MouseEvent e) {
redispatchMouseEvent(e, false);
}
//XXX: If the event is from a component in a popped-up menu,
//XXX: then the container should probably be the menu's
//XXX: JPopupMenu, and containerPoint should be adjusted
//XXX: accordingly.
component = SwingUtilities.getDeepestComponentAt(
container,
containerPoint.x,
containerPoint.y);
if (component == null) {
return;
}
if (component.equals(liveButton)) {
inButton = true;
testForDrag(eventID);
}
is possible to make the gray backgroud of the JFrame "frame" in your case transparent so you would actually see whats behind it, for example your desktop etc.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.