Results 1 to 18 of 18

Thread: scrolling menus

  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default scrolling menus

    Is there an easy way to make a long menu with lots of menu items
    scroll?
    Last edited by willemjav; 01-12-2014 at 09:54 PM.

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

    Default Re: scrolling menus

    You might be able to use Darryl's: Menu Scroller « Java Tips Weblog

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

    Default Re: scrolling menus

    Yes I saw that one, but there isn't an easy way like setScroll(true).....
    I am afraid not?

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: scrolling menus

    I used:
    GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();

    Java Code:
     public JMenu addFonts(JMenu fm) {
             Font[] fonts = e.getAllFonts();
             for (Font f : fonts) {
                JMenuItem jmi = new JMenuItem(f.getFontName());
                fm.add(jmi);
             }
             return fm;
    
         }
    Sure this list gives a doubling of all font sorts which I want to "filter out",
    so there is only one Time (not Times Plain, Times Bold etc.)
    Last edited by willemjav; 01-12-2014 at 10:53 PM.

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: scrolling menus

    This one gives "just" the font names, but still a (to) long list:

    Java Code:
    public JMenu addFonts(JMenu fm) {
             String[] fonts = e.getAvailableFontFamilyNames();
             for (int i = 0; i < fonts.length; i++) {
                JMenuItem jmi = new JMenuItem(fonts[i]);
                fm.add(jmi);
             }
             return fm;
    
         }

  6. #6
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: scrolling menus

    This looks nice but takes a few seconds to appear (any suggestions to improve speed):

    Java Code:
    public JMenu addFonts(JMenu fm) {
             String[] fonts = e.getAvailableFontFamilyNames();
             Font font;
             for (int i = 0; i < fonts.length; i++) {
                font = new Font(fonts[i], Font.PLAIN, 12);
                UIManager.put("MenuItem.font", font);
                JMenuItem jmi = new JMenuItem(fonts[i]);
                fm.add(jmi);
             }
             return fm;
    
         }

  7. #7
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: scrolling menus

    What I need to do is set the size (lenght) of the drop menu.
    So less items will appear faster.
    And add a scroll (how?) for the remaining items to select.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: scrolling menus

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: scrolling menus

    I am working on a text writer as a larger application by means of practicing.
    So there are various menus like File, Edit, Insert, Fonts, etc.
    It should be a menu though a list would be easier....

    Here is a small improvement of the previous code to prevent that all
    fonts of the menu appear in funny letter types!

    Java Code:
    public JMenu addFonts(JMenu fm) {
             String[] fonts = e.getAvailableFontFamilyNames();
             Font font;
             JMenu fontMenu = new JMenu();
             Font ft = fontMenu.getFont();
             for (int i = 0; i < fonts.length; i++) {
                font = new Font(fonts[i], Font.PLAIN, 12);
                UIManager.put("MenuItem.font", font);
                JMenuItem jmi = new JMenuItem(fonts[i]);
                fm.add(jmi);
             }
             UIManager.put("MenuItem.font", ft);
             return fm;
         }
    (Isn't it weird that Java has no standard function for menus to scroll when they get to large?)
    Last edited by willemjav; 01-12-2014 at 11:41 PM.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: scrolling menus

    Then why not use a JComboBox? They scroll. And they can also be added to the menu bar.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: scrolling menus

    Great, that's it, I was about to say: what a pity that you can not add JList to the menu bar.......

  12. #12
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: scrolling menus

    Still a problem, Jim, I am trying to put each ComboBox item (so to speak)
    in its on font type, but it does not let me (takes the las one and changes all equally).
    Trying out some stuff here, hahahaha:

    Java Code:
    public JMenu addFonts(JMenu fm) {
    
             String[] fonts = e.getAvailableFontFamilyNames();
             Font font;
             Object[] fontobject = new Object[fonts.length];
             JMenu fontMenu = new JMenu();
             Font ft = fontMenu.getFont();
             JComboBox jc = new JComboBox();
             for (int i = 0; i < fonts.length; i++) {
                font = new Font(fonts[i], Font.PLAIN, 12);
                fontobject[i] = font;
                jc.add((Component) fontobject[i], i);
             }
             
             fm.add(jc);
          
             return fm;
         }
    unfortunate there is no UIManager.put("ComboBoxItem.font", ft);
    right?

  13. #13
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: scrolling menus

    This brings me close but still something missing:

    Java Code:
     public JMenu addFonts(JMenu fm) {
    
             String[] fonts = e.getAvailableFontFamilyNames();
             Font font;
             Object[] fontobject = new Object[fonts.length];
             JMenu fontMenu = new JMenu();
             Font ft = fontMenu.getFont();
            
             for (int i = 0; i < fonts.length; i++) {
                font = new Font(fonts[i], Font.PLAIN, 12);
                fontobject[i] = font;
             
             }
              JComboBox jc = new JComboBox( fontobject);
             fm.add(jc);
          
             return fm;
         }
    need to "render" it?

  14. #14
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: scrolling menus

    Here is my concrete question, how to render the next listing so that just the font name appears in that current
    font style as a "item" of the combo box.

    java.awt.Font[family=(el&font BLOCK),name=(el&font BLOCK),style=plain,size=12]
    java.awt.Font[family=Abadi MT Condensed Extra Bold,name=Abadi MT Condensed Extra Bold,style=plain,size=12]
    java.awt.Font[family=Abadi MT Condensed Light,name=Abadi MT Condensed Light,style=plain,size=12]
    java.awt.Font[family=Adobe Arabic,name=Adobe Arabic,style=plain,size=12]
    java.awt.Font[family=Adobe Caslon Pro,name=Adobe Caslon Pro,style=plain,size=12]
    java.awt.Font[family=Adobe Fan Heiti Std,name=Adobe Fan Heiti Std,style=plain,size=12]
    java.awt.Font[family=Adobe Fangsong Std,name=Adobe Fangsong Std,style=plain,size=12]
    java.awt.Font[family=Adobe Garamond Pro,name=Adobe Garamond Pro,style=plain,size=12]
    java.awt.Font[family=Adobe Gothic Std,name=Adobe Gothic Std,style=plain,size=12]
    java.awt.Font[family=Adobe Hebrew,name=Adobe Hebrew,style=plain,size=12]

  15. #15
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: scrolling menus

    This is a little over my head but lets try it (with some help please)
    I implemented the ListCellRenderer interface and did the sceleton of the method
    (which will work also for the combo box I assume):


    Java Code:
    public Component getListCellRendererComponent(JList list, Object value, 
                              int index, boolean isSelected, boolean cellHasFocus) {
            
            JComboBox renderer = (JComboBox) 
                    defaultRenderer.getListCellRendererComponent(list, value, index,
                                    isSelected, cellHasFocus);
            
            
            return renderer;
        }
    now about its content, next.....

    I remember reading somewhere that each element of JList is a (super) JLabel, right?
    Would that be the same for the combo box?
    If that is true I should cast to JLabel and call super. right?

    JLabel renderer = (JLabel)
    defaultRenderer.getListCellRendererComponent(list, value, index,
    isSelected, cellHasFocus);
    Last edited by willemjav; 01-13-2014 at 10:21 AM.

  16. #16
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: scrolling menus

    I got so far now and do not know any further for the moment:

    Java Code:
    public JMenu addFonts(JMenu fm) {
    
             String[] fonts = e.getAvailableFontFamilyNames();
             Font font;
             Object[] fontobject = new Object[fonts.length];
             JMenu fontMenu = new JMenu();
             Font ft = fontMenu.getFont();
            
             for (int i = 0; i < fonts.length; i++) {
                font = new Font(fonts[i], Font.PLAIN, 12);
                fontobject[i] = font;
                System.out.println(font);
             
             }
    
             JComboBox jc = new JComboBox(fonts);
             ListCellRenderer renderer = new FontCellRenderer();
             jc.setRenderer(renderer);
             fm.add(jc);
          
             return fm;
         }
    Here is the renderer class that gets instantiated in the above method:

    Java Code:
    package willemwriter;
    
    import java.awt.Component;
    import java.awt.Font;
    import javax.swing.DefaultListCellRenderer;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.ListCellRenderer;
    
    /**
     *
     * @author willem
     */
    public class FontCellRenderer extends JLabel implements ListCellRenderer {
      protected DefaultListCellRenderer defaultRenderer =
                                                new DefaultListCellRenderer();
    
      public Component getListCellRendererComponent(JList list, Object value,
                              int index, boolean isSelected, boolean cellHasFocus) {
    
            JLabel renderer = (JLabel)
                    defaultRenderer.getListCellRendererComponent(list, value, index,
                                    isSelected, cellHasFocus);
            
            if (value instanceof Object) {
                renderer.setFont((Font) value); // <--- here enters the font string
                renderer.setText((String) value);
                
          
            } else {
          
            }
            if (!isSelected) {
              
            }
    
            return this;
      }
    
    }
    It is the value arg that counts right?
    It receives a string like this: java.awt.Font[family=Wide Latin,name=Wide Latin,style=plain,size=12]
    and should know what to do with it, right?

  17. #17
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: scrolling menus

    I have it working (with the help of the internet),
    but it is not what I want!
    I have to return to the menu and make it scroll,
    but will use this version for the toolbar some later.

    Java Code:
     public JMenu addFonts(JMenu fm) {
    
             String[] fonts = e.getAvailableFontFamilyNames();
             
    
             final JComboBox cb = new JComboBox(fonts);
             cb.setRenderer(new ComboRenderer(cb));
    
             cb.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if (e.getStateChange() == ItemEvent.SELECTED) {
                        final String fontName = cb.getSelectedItem().toString();
                        cb.setFont(new Font(fontName, Font.PLAIN, 14));
                        System.out.println(fontName);
                    }
                }
             });
             
             fm.add(cb);
             return fm;
         }
    
         private class ComboRenderer extends BasicComboBoxRenderer {
    
            private JComboBox combobox;
            final DefaultListCellRenderer defaultRenderer =
                                                    new DefaultListCellRenderer();
            private int row;
    
            private ComboRenderer(JComboBox fontcombbox) {
                combobox = fontcombbox;
            }
    
            private void getFontCombo() {
                if (combobox.getItemCount() > 0) {
                    final Object comp =
                                combobox.getUI().getAccessibleChild(combobox, 0);
                    if ((comp instanceof JPopupMenu)) {
                        final JList list = new JList(combobox.getModel());
                        final JPopupMenu popup = (JPopupMenu) comp;
                        final JScrollPane scrollPane = (JScrollPane)
                                                            popup.getComponent(0);
                        final JViewport viewport = scrollPane.getViewport();
                        final Rectangle rect = popup.getVisibleRect();
                        final Point pt = viewport.getViewPosition();
                        row = list.locationToIndex(pt);
                    }
                }
            }
    
            @Override
            public Component getListCellRendererComponent(JList list, Object value,
                            int index, boolean isSelected, boolean cellHasFocus) {
    
                super.getListCellRendererComponent(list, value, index, isSelected,
                                                                    cellHasFocus);
                if (list.getModel().getSize() > 0) {
                    getFontCombo();
                }
                final JLabel renderer = (JLabel)
                        defaultRenderer.getListCellRendererComponent(list, value,
                                                    row, isSelected, cellHasFocus);
                final Object fntObj = value;
                final String fontFamilyName = (String) fntObj;
                setFont(new Font(fontFamilyName, Font.PLAIN, 14));
                return this;
            }
        }

  18. #18
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: scrolling menus

    camickr, finally I used your link concerning scrolling and I added a mouse wheel to the code.
    Thanks!

Similar Threads

  1. Need help with building two menus.
    By Auclown in forum New To Java
    Replies: 6
    Last Post: 05-14-2013, 02:21 AM
  2. JQuery Menus
    By anithajerome in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-01-2010, 11:28 AM
  3. Canvas Menus
    By gedas in forum Java 2D
    Replies: 5
    Last Post: 02-18-2010, 11:13 PM
  4. customizing menus
    By rotte001 in forum Eclipse
    Replies: 0
    Last Post: 02-06-2010, 08:04 PM
  5. Menus Not Displaying
    By kahaj in forum AWT / Swing
    Replies: 12
    Last Post: 09-23-2009, 05:31 AM

Posting Permissions

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