Results 1 to 3 of 3
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,064
    Rep Power
    8

    Default disappearing menu items

    This class is part of a text writer application which I am writing as an excersize. The class sets up the menu for the font family (to choose the font type). A helper class sets all the system fonts (not shown here).

    There are three menu-radio-button items:
    1) select all fonts (more than 200!)
    2) select a short list
    3) show the short list (after been slected)

    At the fourth menu position I inserted another JMenu which shows a list of font names, corresponding to the previous radio button selection, when:
    1) shows all the fonts (JMenu allFontMenu) again as radio button item (to make one selection only;
    2) now the short list will appear, that is first empty (JMenu shortListFontMenu)
    3) again all the fonts appear but now as check boxes so one can select the shore list to appear on 2 (JMenu selectFontMenu)

    The method buildAllList builds the allFontMenu ones and its items are: radiob[] (an array of radio button items)
    The method buildSelectList does the same (built only once) with checkb[] check box items.
    And the makeShortFontlist builds a flexible menu (shortListFontMenu) with the radiob[]

    The funny thing is when I add the radio button menu items to the shortlist, than they disappear from the all list!!!

    BTW I use the indexes and the references to have the same font selected when moving from the All-list to the short-list.

    here is the code:

    Java Code:
    package willemwriter;
    
    import java.awt.Component;
    import java.awt.Font;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import javax.swing.AbstractAction;
    import javax.swing.ButtonGroup;
    import javax.swing.JComboBox;
    import javax.swing.JList;
    import javax.swing.JMenu;
    import javax.swing.JRadioButtonMenuItem;
    import javax.swing.JTextArea;
    import javax.swing.DefaultListCellRenderer;
    import javax.swing.JCheckBoxMenuItem;
    import javax.swing.JLabel;
    import javax.swing.JMenuItem;
    import javax.swing.JPopupMenu;
    import javax.swing.JScrollPane;
    import javax.swing.JViewport;
    import javax.swing.event.MenuEvent;
    import javax.swing.event.MenuListener;
    import javax.swing.plaf.basic.BasicComboBoxRenderer;
    
    /**
     *
     * @author willem
     */
     class MenuFontMenu  {
    
         protected DefaultListCellRenderer defaultRenderer;
         private MenuActionListener menulistener;
         private MenuListener fontlistener;
         private Fontclass fontclass;
         private String[] fontNameList, fontStyleList, fontSizeList;
         private Integer[] fontSizeList2;     
         
         private JMenu fontMenu, allFontMenu, selectFontMenu, shortListFontMenu;
         
         private Font font;
         private IconImport IconImport;
         private JTextArea textarea;
         private JRadioButtonMenuItem rbShortList, rbSetup, rbAllfonts;
         private JCheckBoxMenuItem[] checkb;
         private JRadioButtonMenuItem[] radiob;
         private int familyStyle, familySize;
         private String familyName, currentFont;
    
        
    
         public MenuFontMenu(TextCanvas textcanvas) {
             // thew text area reference
             this.textarea = textcanvas.textarea;
             IconImport = new IconImport("/images");
    
             // Get the system fonts
             fontclass = new Fontclass();
             fontNameList = fontclass.getAllFontNames();
        
             defaultRenderer = new DefaultListCellRenderer();
             menulistener = new MenuActionListener();
             fontlistener = new MenuFontListener();
            
         }
    
         public JMenu buildFontMenu() {
            // Action for "Fonts" menu item
            AbstractAction allFontsAction = new AbstractAction() {
                {	// Constructor
                    putValue(NAME, "All Fonts");
                    putValue(SMALL_ICON, IconImport.getImageIcon("whatever"));
                    putValue(MNEMONIC_KEY, new Integer('N'));
                    putValue(SHORT_DESCRIPTION, "Creates a whatever");
                    putValue(LONG_DESCRIPTION, "Creating new whatever...");
                }
                public void actionPerformed(ActionEvent evt) {
                    
                    fontMenu.remove(4);
                    fontMenu.add(allFontMenu);
                    System.out.println("itemcount: " + allFontMenu.getItemCount());
                    for (int i= 0; i < fontNameList.length; i++) {
                        if (fontNameList[i].equals(currentFont)) {
                            radiob[i].setSelected(true); // remembering selection
                        }
                    }
                }
            };
    
            AbstractAction shortListFontsAction = new AbstractAction() {
                {	
                    putValue(NAME, "Short Fontlist");
                    putValue(SMALL_ICON, IconImport.getImageIcon("whatever"));
                    putValue(MNEMONIC_KEY, new Integer('N'));
                    putValue(SHORT_DESCRIPTION, "Creates whatever");
                    putValue(LONG_DESCRIPTION, "Creating whatever...");
                }
                public void actionPerformed(ActionEvent evt) { 
                    fontMenu.remove(4);
                    fontMenu.add(shortListFontMenu);
                    for (int i= 0; i < fontNameList.length; i++) {
                        if (fontNameList[i].equals(currentFont)) {
                            radiob[i].setSelected(true); // remembering selection
                        }
                    }
                }
            };
    
            AbstractAction setupFontsAction = new AbstractAction() {
                {	// Constructor
                    putValue(NAME, "Setup Shortlist");
                    putValue(SMALL_ICON, IconImport.getImageIcon("whatever"));
                    putValue(MNEMONIC_KEY, new Integer('N'));
                    putValue(SHORT_DESCRIPTION, "Creates a new whatever");
                    putValue(LONG_DESCRIPTION, "Creating new whatever...");
                }
                public void actionPerformed(ActionEvent evt) {
                    fontMenu.remove(4);
                    fontMenu.add(selectFontMenu);
                       
                } 
            };      
    
            // the several menus
            fontMenu = new JMenu("Fonts");
            ButtonGroup group = new ButtonGroup();
    
            // first the three fixed radio buttons
            rbAllfonts = new JRadioButtonMenuItem(allFontsAction);
            rbAllfonts.setSelected(true);
            fontMenu.add(rbAllfonts);
            group.add(rbAllfonts);
    
            rbShortList = new JRadioButtonMenuItem(shortListFontsAction);
            rbShortList.setSelected(false);
            rbShortList.setEnabled(true);
            fontMenu.add(rbShortList);
            group.add(rbShortList);
    
            rbSetup = new JRadioButtonMenuItem(setupFontsAction);
            rbSetup.setSelected(false);
            fontMenu.add(rbSetup);
            group.add(rbSetup);
            fontMenu.addSeparator();
    
            // a second menu added to show the font listings
            allFontMenu = new JMenu("Font List");
            selectFontMenu = new JMenu("Font List");
            selectFontMenu.addMenuListener(fontlistener); // not used here
            shortListFontMenu = new JMenu("Font List");
    
            buildAllList(fontNameList);
            buildSelectList(fontNameList);
            JMenuItem dummyItem = new  JMenuItem("Add first slections");
            shortListFontMenu.add(dummyItem);
            fontMenu.add(allFontMenu);
    
            MenuScroller.setScrollerFor(allFontMenu);
            MenuScroller.setScrollerFor(selectFontMenu);
    
            return fontMenu; // send to the frame class
         }
    
         public void buildAllList(String[] names)  {
            radiob = new JRadioButtonMenuItem[names.length];
            ButtonGroup fontgroup = new ButtonGroup();
            Font ft = fontMenu.getFont();
            Font font;
            for (int i = 0; i < names.length; i++) {
                font = new Font(names[i], Font.PLAIN, 16);
                radiob[i] = new JRadioButtonMenuItem(names[i]);
                radiob[i].setActionCommand(names[i]);
                radiob[i].setFont(font);
                fontgroup.add(radiob[i]);
                radiob[i].addActionListener(menulistener);
                allFontMenu.add(radiob[i]);
            }
            allFontMenu.setFont(ft);
         }
    
           public void buildSelectList(String[] names)  {
             checkb = new JCheckBoxMenuItem[names.length];
             Font ft = fontMenu.getFont();
             Font font;
             for (int i = 0; i < names.length; i++) {
                font = new Font(names[i], Font.PLAIN, 16);
                checkb[i] = new JCheckBoxMenuItem(names[i]);
                checkb[i].setActionCommand(names[i]);
                checkb[i].setFont(font);
                checkb[i].addActionListener(menulistener);
                selectFontMenu.add(checkb[i]);
             }
             selectFontMenu.setFont(ft);
         }
    
         public void makeShortFontlist(ActionEvent evt)  {
            String cmd = evt.getActionCommand();
            ButtonGroup fontgroup = new ButtonGroup();
            for (int i = 0; i < fontNameList.length; i++) {
                if  (cmd.equals(checkb[i].getActionCommand())) {
                    if (checkb[i].isSelected()) {
                        shortListFontMenu.add(radiob[i]);
                        fontgroup.add(radiob[i]);
                      
                    } else {
                        shortListFontMenu.remove(radiob[i]);
                        fontgroup.remove(radiob[i]);
                    }
                 }
            }
         }
    
         class MenuActionListener implements ActionListener{
            public void actionPerformed(ActionEvent evt) {
                Object menusource = evt.getSource();
                String cmd = evt.getActionCommand();
    
                if (rbSetup.isSelected()){
                    makeShortFontlist(evt);
                } else {
                    currentFont = cmd;
                    System.out.println("\naction: " + currentFont);
                }
            }
         }
    
    
         public class MenuFontListener implements MenuListener { // not used
    	public void menuCanceled(MenuEvent e)  {
    
            }
    	public void menuDeselected(MenuEvent e) {
               
            }
    	public void menuSelected(MenuEvent e) {
    
            }
         }
    }
    The font names I get like this (from the helper class)

    Java Code:
    public String[] getAllFontNames() {
            GraphicsEnvironment e =
                                GraphicsEnvironment.getLocalGraphicsEnvironment();
            String[] fontNameList = e.getAvailableFontFamilyNames();
            return fontNameList;
        }
    Last edited by willemjav; 01-17-2014 at 09:47 PM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: disappearing menu items

    The funny thing is when I add the radio button menu items to the shortlist, than they disappear from the all list!!!
    Swing components can only have a single parent. You need to create two separate radio button menu items. But you can share the Actions between each item.

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,064
    Rep Power
    8

    Default Re: disappearing menu items

    Yes I found that out the hard way. There are many of these "small" issues you probably learn in school.
    I might have read about this but did not remember.
    Anyway I found a solution to it, thanks!

Similar Threads

  1. JPopupMenu, adding menu items.
    By Scyth3 in forum New To Java
    Replies: 1
    Last Post: 01-12-2013, 03:46 AM
  2. override the menu items
    By trishtren in forum AWT / Swing
    Replies: 0
    Last Post: 05-19-2011, 11:46 PM
  3. ActionListener for Menu Items
    By CuddlyKittens11 in forum AWT / Swing
    Replies: 7
    Last Post: 04-22-2011, 06:10 AM
  4. folder/file menu items
    By jhughes in forum Advanced Java
    Replies: 2
    Last Post: 07-21-2009, 10:57 PM
  5. hyperlinks from menu items?
    By asterope118 in forum New To Java
    Replies: 1
    Last Post: 07-16-2007, 01:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •