How to show SystemTray Message when Frame is not iconified - java

Is there a way to display a TrayIcon Message
tray.displayMessage("Warning", "Array index out of range", TrayIcon.MessageType.INFO);
when a JFrame is not iconified?
MadProgrammer brought me the idea. I had the code
if(e.getNewState()==NORMAL){
tray.remove(trayIcon);
setVisible(true);
}
so the TrayIcon was removed when i tried to display a message.

Related

How to put two components at the same place

I am currently building a game and I don't know how to make a main menu. I don't know how to put the text on the GIF background. Now the thing is when I try to put them on I try this code:
private void initializeDisplay() {
playBtn.setBorderPainted(false);
playBtn.setFocusPainted(false);
playBtn.setContentAreaFilled(false);
playBtn.setText("PLAY");
playBtn.setFont(new Font("Poiret One", Font.TRUETYPE_FONT, 27));
playBtn.setForeground(Color.WHITE);
mainPanel.add(playBtn);
mainPanel.add(bgImg);
mainPanel.setLayout(new GridBagLayout());
bgImg.setLocation(new Point(200, 200));
mainPanel.setOpaque(true);
mainPanel.addKeyListener(new Keyboard());
mainPanel.addMouseListener(new Mouse());
mainPanel.setFocusable(true);
}
Also when I do that I use a GridBagLayout for the layout type but it seems that I'm not get the result I want, the problem is the button not the image in the background because that's appearing but the button isn't appearing.
You could also add an image to a jlabel's icon field, and then add the label to your panel's content pane.
This post explains it more:
How to set a background picture in JPanel

how i can get menu item of tray icon which is added by some other class

// TrayUtilitiesDemo is a call which is returning me tray icon create by current java process.
MyMenu.setLabel("MyMenu");
TrayUtilitiesDemo.addPopupMenu(MyMenu);
TrayIcon trayIcon = TrayUtilitiesDemo.getTrayIcon();
System.out.println("TrayIcons are: "+trayIcon);
when i am doing
trayIcon.getPopupMenu().countItems();
it is retrun only 1 menuitem. which is added by me that is MyMenu.
there are other 4 menuitems are there added by some other class which is creating this tray icon.
and not able to get the ActionListener also.
basically i want to right click on tray icon click and click on menuitem in PopupMenu added by other class(or by other process) for automation.
using windows 7 machine.
please help.

Making a JDialog always on top of application

I'm making a program that requires this jdialog box to always be focused and on top, ie: make the ding sound when i click on the parent window. here's what i have so far:
JDialog dialog = new JDialog();
// dialog.setAlwaysOnTop(true);
dialog.setSize(400, 220);
dialog.setLocationRelativeTo(relativeTo); //relativeTo is the name of parent frame
dialog.setVisible(true);
// dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
// dialog.setModal(true);
(the commented things are those i have tried unsuccessfully...)
how would i make this dialog box ontop of the parent window? any help would be great! thanks
You need to set the dialog's owner and make the dialog modal:
JDialog dialog = new JDialog(parentFrame, true); // owner, modal

How do I add a double-click event listener to tray icon using SWT?

I have an SWT tray icon which I've created with the following snippet of code:
itmTrayItem = new TrayItem(trySysTray, SWT.NONE);
itmTrayItem.setToolTipText("My App");
itmTrayItem.addListener(SWT.MenuDetect, new Listener() {
public void handleEvent(Event event) {
mnuPopup.setVisible(true);
}
});
Right clicking on the tray icon brings up a context menu. I'd like to add a double-click event to the tray icon so that when the icon is double-clicked I perform some action. How can I do this?
I haven't understood how I was use the mouse listener as I've been finding some parts of the SWT docs lacking in examples.
Thanks
try the SWT.DefaultSelection event, it may do the trick

JOptionPane.showConfirmDialog goes out of screen when the message is large

JOptionPane.showConfirmDialog(this,
message,
"title",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
The message can be 10 lines and message can be 500 lines. It changes dynamically. I want to implement a scroll bar if the message exceeds the screen height.
So I tried:
JTextArea textArea = new JTextArea (message);
JscrollPane scrollPane = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JOptionPane.showConfirmDialog(this,
scrollPane,
"title",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
This will open a dialog and a scroll bar in windows and it works fine, but in mac os the dialog goes out of screen.
Can any one help me?
You can set the preferred size for your scrollPane before showing the dialog to limit its size:
scrollPane.setPreferredSize(new Dimension(400, 200));
The dimension to use can be based on the screen size that you can retrieve like this:
Toolkit.getDefaultToolkit().getScreenSize()
Note that I have not tested this on other platforms than Mac OS X.

Categories

Resources