Changing the font of a JMenuItem

You can either embed HTML in the JMenuItem’s text, or you can call the method setFont.

Main.java:

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
   
public class Main extends JFrame {
   public Main() {
      getContentPane().setLayout(new FlowLayout());
 
      JMenuBar menuBar = new JMenuBar();
      JMenu menu = new JMenu("Sex");
      JMenuItem menuItem1 = new JMenuItem("<html><font size=-1>male</font></html>");
      JMenuItem menuItem2 = new JMenuItem("<html><font size=+1>female</font></html>");
      JMenuItem menuItem3 = new JMenuItem("androgyne");
      menuItem3.setFont(new Font("Monospaced", Font.ITALIC, 36));
 
      // add the MenuItems to the Menu 
      menu.add(menuItem1);
      menu.add(menuItem2);
      menu.add(menuItem3);
 
      menuBar.add(menu);
 
      this.setJMenuBar(menuBar); 
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      setSize(400, 400);
   }
 
   public static void main(String[] args) {
      (new Main()).show();
   }
}

Displaying the description of a JMenuItem in the statusbar when moving over it

Add a ChangeListener. Here’s an example:

Main.java:

import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
   
public class Main extends JFrame {
   public Main() {
      getContentPane().setLayout(new BorderLayout());
 
      JMenuBar menuBar = new JMenuBar();
      JMenu menu = new JMenu("Sex");
      JMenuItem menuItem1 = new JMenuItem("male");
      JMenuItem menuItem2 = new JMenuItem("female");
      JMenuItem menuItem3 = new JMenuItem("androgyne");
 
      // add the MenuItems to the Menu 
      menu.add(menuItem1);
      menu.add(menuItem2);
      menu.add(menuItem3);
 
      StatusBar statusBar = new StatusBar();
 
      new JMenuItemHelpText(menuItem1, "The weak sex", statusBar);
      new JMenuItemHelpText(menuItem2, "The strong sex", statusBar);
      new JMenuItemHelpText(menuItem3, "The average sex, I guess", statusBar);
 
      menuBar.add(menu);
 
      this.setJMenuBar(menuBar); 
      getContentPane().add(BorderLayout.SOUTH, statusBar);
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      setSize(400, 400);
   }
 
   public static void main(String[] args) {
      (new Main()).show();
   }
}
 
class JMenuItemHelpText implements ChangeListener
{
   JMenuItem menuItem;
   String helpText;
   StatusBar statusBar;
 
   JMenuItemHelpText(JMenuItem menuItem, String helpText, StatusBar statusBar) {
      this.menuItem = menuItem;
      this.helpText = helpText;
      this.statusBar = statusBar;
      menuItem.addChangeListener(this);
   } 
 
   public void stateChanged(ChangeEvent evt)
   {
      if (menuItem.isArmed())
         statusBar.setStatus(helpText);
      else
         statusBar.setStatus("");
   }
}
 
class StatusBar extends JPanel
{
   private JLabel statusLabel;
 
   public StatusBar() {
      setLayout(new BorderLayout(2, 2));
      statusLabel = new JLabel("Ready");
      statusLabel.setBorder(BorderFactory.createLoweredBevelBorder());
      statusLabel.setForeground(Color.black);
 
      add(BorderLayout.CENTER, statusLabel);
       
      JLabel dummyLabel = new JLabel("       ");
      dummyLabel.setBorder(BorderFactory.createLoweredBevelBorder());
 
      add(BorderLayout.EAST, dummyLabel);
   }
 
   public void setStatus(String status) {
      if (status.equals("")) 
         statusLabel.setText("Ready");
      else
         statusLabel.setText(status);
   }
 
   public String getStatus() {
      return statusLabel.getText();
   }
}

Justifying the text on a JMenuItem

You have a number of functions:setHorizontalAlignment, setHorizontalTextPosition, setVerticalAlignment, setVerticalTextPosition. Look at the API docs for more information about their behaviour.

Main.java:

import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
   
public class Main extends JFrame {
   public Main() {
      getContentPane().setLayout(new FlowLayout());
 
      JMenuBar menuBar = new JMenuBar();
      JMenu menu = new JMenu("Sex");
      JMenuItem menuItem0 = new JMenuItem("NO ALIGNMENT");
      JMenuItem menuItem1 = new JMenuItem("CENTER");
      JMenuItem menuItem2 = new JMenuItem("LEFT");
      JMenuItem menuItem3 = new JMenuItem("RIGHT");
      JMenuItem menuItem4 = new JMenuItem("LEADING");
      JMenuItem menuItem5 = new JMenuItem("TRAILING");
 
      // add the MenuItems to the Menu 
      menu.add(menuItem0);
      menu.add(menuItem1);
      menu.add(menuItem2);
      menu.add(menuItem3);
      menu.add(menuItem4);
      menu.add(menuItem5);
   
      menuItem1.setHorizontalAlignment(SwingConstants.CENTER);
      menuItem2.setHorizontalAlignment(SwingConstants.LEFT);
      menuItem3.setHorizontalAlignment(SwingConstants.RIGHT);
      menuItem4.setHorizontalAlignment(SwingConstants.LEADING);
      menuItem5.setHorizontalAlignment(SwingConstants.TRAILING);
 
      menuBar.add(menu);
 
      this.setJMenuBar(menuBar); 
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      setSize(400, 400);
   }
 
