Show JFrame when tray icon was clicked even if minimized - java

I need to restore a hidden window when tray icon is clicked. I've actually already found partial solution:
tray_icon.addMouseListener(new MouseListener() {
#Override
public void mouseClicked( MouseEvent e ) {
//Gui.this refers to my frame
Gui.this.setVisible(true);
}
});
This will show the frame if it's behind another window. It will put the frame on top. But if I minimize the frame, it doesn't show the window. It's interesting however, that it slightly highlights the taskbar tab:
Taskbar flashing is nice, but it's not enough:
I want to display the JFrame regardless of the way it was hidden.
Note that I plan to allow "minimize to tray" function. This means I'll be even hiding the window completely (provided Java allows it). It still must be possible to show it.

Along with setVisible, there's other thing to be set:
Gui.this.setState(Frame.NORMAL);
When minimized, the frame's state is Frame.ICONIFIED.
This is the complete callback to restore the hidden frame:
tray_icon.addMouseListener(new MouseListener() {
#Override
public void mouseClicked( MouseEvent e ) {
Gui.this.setVisible(true);
Gui.this.setState (Frame.NORMAL);
}
}
And this is what I use to hide the window and taskbar panel (minimize to tray):
this.addWindowListener(new WindowAdapter()
{
#Override
public void windowIconified(WindowEvent event) {
//Hides it from screen
Gui.this.setState(Frame.ICONIFIED);
//Hides it from taskbar and screen
Gui.this.setVisible(false);
}
});

Related

How to "close" a JMenu in an Event Handler - Java

I have a simple MouseAdapter that I add to a JMenu. I want the menu to show its contents when moused over, react when clicked on, and hide its contents when the mouse leaves it. Here's where I add the listener:
public final void addGuiReaction(Runnable clickReaction) {
JMenu THIS = this;
this.addMouseListener(new MouseAdapter() {
final Runnable reaction = clickReaction;
final JMenu source = THIS;
#Override
public void mouseClicked(MouseEvent arg0) {reaction.run();}
#Override
public void mouseEntered(MouseEvent arg0) {source.doClick();} // Opens the menu
#Override
public void mouseExited(MouseEvent arg0) {/* Need to "close" the menu */}
});
}
This works just fine for both the reaction and the open when moused over functionality, but I can't figure out how to close it. Tried both setSelected(false) and setPopupMenuVisible(false) but the issue with both is that the menu doesn't open again the next time I mouse over it.
Anyone knows a nice way of closing the menu?
Answering to "How to close the JMenu..."
In place of this:
/* Need to "close" the menu */
You can put this:
source.dispatchEvent(new KeyEvent(source, KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_ESCAPE));
Hence simulate the ESC key press which will trigger the respective Action from the ActionMap of the JMenuBar.
However this will not fully solve your problem as while the keyboard arrow keys will work, you will not be able to use the mouse to select any of the items of the JMenu as trying to enter any of them will trigger the JMenu mouseExited event. You may need to attach a proper mouse event listener to the menu items as well.

Get mouse position of a JFrame even when its child component is focused

I want to get the mouse position of my JFrame. But when mouse is on its child component (such as a table, button that is added to the JFrame) MouseMotion event is no more listening. How can I get the mouse position?
I was trying to make a Sidebar on my Swing application, where sidebar is an undecorated JFrame. I have set to dispose it when mouse exits. But when I move the mouse over a component added to the sidebar, it disappears. Idea may be dumb I am new to Java.
You could implement mouseExited as kleopatra suggests, but do it similar to this:
MouseListener closer = new MouseAdapter() {
public void mouseExited(MouseEvent e) {
// obtain source frame and see if mouse has left it
Container cnt;
if (e.getSource() instanceof JFrame) {
// our frame, no conversion needed
cnt = (Container) e.getSource();
} else {
// inside a descendant
cnt = SwingUtilities.getAncestorOfClass(
JFrame.class, e.getComponent());
// convert mouse event to make it appear
// as if the frame generated it (I think :D)
e = SwingUtilities.convertMouseEvent(
e.getComponent(), e, (Component) cnt);
}
Rectangle r = new Rectangle(cnt.getSize());
if (!r.contains(e.getPoint())) {
cnt.setVisible(false);
// or whatever
}
}
};
This is meant to be set for all descendant components of your sidebar and itself. It should check if your mouse is still inside your sidebar no matter over which of it's children/descendants the mouse is hovering.
You should also consider using an undecorated JDialog instead of a JFrame.
The reason why your sidebar is disappearing might be that you only added a mouse listener to it and not to any of it's children. It might be counter-intuitive, but when your mouse pointer enters a child/descendant of the sidebar, a mouseExited event is generated for the sidebar and then a mouseEntered event is generated for whichever child/descendant the mouse has entered. This is just how Swing mouse events are designed to work and there ain't much you can do about it.
Assuming the use-case in your comment is the real issue to solve, the answer is to implement the mouseExited such that it checks whether the mouse is still somewhere over the frame and hide it only if not.
Something like:
MouseListener closer = new MouseAdapter() {
#Override
public void mouseExited(MouseEvent e) {
Rectangle r = new Rectangle(sideBar.getSize());
if (!r.contains(e.getPoint())) {
sideBar.setVisible(false);
}
}
};

stackoverflow error when I add a glasspane to my frame

I am trying to add a universal right click to textfields in my application. I came across a solution where I could add a glass pane on top my frame, make it invisible and register that as a universal mouse listener. If the component is a text field I show the pop up menu, otherwise I redispatch the event. I have pasted the code below...
This example works fine. But when I use this with my application though, I get a stackoverflow error at
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at apple.awt.CWindow._getLocationOnScreen(Native Method)
at apple.awt.CWindow.getLocationOnScreen(CWindow.java:878)
at java.awt.Component.getLocationOnScreen_NoTreeLock(Component.java:1960)
at java.awt.Component.getLocationOnScreen(Component.java:1938)
at javax.swing.SwingUtilities.convertPointToScreen(SwingUtilities.java:364)
at javax.swing.SwingUtilities.convertPoint(SwingUtilities.java:165)
at com.aesthete.csmart.ui.common.components.RightClickGlassPane.redispatchMouseEvent(RightClickGlassPane.java:79)
at com.aesthete.csmart.ui.common.components.RightClickGlassPane.mouseEntered(RightClickGlassPane.java:61)
I understand that every time the mouse is entered on a component the glass pane receives the event and then redispatches. But why is it getting converted into a recursive call?
EDIT:
Just wanted to show everyone how I solved it with Camickr suggestion:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final JPopupMenu popup = new JPopupMenu();
JMenuItem mnItemCopy = new JMenuItem("Copy", CommonUI.getScaledImage(13, 13, "/images/copy.png"));
JMenuItem mnItemCut = new JMenuItem("Cut", CommonUI.getScaledImage(13, 13, "/images/cut.png"));
JMenuItem mnItemPaste = new JMenuItem("Paste", CommonUI.getScaledImage(13, 13, "/images/paste.png"));
popup.add(mnItemCopy);
popup.add(mnItemCut);
popup.add(mnItemPaste);
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
#Override
public void eventDispatched(AWTEvent event) {
if(event instanceof MouseEvent) {
MouseEvent mouseevent=(MouseEvent)event;
if(mouseevent.isPopupTrigger()) {
if (mouseevent.getComponent() instanceof JTextField) {
popup.show(mouseevent.getComponent(), mouseevent.getX(), mouseevent.getY());
}
}
}
}
}, AWTEvent.MOUSE_EVENT_MASK);
}
});
I am trying to add a universal right click to textfields in my application.
Check out Global Event LIsteners. Just check the source of the event and do processing as required. No need to redispatch events.
Note, you should NOT assume a right click is the LAF way to display a popup. Read the section from the Swing tutorial on Bringing Up a Popup Menu for a better solution.

