Swing - last JMenuItem occupy the rest of space on JMenuBar - java

I find that if I have a JMenuBar, and the last element of it (the rightmost one) is a JMenuItem, it will occupy all the rest blank space on the JMenuBar, which is definitely not what we want.
Imagine an "About" JMenuItem as the rightmost item on a JMenuBar. It should only occupy the same space as the other menus.
See my SSCCE: (click the menu and hover over the menuitem on the right to see the effect)
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
public class JMenuItemLastOnMenuBar {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createGUI();
}
});
}
private static void createGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(0, 0, 300, 300);
JMenuBar bar = new JMenuBar();
JMenu menu = new JMenu("Menu 1");
JMenuItem item1 = new JMenuItem("Item 1");
menu.add(item1);
bar.add(menu);
JMenuItem item2 = new JMenuItem("Item 2") {
#Override
public Dimension getPreferredSize() {
return new Dimension(120, 25);
}
};
bar.add(item2);
frame.setJMenuBar(bar);
frame.pack();
frame.setVisible(true);
}
}

You should override the method getMaximumSize
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
public class JMenuItemLastOnMenuBar {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createGUI();
}
});
}
private static void createGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(0, 0, 300, 300);
JMenuBar bar = new JMenuBar();
JMenu menu = new JMenu("Menu 1");
JMenuItem item1 = new JMenuItem("Item 1");
menu.add(item1);
bar.add(menu);
JMenuItem item2 = new JMenuItem("Item 2") {
#Override
public Dimension getMaximumSize() {
Dimension d1 = super.getPreferredSize();
Dimension d2 = super.getMaximumSize();
d2.width = d1.width;
return d2;
}
};
bar.add(item2);
frame.setJMenuBar(bar);
frame.pack();
frame.setVisible(true);
}
}

Related

Programmatically expand sub JMenuItems

I would like to programmatically expand a particular JMenuItem in a JPopup. For example in the code below
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
public class AutoExpandSubMenusDemo extends JFrame {
private final JPopupMenu popup = new JPopupMenu("Popup");
public AutoExpandSubMenusDemo() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
JMenu menuB = new JMenu("B");
menuB.add(new JMenuItem("X"));
JMenuItem menuY = menuB.add(new JMenuItem("Y"));
menuB.add(new JMenuItem("Z"));
popup.add(new JMenuItem("A"));
popup.add(menuB);
popup.add(new JMenuItem("C"));
final JButton button = new JButton("Show Popup Menu");
button.addActionListener(new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
popup.show(button, 0, button.getHeight());
// Show menuY
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(button);
getContentPane().add(buttonPanel);
}
public static void main(String[] args) {
AutoExpandSubMenusDemo f = new AutoExpandSubMenusDemo();
f.setSize(500, 300);
f.setVisible(true);
}
}
I would like to expand the popup menu so that the items B(menuB)/Y(menuY) are expanded and selected when the button is pressed.
Sorry if this is something that's easy to do but I've searched around and can't figure it out.
I did find the
MenuSelectionManager.defaultManager().setSelectedPath(...)
however this didn't work when I tried it and the javadoc specifies that it is called from the LaF and should not be called by clients.
Any help is much appreciated.
While I don't recommend doing this, since the documentation itself advises against it, here's how you could do it:
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.MenuElement;
import javax.swing.MenuSelectionManager;
import javax.swing.SwingUtilities;
public class AutoExpandSubMenusDemo extends JFrame {
private final JPopupMenu popup = new JPopupMenu("Popup");
public AutoExpandSubMenusDemo() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
final JMenu menuB = new JMenu("B");
menuB.add(new JMenuItem("X"));
final JMenuItem menuY = menuB.add(new JMenuItem("Y"));
menuB.add(new JMenuItem("Z"));
popup.add(new JMenuItem("A"));
popup.add(menuB);
popup.add(new JMenuItem("C"));
final JButton button = new JButton("Show Popup Menu");
button.addActionListener(new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
popup.show(button, 0, button.getHeight());
SwingUtilities.invokeLater(new Runnable() {
public void run() {
menuB.setPopupMenuVisible(true);
MenuSelectionManager.defaultManager().setSelectedPath(new MenuElement[]{popup, menuB, menuY});
}
});
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(button);
getContentPane().add(buttonPanel);
}
public static void main(String[] args) {
AutoExpandSubMenusDemo f = new AutoExpandSubMenusDemo();
f.setSize(500, 300);
f.setVisible(true);
}
}
Most of this code is yours. I only added:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
menuB.setPopupMenuVisible(true);
MenuSelectionManager.defaultManager().setSelectedPath(new MenuElement[]{popup, menuB, menuY});
}
});
which seems to work.
You could avoid abusing MenuSelectionManager via 'MenuItem.setArmed(boolean)'.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
menuB.setPopupMenuVisible(true);
menuB.setArmed(true);
menuY.setArmed(true);
}
});
The popup staying visible after selecting another menu item or dismissing the JPopupMenu still needs to be addressed though.
Another way is to fake a mouse event... :D
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MouseEvent event = new MouseEvent(
menuB, MouseEvent.MOUSE_ENTERED, 0, 0, 0, 0, 0, false);
menuB.dispatchEvent(event);
menuY.setArmed(true);
}
});
This way it is as if the user actually used the mouse.
Another example:
MenuSelectionManager.defaultManager().setSelectedPath(
new MenuElement[] {popup, menuB, menuB.getPopupMenu()});
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AutoExpandSubMenusDemo2 extends JFrame {
private final JPopupMenu popup = new JPopupMenu("Popup");
public AutoExpandSubMenusDemo2() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
final JMenu menuB = new JMenu("B");
menuB.add(new JMenuItem("X"));
final JMenuItem menuY = menuB.add(new JMenuItem("Y"));
menuB.add(new JMenuItem("Z"));
popup.add(new JMenuItem("A"));
popup.add(menuB);
popup.add(new JMenuItem("C"));
JPanel buttonPanel = new JPanel();
buttonPanel.add(new JButton(new AbstractAction("Show menuB Popup") {
#Override public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
popup.show(button, 0, button.getHeight());
//[Bug ID: JDK-6949414 JMenu.buildMenuElementArray() endless loop]
//( http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6949414 )
//menuB.doClick();
MenuSelectionManager.defaultManager().setSelectedPath(
new MenuElement[] {popup, menuB, menuB.getPopupMenu()});
}
}));
buttonPanel.add(new JButton(new AbstractAction("Select menuY") {
#Override public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
popup.show(button, 0, button.getHeight());
MenuSelectionManager.defaultManager().setSelectedPath(
new MenuElement[] {popup, menuB, menuB.getPopupMenu(), menuY});
}
}));
getContentPane().add(buttonPanel);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new AutoExpandSubMenusDemo2();
f.setSize(500, 300);
f.setVisible(true);
}
}

