Adding a vertical separator in PopupMenu, in the task bar
Asked Answered
H

2

7

How can I add a vertical separator in the pop up menu of the app in the task bar ?

   tray = SystemTray.getSystemTray();             
   openMenuItem = new MenuItem("Open P");
   stopKLMenuItem = new MenuItem("Stop");
   exitMenuItem = new MenuItem("exit");
   menu.add(exitMenuItem);
   menu.add(stopKLMenuItem);
   menu.addSeparator(); // adds a horizontal separator
   menu.add(openMenuItem);
   trayIcon = new TrayIcon(image,"P",menu);

The statement menu.addSeparator() adds a horizontal separator. I also wanted a vertical separator like :

enter image description here

How do I do this ?

Harrovian answered 28/9, 2012 at 11:8 Comment(1)
no idea, only +1 for interesting questionGleanings
A
6

One easy trick is to add an empty icon to the JMenuItem. The look and feel then adds the vertical separator you are looking for (of course, this only works with the Windows L&F, other L&F may render this differently):

Menu example result

And the code that produced it:

import java.awt.AWTException;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestTrayIcon {

    protected void initUI() {

        TrayIcon trayIcon = new TrayIcon(new ImageIcon(
                "http://3.bp.blogspot.com/-nh7fv5FqpU4/TeUbTvAdSkI/AAAAAAAAAUo/Ig53KJGvzlk/s45/avatar.png").getImage());

        final JPopupMenu popupMenu = new JPopupMenu();
        JMenuItem checkBox1 = new JMenuItem("Last checked...", new ImageIcon(new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB)));
        JMenuItem checkBox2 = new JMenuItem("Open...");

        popupMenu.add(checkBox1);
        popupMenu.addSeparator();
        popupMenu.add(checkBox2);

        trayIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    popupMenu.setLocation(e.getX(), e.getY());
                    popupMenu.setInvoker(popupMenu);
                    popupMenu.setVisible(true);
                }
            }
        });
        try {
            SystemTray.getSystemTray().add(trayIcon);
        } catch (AWTException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (UnsupportedLookAndFeelException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                new TestTrayIcon().initUI();
            }
        });
    }

}
Ama answered 28/9, 2012 at 12:55 Comment(5)
I do not understand you..You mean vertical separator will be added on its own ? I tried this though but no result. code of addition & code of look and feelHarrovian
@SuhailGupta Yes, in Swing it appears automatically (try to run my example on your PC). Now, you need to use a Swing JPopupMenu and not the AWT PopupMenu . Since you can't set it on the TrayIcon directly, use this snippet to make it appear: trayIcon.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent e) { if (e.isPopupTrigger()) { jpopup.setLocation(e.getX(), e.getY()); jpopup.setInvoker(jpopup); jpopup.setVisible(true); } } });Ama
Your code works fine. But the code I made after adding mouse listener doesn't show the vertical bar. code. What is the problem ?Harrovian
@SuhailGupta I updated my code to use the SystemTray and the TrayIcon. It does exactly what you asked for.Ama
your code always works fine. But I am having some problem . Don't know what but I don't see the vertical separator as I run my written code. can we discuss the issue here @ discussion between Guillaume Polet and Suhail pleaseHarrovian
G
1

ideas

  1. add Borders (MatteBorders for example) to the JMenuItem(s)

  2. add JPanel to the JPopup

    • have to set BorderLayout for JPopup and put JPanel to the CENTER area (and/or with Borders too)

    • set BorderLayout for JPanel

    • there put JSeparator to the EAST or WEST area (depends or direction)

    • put there another JPanel with JButtons that simulating JMenuItem (doesn't difrerence on 1st sight) with output to the Swing Action or ActionListener

  3. the same things with JWindow (I using by default instead JPopup or ToolTip) instead of JPopup

  4. maybe I'm wrong but JPopup looks like as chameleon

    • once time as JPanel,

    • another as JWindow,

    • sorry no deepest knowledge, interest about

Gleanings answered 28/9, 2012 at 11:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.