Hide JWindow when applet not visible?

I create a JWindow in my applet to display an update process, and I set the owner to the applet parent. My problem is that the JWindow is visible in all the tabs in my browser and not just the one containing my applet. Is it possible to add a Listener to know when my applet is visible? (And then hide the JWindow when it's not)
Applets appear to be added to a system Frame when they are displayed so you can use a WindowListener. I added the following code in the init() method of the JApplet:
Window window = SwingUtilities.windowForComponent(this);
window.addWindowListener( new WindowAdapter()
{
#Override
public void windowActivated(WindowEvent e)
{
displayWindow.setVisible( true );
}
#Override
public void windowDeactivated(WindowEvent e)
{
displayWindow.setVisible( false );
}
});
I don't play much with applets but I think thats what the start() and stop() methods are for. You would make the window visible in start() and hide the window in stop().

Java modal window with maximize button

How could I create a window which is modal and has a maximize button?
So is it possible to create a modal JFrame or create a JDialog with maximize button?
On most look and feels, modal windows (such as JDialog) do not have a maximise button simply because they're not supposed to be maximised (or minimised) at all.
It's possible with some tricks to add a maximise button, but it would be completly against the way JDialog is supposed to work.
If you need a maximise button, the best solution would be using a JWindow or a JFrame instead of a JDialog. Those windows support maximisation and minimisation.
WARNING: You shouldn't do that, no matter what.
A trick to do this in JDialog:
setUndecorated(true);
getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
Solution 1: Tested on Windows
I used a JFrame for the modal window
JFrame mainWindow = new JFrame;
mainWindow.setVisible(true);
JFrame modalWindow = new JFrame();
// The next two sentences gives modalWindow modal beahaviour
mainWindow.setEnabled(false);
mainWindow.setFocusable(false);
modalWindow.setVisible(true);
Solution 2: Tested on Ubuntu
I added a WindowFocusListener
addWindowFocusListener(new java.awt.event.WindowFocusListener() {
public void windowGainedFocus(java.awt.event.WindowEvent evt) {}
public void windowLostFocus(java.awt.event.WindowEvent evt) {
formWindowLostFocus(evt);}
private void formWindowLostFocus(java.awt.event.WindowEvent evt) {
this.requestFocus();
this.toFront();}
Here is an alternate / slightly more detailed answer.
Try Are You Missing Maximize Button? (formerly here). This is a github archive of blog articles and code by Santhosh Kumar Tekturi from the now defunct JRoller site.
It is a complete utility class that makes a Frame mimic a Dialog, similar to the other answers. It involves adding a WindowListener to the Frame to keep the frame on top of its owner and keep its owner frame disabled (warning: in the windowClosed method it should probably be frame.removeWindowListener(this);, and a WindowListener to the owner to keep the frame on top of it and to remove the listener. It also uses its own EventQueue to process events. Note this is an old post, so as mentioned in the code there may be newer APIs to deal with this code better.
Here is the core function. See the link for the rest. Note: the code in the repository differs from the article; I believe the repository is more developed.
// show the given frame as modal to the specified owner.
// NOTE: this method returns only after the modal frame is closed.
public static void showAsModal(final Frame frame, final Frame owner){
frame.addWindowListener(new WindowAdapter(){
public void windowOpened(WindowEvent e){
owner.setEnabled(false);
}
public void windowClosing(WindowEvent e) {
owner.setEnabled(true);
}
public void windowClosed(WindowEvent e){
frame.removeWindowListener(this); // originally called on owner
}
});
owner.addWindowListener(new WindowAdapter(){
public void windowActivated(WindowEvent e){
if(frame.isShowing()){
frame.setExtendedState(JFrame.NORMAL);
frame.toFront();
}else
owner.removeWindowListener(this);
}
});
owner.toFront();
frame.setVisible(true);
try{
new EventPump(frame).start();
} catch(Throwable throwable){
throw new RuntimeException(throwable);
}
}

Categories

Resources