Manoj Gupta
Manoj Gupta

Reputation: 298

how to get a Integer value from a Combo box in java?

I've created a class named as fontlist and I want to get the Integer value from the Combo box, but it gives me some error.

How do I get an Integer value from the Combo box? Also, I want to change the size of text according to an integer value that gets to the fontsize Combo Box ...

My code is:

        import java.awt.*;
        import javax.swing.*;
        import java.awt.event.*;
        import java.util.*;

        class fontlist extends JFrame implements ItemListener
        { JComboBox jcb,fontSize;
        Container content;
        JTextArea jta;
        JScrollPane jsp;
        private static final int[] fontsize = {8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72};

        fontlist()
        {
        content=getContentPane();
        setLayout(null);
        setBackground(Color.WHITE);
        jcb=new JComboBox();
        content.add(jcb);
        jcb.setBounds(100,100,100,20);

        fontSize=new JComboBox();
        content.add(fontSize);
        fontSize.setBounds(200,100,100,20);
        jta=new JTextArea();
        jsp=new JScrollPane(jta);
        content.add(jsp);
        jsp.setBounds(100,120,200,200);
        jcb.addItemListener(this);
        fontSize.addItemListener(this);
        String fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        for ( int i = 0; i < fonts.length; i++ )
        {
        jcb.addItem(fonts[i]);
        } 
        for ( int j = 0; j <16; j++ )
        {
        fontSize.addItem(fontsize[j]);
        } 
        }
        public void itemStateChanged(ItemEvent ie)
        {
        if (ie.getStateChange() == ItemEvent.SELECTED) 
        {
        System.out.println(""+fontSize.getSelectedItem());
          String size = (fontSize.getSelectedItem()+"");
         System.out.println(size);
         int size1 =Integer(size);
        }
        }
        public static void main(String args[])
        {
        fontlist fl=new fontlist();
        fl.setSize(700,500);
        fl.setVisible(true);
        }

        }

And the error is:

enter image description here

Upvotes: 2

Views: 2121

Answers (3)

Cruncher
Cruncher

Reputation: 7796

Change

int size1 =Integer(size); 

to

int size1 = Integer.parseInt(size);

After a second look at your code

int size1 = (Integer)(fontSize.getSelectedItem());

may also work.

Upvotes: 1

clcto
clcto

Reputation: 9648

You probably meant to use:

int size1 = new Integer( size );

Or you could use:

int size1 = Integer.parseInt( size );

(Both will require you to use a try/catch block to catch NumberFormatException)

Upvotes: 1

vandale
vandale

Reputation: 3650

declare fontSize as JComboBox<Integer> fontSize

you need to let java know that the combo box hold Integers If you look at the documentation for JComboBox , you will note it is declared as JComboBox<E>. The E is what is know as a type parameter and describes what type of data the JComboBox will hold. If you don't iunclude it, it will default to Object

Upvotes: 0

Related Questions