-2

I don't know the name of this type of menu, but I want to add an item to this kind of menu.

Follow the link for an example of the menu: Link: http://postimg.org/image/7izr3zapl/full/

DWright
  • 9,258
  • 4
  • 36
  • 53
Lightspeed360
  • 161
  • 1
  • 10
  • I haven't researched it, but I know that that is platform dependent (i.e. only on Windows). As far as I know Java was designed to be platform independent, so what you're asking might be impossible to do. However, like I said, I haven't researched this. – Mark Said Camilleri Apr 26 '15 at 20:46
  • These menus are called jump lists and can be added by a WPF .Net application, see: [Adding Taskbar context menu to win7 application](http://stackoverflow.com/questions/3156378/adding-taskbar-context-menu-to-win7-application). It is more difficult to do that from a Java application, but might be possible. – Freek de Bruijn Apr 26 '15 at 20:57
  • IntelliJ IDEA shows a list of recent projects in its jump list, so you could look at the [WinDockDelegate](https://github.com/JetBrains/intellij-community/blob/3f7e93e20b7e79ba389adf593b3b59e46a3e01d1/platform/platform-impl/src/com/intellij/ui/win/WinDockDelegate.java) class and how it is used there. – Freek de Bruijn Apr 26 '15 at 21:05
  • See also [How to create great screenshots?](http://meta.stackexchange.com/q/99734/155831) BTW - those are menu items with **icons**. They are not buttons! – Andrew Thompson Apr 27 '15 at 01:14

2 Answers2

1

IntelliJ IDEA has implemented this for Windows and OS X, so you could use it as an example.

Focusing on Windows here, you can take a look at the RecentTasks class for the implementation. To add the recent tasks, the addTasksNativeForCategory native method gets called. This C++ method is implemented in the following file: jumplistbridge.cpp.

Freek de Bruijn
  • 3,552
  • 2
  • 22
  • 28
1

Those are menu items with icons. They are not buttons!

Menu with Icons

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.net.URL;
import javax.imageio.ImageIO;

public class MenuWithIcon {

    private JComponent ui = null;
    String[] urlStrings = {
        "https://i.stack.imgur.com/gJmeJ.png",
        "https://i.stack.imgur.com/gYxHm.png",
        "https://i.stack.imgur.com/F0JHK.png"
    };
    String[] menuNames = {
        "Blue Circle", "Green Triangle", "Red Square"
    };

    MenuWithIcon() {
        initUI();
    }

    public void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        Image img = new BufferedImage(300, 150, BufferedImage.TYPE_INT_ARGB);
        ui.add(new JLabel(new ImageIcon(img)));
    }

    public JMenuBar getMenuBar() throws Exception {
        JMenuBar mb = new JMenuBar();

        JMenu menu = new JMenu("Menu");
        mb.add(menu);

        for (int i=0; i<urlStrings.length; i++) {
            URL url = new URL(urlStrings[i]);
            Image img = ImageIO.read(url);
            JMenuItem mi = new JMenuItem(menuNames[i], new ImageIcon(img));
            menu.add(mi);
        }

        return mb;
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                MenuWithIcon o = new MenuWithIcon();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                try {
                    f.setJMenuBar(o.getMenuBar());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433