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/
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/
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.
Those are menu items with icons. They are not buttons!
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);
}
}