Swing; How would I position my components in this format?

So, I would like my items to be positioned in the format below; I'm really not confident with positioning but would like to learn it a bit more. Here is the code as of yet:
package com.bleh.harry;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main
{
private JMenuBar menuBar;
private JMenu fileMenu, windowMenu, helpMenu;
private JMenuItem fileNew, fileOpen, fileSave, windowTheme, windowLayout, windowProperties, helpWelcome, helpHelp, helpAbout;
private JTextArea mainTextArea;
public
Main()
{
JPanel mainCard = new JPanel(new BorderLayout(8,8));
JPanel mainTop = new JPanel(new FlowLayout(FlowLayout.CENTER));
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
windowMenu = new JMenu("Window");
helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(windowMenu);
menuBar.add(helpMenu);
final CardLayout layout = new CardLayout(); //ADDS CARDS TO CONTAINER
final JPanel cards = new JPanel(layout);
cards.add(mainCard, "2");
mainCard.add(mainTop, BorderLayout.NORTH);
JFrame window = new JFrame("Pseudo code text editor");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(cards);
window.setSize(1280, 720);
window.setLocationRelativeTo(null);
window.setVisible(true);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Main();
}
});
}
}
You could use a BorderLayout...
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class LayoutTest {
private JMenuBar menuBar;
private JMenu fileMenu, windowMenu, helpMenu;
private JMenuItem fileNew, fileOpen, fileSave, windowTheme, windowLayout, windowProperties, helpWelcome, helpHelp, helpAbout;
private JTextArea mainTextArea;
public LayoutTest() {
JPanel mainCard = new JPanel(new BorderLayout(8, 8));
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
windowMenu = new JMenu("Window");
helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(windowMenu);
menuBar.add(helpMenu);
final CardLayout layout = new CardLayout();
final JPanel cards = new JPanel(layout);
cards.add(mainCard, "2");
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add("One", createPane());
tabbedPane.add("Two", createPane());
tabbedPane.add("Three", createPane());
tabbedPane.add("Four", createPane());
mainTextArea = new JTextArea(20, 40);
mainCard.add(tabbedPane, BorderLayout.WEST);
mainCard.add(new JScrollPane(mainTextArea));
JFrame window = new JFrame("Pseudo code text editor");
window.setJMenuBar(menuBar);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(cards);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
protected JPanel createPane() {
return new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
};
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new LayoutTest();
}
});
}
}
The problem here, is the amount of space that the JTabbedPane wants is depended on it's content...
You could even try using a GridBagLayout which might give you a little more control...
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class LayoutTest {
private JMenuBar menuBar;
private JMenu fileMenu, windowMenu, helpMenu;
private JMenuItem fileNew, fileOpen, fileSave, windowTheme, windowLayout, windowProperties, helpWelcome, helpHelp, helpAbout;
private JTextArea mainTextArea;
public LayoutTest() {
JPanel mainCard = new JPanel(new GridBagLayout());
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
windowMenu = new JMenu("Window");
helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(windowMenu);
menuBar.add(helpMenu);
final CardLayout layout = new CardLayout(); //ADDS CARDS TO CONTAINER
final JPanel cards = new JPanel(layout);
cards.add(mainCard, "2");
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add("One", createPane());
tabbedPane.add("Two", createPane());
tabbedPane.add("Three", createPane());
tabbedPane.add("Four", createPane());
mainTextArea = new JTextArea(20, 40);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 0.25;
gbc.weighty = 1;
mainCard.add(tabbedPane, gbc);
gbc.weightx = 0.75;
mainCard.add(new JScrollPane(mainTextArea), gbc);
JFrame window = new JFrame("Pseudo code text editor");
window.setJMenuBar(menuBar);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(cards);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
protected JPanel createPane() {
return new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
};
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new LayoutTest();
}
});
}
}
Just remember, the JMenuBar belongs to the window ;)
To lay components out next to each other, I'd recommend using a BoxLayout. A BoxLayout takes an orientation parameter and lays components out accordingly. The two most used options are X_AXIS and Y_AXIS. X_AXIS lays things out left to right while Y_AXIS lays things out top to bottom. You want X_AXIS.
To set the layout with a BoxLayout using an instance of JFrame named window, do:
window.setLayout(new BoxLayout(window, BoxLayout.X_AXIS));

