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

    Default Please help, the combo content is not rendering into the font family type!

    Please help, the combo content is not rendering into the font family type!
    What is going (again) wrong here.
    I am re-writing code so many times that I get lost....

    Java Code:
    combo1 = new JComboBox(dummy);           
                combo1.setRenderer(new ComboRenderer1());
                combo1.setEnabled(false);
                combo1.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent event) {
    
                        currentFont = (String) combo1.getSelectedItem();
                        if (currentFont != null) {
                            fontMenu.getCurrentFontInfo().setFontFamily(currentFont);
                        }
                    }
                });

    Java Code:
    if (shortFontListMap.size() > 0) {
                                
                            Set entries = shortFontListMap.entrySet();
                            Iterator iter = entries.iterator();
                            JRadioButtonMenuItem rb;
                                                   
                            while (iter.hasNext()) {
                                Map.Entry entry = (Map.Entry)iter.next();
                                String name = (String) entry.getKey();
                                Font font = new Font(name, Font.PLAIN, 16);
                                combo1.setFont(font);
                                combo1.addItem(name);
                            }
                            combo1.setEnabled(true);
                         }


    Java Code:
     private class ComboRenderer1 extends BasicComboBoxRenderer {
    
                @Override
                public Component getListCellRendererComponent(JList list,
                        Object value, int index, boolean isSelected,
                                                    boolean cellHasFocus) {
                    super.getListCellRendererComponent(list, value, index,
                                                    isSelected, cellHasFocus);
                    String fontname =  (String) value;
    
                    this.setText(fontname);
                    return this;
                }
            }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    929
    Rep Power
    2

    Default Re: Please help, the combo content is not rendering into the font family type!

    Again, creating a small practice-case will help enormously...
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class ComboDemo {
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
    
            JPanel content = new JPanel( new FlowLayout());
    
            DefaultComboBoxModel<Font> comboBoxModel = new DefaultComboBoxModel<Font>();
            final JComboBox<Font> fontCombo = new JComboBox<Font>(comboBoxModel);
    
            Font[] allFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
            for (Font font : allFonts) {
                comboBoxModel.addElement(font);
            }
    
            fontCombo.setRenderer(new DefaultListCellRenderer() {
                @Override
                public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                    JLabel result = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                    Font font = (Font) value;
                    result.setFont(font.deriveFont(Font.PLAIN, 14));
                    result.setText(font.getName());
                    return result;
                }
            });
    
            fontCombo.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Font selected = (Font) fontCombo.getSelectedItem();
                    fontCombo.setFont(selected.deriveFont(Font.PLAIN, 14));
                }
            });
    
            fontCombo.setSelectedIndex(0);
    
    
            content.add(fontCombo);
    
            f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            f.setLocationRelativeTo(null);
            f.setContentPane(content);
            f.setSize(500, 500);
            f.setVisible(true);
    
        }
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

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

    Default Re: Please help, the combo content is not rendering into the font family type!

    thanks surfman!

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

    Default Re: Please help, the combo content is not rendering into the font family type!

    I needed to take the parameter types <> out on my Java.

    So I modified your example just a little, since my combo items
    are of the type String not Font!
    Thanks, again Surfman:

    Java Code:
    private class ComboRenderer1 extends BasicComboBoxRenderer {       
            @Override
                public Component getListCellRendererComponent(JList list, Object 
                        value, int index, boolean isSelected, boolean cellHasFocus) {
                    JLabel result = (JLabel) 
                            super.getListCellRendererComponent(list, value, index, 
                                                        isSelected, cellHasFocus);
                    String fontname = (String) value;
                    Font font = new Font(fontname, Font.PLAIN, 16);
                    result.setFont(font);
                    result.setText(font.getName());
                    return result;
                }
            }

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    929
    Rep Power
    2

    Default Re: Please help, the combo content is not rendering into the font family type!

    DefaultComboBoxModel<Font> is indeed Java 7.

    Why are you subclassing BasicComboBoxRenderer? The way to modify the way the JComboBox renders the list is to use a ListCellRenderer. BasicComboBoxRenderer is PLAF specific and should be avoided. Unless you write your own Look and Feel, I doubt you should be subclassing any java.swing.plaf.*
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

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

    Default Re: Please help, the combo content is not rendering into the font family type!

    So i extend DefaultListCellRenderer, instead?

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    929
    Rep Power
    2

    Default Re: Please help, the combo content is not rendering into the font family type!

    Quote Originally Posted by willemjav View Post
    So i extend DefaultListCellRenderer, instead?
    That is the common practice, yes.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. rendering actions in a combo box
    By willemjav in forum Advanced Java
    Replies: 5
    Last Post: 02-02-2014, 12:16 PM
  2. renewing the content of a combo box
    By willemjav in forum AWT / Swing
    Replies: 5
    Last Post: 02-01-2014, 11:49 AM
  3. Incorrect Bengali font rendering
    By netvaibhav in forum AWT / Swing
    Replies: 3
    Last Post: 10-24-2010, 07:35 PM
  4. Replies: 0
    Last Post: 08-18-2010, 10:31 AM
  5. JTextPane Font's with AffineTransform Rendering
    By StormyWaters in forum AWT / Swing
    Replies: 7
    Last Post: 09-18-2009, 09:56 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
  •