Results 1 to 4 of 4
  1. #1
    vaidhu Guest

    Default Ultimate challenge in JComboBox

    I want to set text in my combobox with different fonts and with more than one item of every font type.


    This is my code which need changes



    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.plaf.basic.*;
    public class ComboBoxFonts extends JFrame implements ItemListener
    {
    // JTextArea textArea;
    JComboBox comboBox;

    public Font abiFont = new Font("Arial Black Italic",Font.PLAIN,20);
    public Font bmoFont = new Font("Book Man Old Style Bold",Font.BOLD,20);
    public Font cniFont = new Font("Courier New Italic",Font.PLAIN,20);
    public Font tnrFont = new Font("Times New Roman",Font.PLAIN,20);
    public Font verFont = new Font("Verdana Bold",Font.BOLD,20);
    public Font webFont = new Font("Webdings",Font.BOLD,20);


    public ComboBoxFonts()
    {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment ();
    Font [] fonts = ge.getAllFonts ();
    Font [] example = {abiFont,bmoFont,cniFont,tnrFont,verFont,webFont};
    //The above example array contains the order in which the fonts are displayed in our ComboBox.
    comboBox = new JComboBox(example);
    comboBox.setRenderer( new MyFontRenderer() );
    comboBox.addItemListener( this );
    getContentPane().add( comboBox, BorderLayout.NORTH );
    comboBox.setBounds(162,42,180,25);
    }

    public void itemStateChanged(ItemEvent e)
    {
    Font font = (Font)e.getItem();
    //textArea.setFont( font.deriveFont( textArea.getFont().getSize2D() ) );
    comboBox.setFont( font.deriveFont( comboBox.getFont().getSize2D() ) );
    }

    public static void main(String[] args)
    {
    ComboBoxFonts frame = new ComboBoxFonts();
    frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
    frame.pack();
    frame.setLocationRelativeTo( null );
    frame.setVisible( true );
    }
    class MyFontRenderer extends BasicComboBoxRenderer
    {

    public Component getListCellRendererComponent(
    JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
    {
    super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

    Font font = (Font)value;
    String fontName = font.getFontName();
    //setFont( font.deriveFont(12.0f) );

    int size = font.getSize();
    System.out.println("fontName : "+fontName+" size : "+size);


    if(fontName.equals("Arial Black Italic"))
    {
    setFont( font.deriveFont(100));
    setText(""+(char)69+""+(char)78+""+(char)71+""+(ch ar)76+""+(char)73+""+(char)83+""+(char)72);
    //setText("Welcome To Geneva Software Technologies Pvt ltd");
    return this;
    }

    else if(fontName.equals("Times New Roman"))
    {
    setFont( font.deriveFont(100));
    setText(""+(char)69+""+(char)78+""+(char)71+""+(ch ar)76+""+(char)73+""+(char)83+""+(char)72);
    setText(""+(char)99+""+(char)117+""+(char)104+""+( char)122+""+(char)77+""+(char)73+""+(char)102);
    return this;
    }

    else if(fontName.equals("Webdings"))
    {
    //Webdings font is the last font displayed in our ComboBox drop down list
    setFont( font.deriveFont(100));
    setText(""+(char)69+""+(char)78+""+(char)71+""+(ch ar)76+""+(char)73+""+(char)83+""+(char)72);
    return this;
    }
    else if(fontName.equals("Verdana Bold"))
    {
    setFont( font.deriveFont(100));
    setText(""+(char)69+""+(char)78+""+(char)71+""+(ch ar)76+""+(char)73+""+(char)83+""+(char)72);
    return this;
    }
    else if(fontName.equals("Book Man Old Style Bold"))
    {
    setFont( font.deriveFont(100));
    setText(""+(char)69+""+(char)78+""+(char)71+""+(ch ar)76+""+(char)73+""+(char)83+""+(char)72);
    return this;
    }
    else
    {
    setFont(font.deriveFont(100));
    setText(""+(char)69+""+(char)78+""+(char)71+""+(ch ar)76+""+(char)73+""+(char)83+""+(char)72);
    return this;
    }

    }
    }

    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Please don't post the same question more than one times. Wait others response. If you want to move your thread somewhere else, ask any one of our moderator. We ready to help you any time.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    he's also cross-posting in the Sun Java forums.

    To the OP: Please do not cross-post questions in multiple forums. This will frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread. For this reason, many volunteers here and at the other sites refuse to help repeat offenders.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.plaf.basic.BasicComboBoxRenderer;
    
    public class ComboRendTest extends JFrame implements ItemListener
    {
        JComboBox comboBox;
    
        public Font abiFont = new Font("Arial Black",Font.PLAIN,20);
        public Font bmoFont = new Font("Bookman Old Style Bold",Font.BOLD,20);
        public Font cniFont = new Font("Courier New Italic",Font.PLAIN,20);
        public Font tnrFont = new Font("Times New Roman",Font.PLAIN,20);
        public Font verFont = new Font("Verdana Bold",Font.BOLD,20);
        public Font webFont = new Font("Webdings",Font.BOLD,20);
    
        public ComboRendTest()
        {
            Font[] example = {abiFont,bmoFont,cniFont,tnrFont,verFont,webFont};
            examineFonts(example);
            //The above example array contains the order in which the fonts are
            // displayed in our ComboBox.
            comboBox = new JComboBox(example);
            comboBox.setRenderer( new MyFontRenderer() );
            comboBox.addItemListener( this );
            Dimension d = comboBox.getPreferredSize();
            d.width = 180;
            comboBox.setPreferredSize(d);
            JPanel panel = new JPanel(new GridBagLayout());
            panel.add(comboBox, new GridBagConstraints());
            getContentPane().add( panel, BorderLayout.CENTER );
        }
    
        public void itemStateChanged(ItemEvent e)
        {
            if(e.getStateChange() == ItemEvent.SELECTED)
            {
                Font font = (Font)e.getItem();
                comboBox.setFont( font.deriveFont(
                        comboBox.getFont().getSize2D() ) );
            }
        }
    
        private void examineFonts(Font[] fonts)
        {
            GraphicsEnvironment ge =
                GraphicsEnvironment.getLocalGraphicsEnvironment();
            Font[] allFonts = ge.getAllFonts();
            for(int i = 0; i < allFonts.length; i++)
            {
                String name = allFonts[i].getName();
                for(int j = 0; j < fonts.length; j++)
                {
                    if(name.equals(fonts[j].getName()))
                    {
                        System.out.printf("%22s font found:", name);
                        String s = "is" + (isUsable(fonts[j]) ? "" : " not");
                        System.out.printf(" %s usable%n", s);
                    }
                }
            }
        }
    
        private boolean isUsable(Font font)
        {
            String s = "ENGLISH" + "cuhzMIf";
            char[] chars = s.toCharArray();
            for(int i = 0; i < chars.length; i++)
            {
                if(!font.canDisplay(chars[i]))
                {
                    return false;
                }
            }
            return true;
        }
    
        public static void main(String[] args)
        {
            ComboRendTest frame = new ComboRendTest();
            frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
            frame.setSize(240,175);
            frame.setLocationRelativeTo( null );
            frame.setVisible( true );
        }
    }
    
    class MyFontRenderer extends BasicComboBoxRenderer
    {
        public Component getListCellRendererComponent(JList list,
                                                      Object value,
                                                      int index,
                                                      boolean isSelected,
                                                      boolean cellHasFocus)
        {
            super.getListCellRendererComponent(list, value, index,
                                               isSelected, cellHasFocus);
    
            Font font = (Font)value;
            String fontName = font.getFontName(); 
            setFont(font.deriveFont(100));
    
            if(fontName.equals("Arial Black"))
            {
                setText(""+(char)69+""+(char)78+""+(char)71+""+
                        (char)76+""+(char)73+""+(char)83+""+(char)72);
            }
    
            else if(fontName.equals("Times New Roman"))
            {
                //setText(""+(char)69+""+(char)78+""+(char)71+""+
                //        (char)76+""+(char)73+""+(char)83+""+(char)72);
                setText(""+(char)99+""+(char)117+""+(char)104+""+
                        ( char)122+""+(char)77+""+(char)73+""+(char)102);
            }
    
            else if(fontName.equals("Webdings"))
            {
                //Webdings font is the last font displayed in our
                // ComboBox drop down list
                setText(""+(char)69+""+(char)78+""+(char)71+""+
                        (char)76+""+(char)73+""+(char)83+""+(char)72);
            }
            else if(fontName.equals("Verdana Bold"))
            {
                setText(""+(char)69+""+(char)78+""+(char)71+""+
                        (char)76+""+(char)73+""+(char)83+""+(char)72);
            }
            else if(fontName.equals("Book Man Old Style Bold"))
            {
                setText(""+(char)69+""+(char)78+""+(char)71+""+
                        (char)76+""+(char)73+""+(char)83+""+(char)72);
            }
            else
            {
                setText(""+(char)69+""+(char)78+""+(char)71+""+
                        (char)76+""+(char)73+""+(char)83+""+(char)72);
            }
            return this;
        }
    }

Similar Threads

  1. java programming challenge Number 1
    By tacosalad in forum New To Java
    Replies: 5
    Last Post: 01-16-2008, 11:25 PM
  2. k this is my ultimate project. kinda
    By jason27131 in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 05:47 AM
  3. The Million Musician Challenge 0.821
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-25-2007, 08:39 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
  •