How to change icon for JmenuItem on rollover

I want to put a rollover effect in my JMenu this is my code:
Icon firstPicAcc= new ImageIcon(Welcome.class.getResource("/app/resources/user1.jpg"));
Icon secPicAcc= new ImageIcon(Welcome.class.getResource("/app/resources/user2.jpg"));
JMenu mnAccountSettings = new JMenu("Account Settings");
mnAccountSettings.addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent arg0) {
}
});
mnAccountSettings.setFont(new Font("Dialog", Font.PLAIN, 20));
mnAccountSettings.setForeground(new Color(0, 153, 0));
mnAccountSettings.setBackground(new Color(255, 204, 255));
mnAccountSettings.setRolloverEnabled(true);
mnAccountSettings.setIcon(firstPicAcc);
mnAccountSettings.setRolloverIcon(secPicAcc);
mnAccount.add(mnAccountSettings);
how can i do that? thanks!
What should happen is when I rolled my mouse over the JMenu bar the original icon should change into another icon.
What you want to do is add a ChangeListener to the JMenuItem and check if it's selected or armed and change the icon accordingly. The ChangeListener works for both keyboard and mouse.
See this good read by #kleopatra
private JMenuItem createMenuItem(final ImageIcon icon, String title) {
JMenuItem item = new JMenuItem(title);
item.setIcon(icon);
ChangeListener cl = new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
if (e.getSource() instanceof JMenuItem) {
JMenuItem item = (JMenuItem) e.getSource();
if (item.isSelected() || item.isArmed()) {
item.setIcon(stackIcon);
} else {
item.setIcon(icon);
}
}
}
};
item.addChangeListener(cl);
return item;
}
Here is running example. Just replace images with yours
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class RolloverMenuItem {
ImageIcon stackIcon = new ImageIcon(getClass().getResource("/resources/stackoverflow2.png"));
public RolloverMenuItem() {
ImageIcon newIcon = new ImageIcon(getClass().getResource("/resources/image/new.gif"));
ImageIcon saveIcon = new ImageIcon(getClass().getResource("/resources/image/open.gif"));
ImageIcon openIcon = new ImageIcon(getClass().getResource("/resources/image/save.gif"));
JMenu menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_F);
JMenuItem item1 = createMenuItem(newIcon, "New");
JMenuItem item2 = createMenuItem(openIcon, "Open");
JMenuItem item3 = createMenuItem(saveIcon, "Save");
menu.add(item1);
menu.add(item2);
menu.add(item3);
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
JFrame frame = new JFrame("Rollover MenuItem");
frame.setJMenuBar(menuBar);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 250);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JMenuItem createMenuItem(final ImageIcon icon, String title) {
JMenuItem item = new JMenuItem(title);
item.setIcon(icon);
ChangeListener cl = new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
if (e.getSource() instanceof JMenuItem) {
JMenuItem item = (JMenuItem) e.getSource();
if (item.isSelected() || item.isArmed()) {
item.setIcon(stackIcon);
} else {
item.setIcon(icon);
}
}
}
};
item.addChangeListener(cl);
return item;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new RolloverMenuItem();
}
});
}
}

Invoking a MenuItem with one keystroke

We know that we can invoke a menu item with the help of setaccelerator() method where a combination of two keystrokes are required. what if i want to invoke a menu item by just one keystroke...here is where i am having a bit problem
menuitem=new JMenuItem("Delete");
menuitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE);
menu.add(menuitem);
Please help....!!
Check How to Use Menus for details. Below is an example that utilizes Action, that defines accelerator. You can also set accelerator on the menu item, ie: item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));.
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class MenuDemo {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
final JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Menu");
menuBar.add(menu);
JMenuItem item = new JMenuItem(new TestAction(frame));
menu.add(item);
frame.setJMenuBar(menuBar);
frame.setSize(new Dimension(300, 300));
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
}
static class TestAction extends AbstractAction {
private Component parent;
public TestAction(Component parent) {
super("Test");
this.parent = parent;
putValue(Action.ACCELERATOR_KEY,
KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));
}
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(parent, "Test");
}
}
}

Having gradient background of system tray context menu

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..
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());
}
}

Categories

Resources