2

I am developing desktop application in java.On start up my application runs from system tray.But the system tray's context menu is having standard look and feel according to OS.I want to customize the context menu.I want to give gradient background to it , want to change fonts,borders etc.Let me know is this possible ? If there are some examples for that please provide links for the same..

Thanks in advance..

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Krutika
  • 17
  • 3
  • what do you mean by "runs from system tray"? that statement is a little confusing to me. Is [this](http://stackoverflow.com/questions/6403821/how-to-add-an-image-to-a-jframe-title-bar/6403846#6403846) what you're talking about? – mre Jul 04 '11 at 18:12
  • Check thw following link:http://download.oracle.com/javase/tutorial/uiswing/misc/systemtray.html – Krutika Jul 05 '11 at 16:29
  • Above link will explain the system tray functionality to you.Here menu is shown to which I want to give gradient background.I think this is enough to make my question clear to u.. – Krutika Jul 05 '11 at 16:34

1 Answers1

2

hmmm only this blog by one of SwingGuru's can hepl you with that

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class FunnyMenuTest {

    private JPopupMenu popupMenu;
    private JMenuItem m_mniInsertRow;
    private JMenuItem m_mniInsertScrip;
    private JMenuItem m_mniDeleterRow;
    private JMenuItem m_mniDeleteExpiredScrip;
    private JMenuItem m_mniSetAlert;

    public void makeUI() {
        JFrame frame = new JFrame("Funny JMenu Test");
        popupMenu = new JPopupMenu();
        m_mniInsertRow = new JMenuItem("Insert a Row");
        m_mniInsertRow.setOpaque(false);
        m_mniInsertScrip = new JMenuItem("Insert a Scrip");
        m_mniInsertScrip.setOpaque(false);
        m_mniDeleterRow = new JMenuItem("Delete a Row");
        m_mniDeleterRow.setOpaque(false);
        m_mniDeleteExpiredScrip = new JMenuItem("Delete a Expired Scrip");
        m_mniDeleteExpiredScrip.setOpaque(false);
        m_mniSetAlert = new JMenuItem("Set Alert");
        m_mniSetAlert.setOpaque(false);
        popupMenu.add(m_mniInsertRow);
        popupMenu.add(m_mniInsertScrip);
        popupMenu.addSeparator();
        popupMenu.add(m_mniDeleterRow);
        popupMenu.add(m_mniDeleteExpiredScrip);
        popupMenu.add(new JSeparator());
        popupMenu.add(m_mniSetAlert);
        JMenuBar menuBar = new JMenuBar();
        for (int i = 0; i < 5; i++) {
            JMenu menu = new JMenu("Menu " + i);
            for (int j = 0; j < 5; j++) {
                JMenuItem item = new JMenuItem("Item " + j);
                item.setOpaque(false);
                menu.add(item);
            }
            menuBar.add(menu);
        }
        JMenu menu = new JMenu("Menu 1");
        JMenuItem item = new JMenuItem("Item ");
        item.setOpaque(false);
        menu.add(item);
        menuBar.add(menu);
        JPanel jp = new JPanel();
        jp.setPreferredSize(new Dimension(400, 400));
        jp.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    int x = e.getX();
                    int y = e.getY();
                    popupMenu.show(e.getComponent(), x, y);
                }
            }
        });

        frame.getContentPane().add(jp);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(400, 400));
        frame.setLocationRelativeTo(null);
        frame.setJMenuBar(menuBar);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new FunnyMenuTest().makeUI();
            }
        });
        UIManager.put("PopupMenuUI", "JMenu.GradientPopupMenuUI");
    }
}

and

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.MultipleGradientPaint.CycleMethod;
import java.awt.Paint;
import java.awt.geom.Point2D;
import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicPopupMenuUI;

public class GradientPopupMenuUI extends BasicPopupMenuUI {

    private Paint paint = new LinearGradientPaint(new Point2D.Double(5, -5), new Point2D.Double(),
            new float[]{0.2F, 0.9F}, new Color[]{Color.WHITE, Color.GREEN}, CycleMethod.REFLECT);

    public static ComponentUI createUI(JComponent c) {
        return new GradientPopupMenuUI();
    }

    @Override
    public void paint(Graphics g, JComponent c) {
        ((Graphics2D) g).setPaint(paint);
        g.fillRect(0, 0, c.getWidth(), c.getHeight());
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • +1 for the gradient popup menu. The only doubt is the UI is L&F dependent. Changing L&F may damage this. – StanislavL Jul 05 '11 at 06:13
  • @mKorble : In the link of the blog , JMenuItem is used.But this JMenuItem can not be used with SystemTray.So do u have any idea how can i used jMenuItem(or JPopupMenu ) in System Tray? – Krutika Jul 05 '11 at 17:27
  • @Krutika are you read tutorial that link you posted?, System Tray is only Icon with implemented funcionalities for JPopupMenu, check http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/systemtray/ and http://download.oracle.com/javase/6/docs/api/java/awt/TrayIcon.html – mKorbel Jul 05 '11 at 17:33
  • @Krutika http://download.oracle.com/javase/tutorial/uiswing/examples/misc/TrayIconDemoProject/src/misc/TrayIconDemo.java – mKorbel Jul 05 '11 at 17:37
  • @mKorble:Let me be specific..In your first link trayIcon = new TrayIcon(image, "Tray Demo", popup); is used to add popup menu to the tray icon.Here is the limitation.JPopupMenu can not be used with this... – Krutika Jul 05 '11 at 17:50
  • @Krutika http://www.java2s.com/Code/Java/JDK-6/AddMousemotionlistenertoTrayIcon.htm – mKorbel Jul 05 '11 at 17:55