Is it possible in Java Swing to show message box using tooltip item. I need to show tooltip not only when a mouse hovers some component but also when I choose specific item in context menu of this component when tooltips are turned off.
You can use PopupFactory to show your popup message
final Popup p = PopupFactory.getSharedInstance().getPopup(myComponent, new JLabel("Here is my popup!"), x, y);
p.show();
// create a timer to hide the popup later
Timer t = new Timer(5000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
p.hide();
}
});
t.setRepeats(false);
t.start();
where myComponent - is the component for which popup must be shown
x, y - coordinates of popup.
Related
I am working on an application primarily in swing, though the chart dialog uses JavaFX. (Chart in a GridPane in a JFXPanel to be exact.)
First, Chart does not implement a setContextMenu method to use a JFX ContextMenu. I have seen a workaround by attaching a handler to the right click of the chart, but the menu is not removed from the GUI when it loses focus, rather it persists until the user either 1) triggers it to open again or 2) clicks a MenuItem.
chart.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent t) {
if (t.getButton() == MouseButton.SECONDARY) {
contextMenu.show(chart, t.getScreenX(), t.getScreenY());
}
}
});
That said, I would prefer to use a Swing JPopupMenu instead. How can I bind this to my Chart and maintain a natural behavior?
Thank you!
I determined it was not possible to use a JPopupMenu.
The ContextMenu is added to a JLabel:
Label contextMenuLabel = new Label();
grid.add(contextMenuLabel, 0, 0); // Add to the same grid after adding the chart
contextMenuLabel.setPrefSize(width, height);
contextMenuLabel.setMinSize(width, height);
contextMenuLabel.setMaxSize(width, height);
ContextMenu contextMenu = new ContextMenu();
contextMenuLabel.setContextMenu(contextMenu);
I am trying to make my application minimize on taskbar and restore when I double click on the trayIcon. I also have a popup menu that has an item which restores the window when clicked.
trayIcon = new TrayIcon(image, "Anything", popup);
trayIcon.addActionListener(actionListener);
trayIcon.addMouseListener(mouseListener);
sysTray.add(trayIcon);
And here is the code of the actionListener and mouseListener:
private ActionListener actionListener = new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("Restore"))
{ // RIGHT CLICK -> RESTORE
// Do something
}
}
};
private MouseListener mouseListener = new MouseListener()
{
#Override
public void mouseClicked(MouseEvent e)
{
if (javax.swing.SwingUtilities.isLeftMouseButton(e) && e.getClickCount()>1)
{ // DOUBLE LEFT MOUSE CLICK
// Do something
}
}
// Rest of the code
}
The Restore option of the popup menu works fine, however when I am double-clicking on the trayIcon at the System Tray I get a Null Pointer Exception at line if(e.getActionCommand().equals("Restore"))
How can I eliminate this and if possible merge both listeners into one?
N.B. This answer is made from a conversation with the OP in the comments, and some of the solutions came from the OP
The TrayIcon does not populate the ActionCommand field when firing the event, hence the code dies with an NPE.
Since the tray icon only calls its ActionListener only when double clicked or in an analagous action (via keyboard), you can create a RestoreListener that does not check that condition at all, and is only used with the tray icon and the "Restore" menu item.
private ActionListener restoreListener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// Do the actual restoration
}
};
And actually adding it to the items...
trayIcon = new TrayIcon(image, "Anything", popup);
trayIcon.addActionListener(restoreListener);
MenuItem restoreMenuItem = new MenuItem(...);
restoreMenuItem.addActionListener(restoreListener);
This seems to behave slightly differently from a MouseListener that it does not put the window on top, this can be remedied by calling toTop() on it.
I need to set component's location in a window.
I need to draw component on a GlassPane near another component, which was clicked. I pass the component, which raises click event to some manager, there I want to get coordinates where to paint.
public void mouseClicked(MouseEvent e) {
ApplicationManager.getInstance().drawOnGlassPane((Component e.getSource());
}
public void drawOnGlassPane(final Component caller) {
mainFrame = (JFrame) SwingUtilities.getWindowAncestor(caller);
JPanel glassPane = (JPanel) mainFrame.getGlassPane();
glassPane.setVisible(true);
Point where = caller.getLocationOnScreen();
JButton btn = new JButton("on glass pane");
btn.setBounds((int) where.getX(), (int) (where.getY() + caller.getHeight()), 50, 20);
glassPane.add(btn);
}
}
The new component appears in a wrong place. How could I set correct location?
this question is described including example in the tutorial about How to Use Root Panes, another example here and here
I am creating a GUI in which my home page has a button labelled "Welcome to the Panel"
The point is that when you press on this button, it will navigate to a new page where I will have other functions. My only problem is that I dont know the syntax or how that when clicking a button, it will navigate to new page.
JButton btn = new JButton("Welcome to the Panel");
btn.setActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
// Here you open the other window. You can use JFrame, JOptionPane or JDialog
}
});
button.addActionListener(new ActionListner()
{
public void actionPerformed(ActionEvent ae)
{
//code to show pane
}
});
You need to register an ActionListener on your button and inside that action listener you make that panel (the page) visible.
How you do that depends on your layout, i.e. with a CardLayout you'd show the corresponding card (here's the doc). Using other layouts you might have to replace a component, e.g. if you use a BorderLayout and your content is placed in the center, replace the center component with the panel you want to show.
Note that if you're not familiar with layout managers yet, you should first have a look at those before doing dynamic changes to the ui (like navigation etc.).
hii
I have used Image as JButton for set in to panel
but now i want to use mouse motion listener actions on that image
for that purpose what can i do
following is the code for image
JButton buttonForPicture = new JButton();
buttonForPicture.setBorder(new EmptyBorder(0, 0, 0, 0));
buttonForPicture.setOpaque(false);
buttonForPicture.setIcon(new ImageIcon("/Users/hussainalisyed/Documents/Images/pic9.jpg"));
panel5.add(buttonForPicture,BorderLayout.CENTER);
is there any another way to do that
or
...
I'm not sure exactly what you're asking?
Your button is like any other JButton:
buttonForPicture.addMouseMotionListener(new MouseMotionListener() {
#Override
public void mouseMoved(MouseEvent e) {
}
#Override
public void mouseDragged(MouseEvent e) {
}
});
That catches movement events for the whole button, not just the image.
Read the JButton API there are methods to change the icon on a mouse rollover, if thats what you are trying to do. Search the API for methods containing "icon" to see what your options are.
If you just want to know how to write a MouseMotionListener, then read the section from the Swing tutorail on How to Write a Mouse Motion Listener for a working example.