   public static void main(String[] args) {
      (new Main()).show();
   }
}

Dynamically removing a separator from a JMenu

Look in the JMenu API for the remove methods!

Main.java:

import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
   
public class Main extends JFrame {
   public Main() {
      getContentPane().setLayout(new FlowLayout());
 
      JMenuBar menuBar = new JMenuBar();
      // used in inner class, so final
      final JMenu menu = new JMenu("Sex");
      JMenuItem menuItem0 = new JMenuItem("female");
      JMenuItem menuItem1 = new JMenuItem("male");
      JMenuItem menuItem2 = new JMenuItem("androgyn");
 
      // add the MenuItems to the Menu 
      menu.add(menuItem0);
      menu.add(menuItem1);
 
      // used in inner class, so final
      final JSeparator jsep = new JSeparator();
      menu.add(jsep);
      menu.add(menuItem2);
   
      menuBar.add(menu);
 
      this.setJMenuBar(menuBar); 
      final JButton button = new JButton("remove menu separator");
      button.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            menu.remove(jsep);
            button.setEnabled(false);
         }
      });
      getContentPane().add(button);
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      setSize(400, 400);
   }
 
   public static void main(String[] args) {
      (new Main()).show();
   }
}

Aligning a ‘Help’ JMenu component to the right side of the JMenuBar

JMenuBar has a method called setHelpMenu but you’ll get a runtime error when you try to use it. A workaround is to use Box.createHorizontalGlue to create the necessary empty space between the last left-aligned menu and the help menu. For more information about BoxLayout and its static methods, check out the link below.

Main.java:

import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*; 
 
public class Main extends JFrame {
   public Main() {
      JMenu fileMenu = new JMenu("File");
      fileMenu.add(new JMenuItem("Open"));
      fileMenu.add(new JMenuItem("Save"));
 
      JMenu helpMenu = new JMenu("Help");
      helpMenu.add(new JMenuItem("Help Topics"));
      helpMenu.add(new JSeparator());
      helpMenu.add(new JMenuItem("About"));
 
      JMenuBar mb = new JMenuBar();
      mb.add(fileMenu);
      // following line of code gives a runtime exception:
      //        "setHelpMenu() not yet implemented"
      //mb.setHelpMenu(helpMenu);
 
      // workaround:
      mb.add(Box.createHorizontalGlue());
      mb.add(helpMenu);
      
      this.setJMenuBar(mb);
  
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(0);
         }
      });
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.setSize(400, 400);
      main.setVisible(true);
   }
} 

Changing the colors of my JMenu and JMenuItems

Use setForeground and setBackground. You can change the default selection colors using the UIManager.

Main.java:

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
    
public class Main extends JFrame {
   public Main() {
      getContentPane().setLayout(new FlowLayout());
  
      // change default selection colors
      UIManager.put("Menu.selectionBackground", 
             new javax.swing.plaf.ColorUIResource(Color.blue));
      UIManager.put("MenuItem.selectionBackground", 
             new javax.swing.plaf.ColorUIResource(Color.green));
 
      JMenuBar menuBar = new JMenuBar();
      JMenu menu = new JMenu("Sex");
      JMenuItem menuItem1 = new JMenuItem("male");
      JMenuItem menuItem2 = new JMenuItem("female");
      JMenuItem menuItem3 = new JMenuItem("androgyne");
 
      menuItem1.setBackground(Color.red);
      menuItem1.setForeground(Color.yellow);
      menuItem2.setBackground(Color.red);
      menuItem2.setForeground(Color.yellow);
      menuItem3.setBackground(Color.red);
      menuItem3.setForeground(Color.yellow);
  
      menu.setBackground(Color.white);
      menu.setForeground(Color.blue);
 
      // add the MenuItems to the Menu 
      menu.add(menuItem1);
      menu.add(menuItem2);
      menu.add(menuItem3);
  
      menuBar.add(menu);
  
      this.setJMenuBar(menuBar); 
  
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
  
      setSize(400, 400);
   }
  
