How to remove GWT menubar on Screen click? - java

I am a beginner using GWT. I have a menubar which pops-up on a Label click. I need to remove it when the user clicks anywhere on screen except the Label which caused it to display (Legal) I tried various methods like hooking up this event on
RootPanel.get().addDomHandler(clickDetectHandler, ClickEvent.getType());
public void onClick(ClickEvent event) {
Object source = event.getSource();
if (!(source instanceof MenuBar))
panel.remove(menu);
I even tried using the MouseOutEvent but it doesn't detect click. I am able to remove it on a click back to the legal label. But I need it to be removed on detecting a click on the screen. Please advise.

GWT has a panel called PopupPanel which automatically handles exactly the behaviour that you want.
Quoting from the javadoc:
"PopupPanel's constructor takes 'auto-hide' as its boolean parameter.
If this is set, the panel closes itself automatically when the user clicks outside of it."
Is it possible to have the pop-up menu display inside of a PopupPanel?
http://google-web-toolkit.googlecode.com/svn/javadoc/2.5/com/google/gwt/user/client/ui/PopupPanel.html

Have a look at this GWT sample. This seems to have the behaviour you describe. It comes with source code.
Alternatively you can try handling the blur event on the menu widget.

Related

How to detect if user clicks on window title bar or any other part of window border in Java

I have implemented a pop-up numeric keypad using the Swing Popup class. I have a button associated with a JTextField that opens the numeric keypad when the user clicks on it, and then when/if the JTextField loses focus the Popup closes. It generally works well, except that occasionally I get artifacts that are "left over" from a Popup after it has been hidden. Sometimes the artifact is an image of the Components that were shown in the Popup, but more often it is a "black hole" of sorts that obscures anything else displayed in the same area of the screen in which the Popup had been, which can only be remedied by shutting down the application and the JVM.
The problem is difficult to reproduce, but it seems to manifest when the user manipulates the base Window while the Popup is open, such as by moving or resizing it. My thought is to simply hide the Popup when anything like that happens, which I can do with a combination of a WindowListener and a ComponentListener. However, I'd like to take it one step further and hide the Popup as soon as the user so much as clicks on the window title bar or another portion of its frame, even before they move, resize or iconify it. JComboBox popups in fact work this way. However, I have been unable to find any kind of mechanism by which I can be notified that a user has clicked on the window title bar. I've looked at the JComboBox and related code and can't figure out from that how it's working this magic, either. Is there any other kind of listener I could use to get this kind of notification?
I have implemented a pop-up numeric keypad using the Swing Popup class.
Well post your code demonstrating the implementation and problem when you post a question.
I don't know exactly what you are doing but you might be able to use a JPopupMenu. This will close when you click on the frame title bar with no FocusListener or any additional logic.
will be deleted, just code for testing,
private boolean _myWindowFocusLost = false;
.
_xxXxx.addFocusListener(new FocusAdapter() {
#Override
public void focusGained(FocusEvent e) {//Invoked when a component gains the keyboard focus.
if (e.getOppositeComponent() != null) {
if (e.getOppositeComponent() instanceof JComponent) {
JComponent opposite = (JComponent) e.getOppositeComponent();
if ((opposite.getTopLevelAncestor() != _myPopupWindow) && (!_myWindowFocusLost)) {
_myWindowFocusLost = false;
}
}
}
}
});

Is there a way to control menu popup position in JavaFX 8?

I am working on custom styling for my application. My current problem stems from the fact that designers specified entirely different styles for menu bar and menu items. I implemented the button style, but menu popup now slightly overlays the button itself (including button label), which is ugly.
Is there some way to control menu popup position relatively to menu bar button? Where is the code that actually positions those popups? I searched through JavaFX source, but javafx.scene.control.Menu class does not handle anything related to context menus - seems that menu logic happens in some entirely different place.
I believe the popups from the menu is done with ContextMenu (the same as right clicking menu). You will also be able to change the location using CSS. You can also use ScenicView and SceneBuilder to extensively debug your GUI and find the appropriate CSS paths (if you are not already doing so).
The relevant css classes can be found here How can I style a JavaFX menu and its items in CSS? and padding and margin can be used to move the context menu.
Check out the default CSS style called Modena. Line 1166 is where the menu stuff starts.
Hope this helps.
UPDATE:
Here is my test:
As it turns out, the context menu pops up at the bottom of the menu button. As far as I know there is no way to MOVE the actual node via CSS.
This means, as it overlaps for you, the menu button is not as big as your menubar, if you make it as big as your menubar (as I did in the picture) it will appear perfectly under the menubar.
As you can see, instead of using padding on the menubar, I use it on the menu buttons. This will automatically resize the menubar and cause the contextmenu to pop up perfectly in place.
I have also removed the color to show you that it is fully transparent if you remove the color.
As you can see here:
-fx-effect: null; also removes the default shadow effect. If you want more extensive control over this, you will need to implement your own menu buttons and your own context menu. Only then, you will have full control.
The actual code for showing the popup is located in MenuButtonSkinBase:
private void show() {
if (!popup.isShowing()) {
popup.show(getSkinnable(), getSkinnable().getPopupSide(), 0, 0);
}
}
Two last parameters to popup.show are X and Y offset of popup. Unfortunately, method is marked as private for some reason, so it is not possible to simply create a subclass of MenuButtonSkin and override that method.
One possible solution is to copy-paste code from MenuButtonSkinBase and MenuButtonSkin into your own file (about 300 lines of code) and tweak the method there. After that, you will be able to do:
menuButton.setSkin(new TweakedMenuButtonSkin(menuButton));
You dont need to copy the whole skin. If you extend the skin you can change this fairly easily (even though the method is private). Just copy paste the following.... (change the position to whatever you want in the "show" method)
#Override
protected void handleControlPropertyChanged(String p) {
if ("SHOWING".equals(p)) {
if (getSkinnable().isShowing()) {
show();
} else {
super.handleControlPropertyChanged(p);
}
} else {
super.handleControlPropertyChanged(p);
}
}
private void show() {
if (!popup.isShowing()) {
popup.show(getSkinnable(), getSkinnable().getPopupSide(), 100, 100);
}
}

Jtextpane click to create popup menu effect on selection not as intended

I have a JTextPane sitting in a JFrame, with a popup menu that is assigned to the JTextPane through the JTextPane.setComponentPopupMenu method.
I want to give the JTextPane a "Word-like" popup behavior. By that I mean, if you right click outside of your current text selection, the caret will reposition to where you right clicked, with menu options that affect a text selection (such as cut, copy, or bold) disabled. If you right click within your current text selection, the popup will appear with options that effect text selection enabled, the text selection will persist, and the caret will not move.
The problem is I cannot seem to find where I can put the code that handles the selection change. I tried:
Using the "PopupMenuWillBecomeVisible" event which is triggered before a popup becomes visible. The event passed into this method does not contain any mouse event information so there is no way for me to use viewtomodel to find out how to modify the selection. I could use MouseInfo but that seems dubious at best.
Using MousePressed/MouseReleased events in the JTextPane or JFrame. Apparently, neither of these events are invoked when a popup menu is triggered. In fact, I still can't determine what the parent component of my popup menu is. (I did read that in windows "MouseReleased" is the popup trigger, while in other systems "MousePressed" is the trigger. I tried both and neither worked).
So, I guess the problem is that I can't seem to find a place to put code where it would be called before the popup menu becomes visible, but has awareness of the mouseEvent that triggered the popup menu. I must be missing something here.
with a popup menu that is assigned to the JTextPane through the JTextPane.setComponentPopupMenu method.
You can use the older approach of displaying the popup based on your own custom MouseListener.
See the section from the Swing tutorial on Bringing Up a Popup Menu. Now you have access to the MouseEvent so you can convert that point to a point in the Document so you know where the click was made, on selected or unselected text.

Bad event on java panel

I have a java panel with 4 buttons. When I click on of these buttons, a new frame appears and the first is hidden with setVisibile(false).
On that new window, I have another button, but when i click it, I got the event corresponding to the fourth button of the first window. Clicking the button again does the trick, but of course this is not acceptable.
Am I missing something? I just show the frames with
nameOfTheFrame.setVisible(true);
and I have MouseListeners on every button.
The code of the last button is simply:
System.exit(0);
EDIT
Sample code:
private void btn_joinGamePressed(java.awt.event.MouseEvent evt) {
GraphicsTools.getInstance().getCreateGame().setVisible(false);
GraphicsTools.getInstance().getMainPanel().setVisible(false);
GraphicsTools.getInstance().getRegistration().setVisible(true);
}
GraphicsTools is a Singleton.
EDIT 2
Some more informations.
I noticed that on MAC OS works fine. The problem happens only on Linux and Windows.
This must be happening because of your mouse listeners. May be it is identifying the old button in your first click which is in the same location of new button (It is just my guess).
Change the mouse listeners to action listeners. For a button, it is sufficient if you have action listener.
Try this.
Try calling revalidate() on the frames as you change their viability.
Edit:
It could be something with the creation of the frames. Make sure you are calling 'pack()` on the frames.

How to make a button deep in a nested Swing panel get the "keyboard focus"?

I have a swing frame that contains embedded panels that contain other panels, etc.
Deep down, there is a button. I want the button to get focus so that pressing the "enter" key would generate an actionPerformed event.
However, if I do myButton.requestFocus() or myButton.requestFocusInWindow() the whole window gets the focus, but nothing seems to happen in terms of keyboard.
I'm obviously missing something about the focus subsystem.
Update2: I explicitly added a KeyListener in addition to the ActionListener and now it works. This is really weird, since I thought that actionListener includes both key and mouse actions.
For the enter key to work you probably want to set the default button rather than the keyboard focus:
button.getRootPane().setDefaultButton(button);
If you really want the keyboard focus then your problem might be related to when you call requestFocus. Sometimes if it is called before a component is fully visible it can be ignored. To fix that you can delay the requestFocus call until after other events have been processed:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
button.requestFocus();
}
});
Sounds like requestFocus() is failing at some level. Try testing to see if any of the parent jPanels or other components can request focus, and work your way up to find out where the problem lies.
There is a way to programatically specify the functionality of the tab ( you know when you press tab and the next widget gets selected )
By default it follows the way the components were added.
Using this custom mechanism will let you select your nested button as the first which receive the actionperformed event.
Unfortunately I don't remember what the name for this "mechanism" is, but is something like traversal or focus traversal
First, don't use requestFocus() use requestFocusInWindow(). requestFocus has platform specific issues, while requestFocusInWindow is more consistent.
Your actual problem; the component (or one of its parents) is probably not visible, or has been render non-focusable.
I want the button to get focus so that
pressing the "enter" key would
generate an actionPerformed event.
The is LAF dependents. Enter works in Windows, but not the Metal LAF. Check out Enter Key and Button for more inforation.
The requestFocusInWindow() method only works if the Component is currently visible on the frame. There are no other tricks so we are just making random guesses about what your are doing wrong. If you need further help you need to post a SSCCE demonstrating the problem.
You can get the rootPane of the frame and update the inputMap and actionMap. See the below code.
InputMap map = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
map.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "ok");
ActionMap actionMap = getRootPane().getActionMap();
actionMap.put("ok", enterAction);
Here enterAction is a AbstractAction object whose actionPerformed() will be called when user presses Enter.

Categories