I have a jwindow(set to be always on top) that you can click to get a pop menu. If the user right clicks the window it shows the pop menu but then if the user clicks any other window(such as firefox) pop menu does not disappear.
I tried to fix it by adding FocusListener on the jwindow, i implemented FocusListener and override
public void focusGained(FocusEvent e) {
System.out.println("gain" );
}
public void focusLost(FocusEvent e) {
System.out.println("lost" );
}
but there event never get called. i also tried the following,
addWindowFocusListener(new WindowAdapter() {
public void windowGainedFocus(WindowEvent e) {
System.out.println("gain 2" );
}
});
this event also not called.
All this jwindows has is a single JLabel with a picture on it.
From memory JWindow's do not receive focus/window events.
You are suppose to call setFocusableWindowState(true) on a JWindow to allow it to be focusable. But that "still" is not enough. The JWindow must also have focusable Components and I'm still not able to get it to work.
Using JFrame setUndecorated() seems the best choice.
To be focusable, a JWindow needs to be created with a parent Frame, like new JWindow(parentFrame). Do that and I think you should find it will automatically get the focus when you set it to visible.
Not really sure what you are trying to do. If you are trying to hide the popup manually then you should probably use a WindowListener and handle the windowDeactivated event.
If you really want to display a popup menu, you should be using JPopupMenu, not implementing it yourself.
Related
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;
}
}
}
}
});
I have a class that extends JPanel and implements MouseListener. In this class, if the panel is clicked the following two functions are executed:
#Override
public void mouseClicked(MouseEvent e) {
displayExitPopup();
}
private void displayExitPopup() {
JPopupMenu exitPopup = new JPopupMenu();
exitPopup.add("Exit Game");
exitPopup.add("Cancel");
exitPopup.show(this, this.getWidth(), this.getHeight());
}
This all works fine and the popup displays the 2 options as it should.
Now I am trying to perform actions when either of the two options in the popup menu are clicked - System.exit(0) if Exit Game is clicked, and the popup menu to close if Cancel is clicked. How can this be done?
If you take a look at the JavaDocs for JPopupMenu#add(String), you will find that it's a convenience method which returns a JMenuItem, you should then be able to add an ActionListener to it
Take a look at Bringing up a PopupMenu for more details
Having said that, I would instead, encourage you to make use the Action API which will allow you to create self contained units of work, which also provide the information needed to create the JMenuItem used by the JPopupMenu
Have a look at How to Use Actions for more details
You should also be using JComponent#setComponentPopupMenu instead of trying to use a MouseListener, as different platforms have different triggers for a popup menu and it's complicated and ugly real quick
When adding a item to JPopupMenu, you get a JMenuItem. On this object, you can call addActionListener to add a action listener, like you have with a JTextField or a JButton.
exitPopup.add("Cancel").addActionListener(e-> {
// do something
});
So I have a Java Swing program and I want to be able detect mouse clicks.
addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
update(evt); //another method in the program
}
});
The code works if I click on the window's side or places where there isn't an object, but does not work when I click on objects in the JFrame, such as my JTable or my text field.
Please help me how to have the MouseListener work on objects inside the JFrame as well.
When you click on a textfield, the textfield gains focus. That means your frame loses ownership of focus, and since your listener is most likely added to your frame, your listener stops working right when frame isnt in focus. Add your listener to all compoments, or use Key Binding
I've created a simple Swing panel that, when loaded, takes up my application's entire window. It contains two JTextAreas and a handful of buttons. I want one of the text areas to have the focus when the panel loads, so that the user can immediately start typing instead of having to click on the text area first. How can I achieve this?
By default focus goes to the first component defined on the window.
If this is not the component you want to have focus then you need to request focus once the window is realized.
The Dialog Focus example shows a couple of ways to do this.
See here the Documentation which contains exactlly what you are searching for (I think):
A component can also be given the
focus programmatically, such as when
its containing frame or dialog-box is
made visible. This code snippet shows
how to give a particular component the
focus every time the window gains the
focus:
//Make textField get the focus whenever frame is activated.
frame.addWindowFocusListener(new WindowAdapter() {
public void windowGainedFocus(WindowEvent e) {
textField.requestFocusInWindow();
}
});
You just need to call requestFocus method of Jcomponent class,
public void requestFocus()
On the Component that you want to focus.
And pleas make sure that you call this method after setVisible is called for its parent component.
For example:-
You have a Jframe in which you added a JTextArea, so after calling you should call in following order:-
jframe.setVisible(true);
jarea.requestFocus();
When i right click on a JTable in a JFrame I show a JPopupMenu. If I left this JPopupMenu shown as it is and moved with the mouse to the JTable I can still hover on its rows.
This is not the default behavior of Windows applications. In normal case if a popup menu appears in a program it blocks any hover actions on the popup owner window.
Can i do the same thing in Java ?
One way to approach this problem is to set an instance variable in one of your GUI elements to flag whether or not to enable hover events. I have shown below how this may work, but it's not in its complete form, you will also need to re-enable hover when the JPopupMenu is dismissed, and also check the state of the ENABLE_HOVER field before firing hover effects.
public MyTable extends JTable {
private boolean ENABLE_HOVER = true;
public MyTable() {
...
this.addMouseListener(new MouseListener(){
...
public void mouseClicked(MouseEvent e) {
if (isRightClick(e)) {
setHoverEnabled(false);
showJPopupMenu();
}
}
});
}
protected void setHoverEnabled(final boolean hover) {
this.ENABLE_HOVER = hover;
}
}
Another method which may be better suited to disabling multitudes of however enabled elements is to intercept the events at the glass pane. An example of how this might work is shown here. Be warned though if your interface is already built it may require significant re-jigging of your component classes.
You will need to intercept all events at the glass pane, if hover is enabled (no popup menu shown) you would pass the event to the appropriate component. Otherwise if hover is disabled and the MouseEvent occurred over the JPopupMenu is passed only to the JPopupMenu.