Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Scrollable JMenu?

843810Aug 26 2010 — edited Aug 27 2010
Hello everyone,
I've recently been creating a small chat program, and I figured I had enough base essentials that it was time to add some customization. I want a font menu that will give the user to change the message box (JTextArea) to any font that is installed on their system and to set a size between 8 and 14. To do this, I used the following code:
JMenu fontMenu = new JMenu("Font");
        customize.add(fontMenu);
        //TODO make this scrollable
        GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Font[] fonts = e.getAllFonts(); // Get the fonts
        for (Font f : fonts) 
        {         
            final Font font = new Font(f.getName(), f.getStyle(), 10);
            JMenuItem fontItem = new JMenuItem(f.getName());
            fontMenu.add(fontItem);
            fontItem.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed (ActionEvent ae)
                {                  
                    boolean ok = true;
                    
                    do
                    {
                        String stringSize = JOptionPane.showInputDialog(null,"Enter a font size between 8 " +
                        		"and 14", "Font Size", JOptionPane.QUESTION_MESSAGE);
                        try
                        {
                            int size = Integer.parseInt(stringSize);
                            
                            if (size < 8 || size > 14)
                            {
                                ok = false;
                            }
                            else
                            {
                                ok = true;
                                convArea.setFont(new Font(font.getName(), font.getStyle(), size));
                                systemMessage("Font changed to " + convArea.getFont().getName() + 
                                        ", size " + convArea.getFont().getSize());
                            }
                        }
                        catch (NumberFormatException ex)
                        {
                            ok = false;
                        }
                    }while (!ok);
                }
            });
        }
Just so you know, customize is the name of a JMenu. systemMessage is a method to post a certain message on one user's message box. That's all well and good, but there's so many fonts installed on a system that it spills way off the screen, as shown in this screenshot: http://i36.tinypic.com/21evzpk.png

How could I make that the JMenu fontMenu limited to approximately 10 JMenuItems visible at once, and then make it scrollable using a scrollbar or the mouse wheel or scroll buttons (similar to the bookmarks menu in Firefox or Google Chrome when there are a lot of bookmarks).

Any help would be greatly appreciated. Thanks in advance.

Edited by: Skyline969 on Aug 26, 2010 7:53 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 24 2010
Added on Aug 26 2010
2 comments
827 views