   public static void main(String[] args) {
      (new Main()).show();
   }
}

Changing the orientation of a JSlider

You can either specify the orientation when you create your JSlider object or afterwards with the method setOrientation.

Main.java:

import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
   
public class Main extends JFrame implements ActionListener {
   private JSlider slider;
 
   public Main() {
      getContentPane().setLayout(new FlowLayout());
 
      JMenuBar menuBar = new JMenuBar();
      JMenu menu = new JMenu("JSlider Orientation");
      JMenuItem menuItem1 = new JMenuItem("HORIZONTAL");
      JMenuItem menuItem2 = new JMenuItem("VERTICAL");
      menu.add(menuItem1);
      menu.add(menuItem2);
      menuItem1.addActionListener(this);
      menuItem2.addActionListener(this);
      menuBar.add(menu); 
      setJMenuBar(menuBar);
      
      slider = new JSlider(JSlider.HORIZONTAL, 0, 30, 15);
 
      getContentPane().add(slider);

      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      setSize(400, 400);
   }
 
   public static void main(String[] args) {
      (new Main()).show();
   }
 
   public void actionPerformed(ActionEvent ae) {
      if (ae.getActionCommand().equals("HORIZONTAL"))
         slider.setOrientation(JSlider.HORIZONTAL);
      else if (ae.getActionCommand().equals("VERTICAL"))
         slider.setOrientation(JSlider.VERTICAL);
   }
}

Invoking a JPopupMenu from a JComboBox

Add a MouseListener:

import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
    
public class Main extends JFrame
{ 
   public Main() {
      getContentPane().setLayout(new FlowLayout());
      
      final JComboBox combobox = new JComboBox(new String[] { "Item 1", "Item 2", "Item 3" });
      final JPopupMenu popup = new JPopupMenu();
      JMenuItem mi1 = new JMenuItem("Popup Item 1");
      JMenuItem mi2 = new JMenuItem("Popup Item 2");
      popup.add(mi1);
      popup.add(mi2);
 
      ((JButton) combobox.getComponent(0)).addMouseListener(new MouseAdapter() {
         public void mousePressed(MouseEvent me) {
            if (me.isPopupTrigger()) {
               popup.show(combobox, me.getX(), me.getY());
            }
         }
 
         public void mouseReleased(MouseEvent me) {
            if (me.isPopupTrigger()) {
               popup.show(combobox, me.getX(), me.getY());
            }
         }
      });
 
      getContentPane().add(combobox);
  
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(1);
         }
      });      
   
      setSize(new Dimension(200, 200));
   } 
 
   public static void main(String[] args) throws Exception {
      Main main = new Main();
      main.setVisible(true);
   }
}

Displaying a menu when right clicking on a cell

Try this:

import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
 
public class Main extends JFrame 
{
   JTable table;
   TablePopup tablePopup;
 
   public Main() {
      super("Table example, Wines from Bordeaux");
 
      Object[][] tabledata = {
            { "Chateau Meyney, St. Estephe", 	   new Integer(1994), "$18.75"},
            { "Chateau Montrose, St. Estephe", 	   new Integer(1975), "$54.25" },
            { "Chateau Gloria, St. Julien", 	   new Integer(1993), "$22.99" },
            { "Chateau Beychevelle, St. Julien",   new Integer(1970), "$61.63" },
            { "Chateau La Tour de Mons, Margeaux", new Integer(1975), "$57.03" },
            { "Chateau Brane-Cantenac, Margeaux",  new Integer(1978), "$49.92" },
      };
 
      String columnheaders[] = { "Wine", "Vintage", "Price" };
 
      table = new JTable(tabledata, columnheaders);
      table.setPreferredScrollableViewportSize(new Dimension(500, 70));
      JScrollPane scrollPane = new JScrollPane(table); 
      tablePopup = new TablePopup(table);
 
      table.addMouseListener(new MouseAdapter() {
            public void mouseReleased (MouseEvent e) {
                if (e.isPopupTrigger()) {
                   tablePopup.show (e.getComponent(), e.getX(), e.getY());
                }
            }
      });
  
      getContentPane().add(scrollPane);
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(0);
         }
      });
 
      pack();
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.show();
   }
 
   class TablePopup extends JPopupMenu {
      public TablePopup(JTable table) {
         JMenuItem itemDelete = new JMenuItem("Delete row");
         JMenuItem itemAdd = new JMenuItem("Add row");
         itemDelete.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
               System.out.println("Delete row");
            }
         });
         itemAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
               System.out.println("Add row");
            }
         });
 
         add(itemDelete);
         add(new JSeparator());
         add(itemAdd);
      }
   }
}