JComboBox Error

Lukasz picture Lukasz · Aug 5, 2013 · Viewed 7.4k times · Source

I'm trying to get the following program to compile from "Introduction to Java Programming 9th edition, by Liang. I'm getting an error with the following example in regards to the JComboBox:

import javax.swing.*;

public class GUIComponents
{
    public static void main (String[] args)
    {
        JButton jbtOK = new JButton ("OK");                                             // Creates a button with test OK
        JButton jbtCancel = new JButton ("Cancel");                                 // Creats a cancel button
        JLabel jlblName = new JLabel ("Enter your name: ");                 //  Creates a label with the respective text
        JTextField jtfName = new JTextField ("Type Name Here");     // Creates a text field with the respective text
        JCheckBox jchkBold = new JCheckBox ("Bold");                        // Creates a check boc wth the text bold
        JCheckBox jchkItalic = new JCheckBox ("Italic");
        JRadioButton jrbYellow = new JRadioButton ("Yellow");               // Creates a radio button with text Yellow
        JRadioButton jrbRed = new JRadioButton  ("Red");                        // Creates a radio Button with text Red
        **JComboBox jcboColor = new JComboBox (new String[] {"Freshman", "Sophomore", "Junior", "Senior"});**
        JPanel panel = new JPanel ();                                                           // Creates a panel to group components
        panel.add (jbtOK);                                                                          // Add the OK button to the panel
        panel.add (jbtCancel);                                                                      // Add the Cancel button to the panel
        panel.add (jlblName);                                                                       // Add the lable to the panel
        panel.add (jtfName);
        panel.add (jchkBold);
        panel.add (jchkItalic);
        panel.add (jrbRed);
        panel.add (jrbYellow);
        panel.add (jcboColor);

        JFrame frame = new JFrame ();
        frame.add (panel);
        frame.setTitle ("Show GUI Components");
        frame.setSize (450,100);
        frame.setLocation (200, 100);
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setVisible (true);
     }
}

The error that is being produced is:

warning: [unchecked] unchecked call to JComboBox(E[]) as  a member of the raw type JComboBox
    JcomboBox jcboColor = new JComboBox(new String[] {"Freshman", "Sophomore", "Junior", "Senior"});

Where E is a time-variable: 
    E extends Object Declared in class JComboBox

Answer

Reimeus picture Reimeus · Aug 5, 2013

It's a warning not an error. You're missing the generic type which JComboBox expects, introduced in Java 1.7. Without it a cast will be necessary every time a value is retrieved from the ComboBoxModel Add the String type to the declaration to match the model data

JComboBox<String> jcboColor = new JComboBox<>(new String[] { ... });

Read this interesting article What is an "unchecked" warning? from the Generics FAQ