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