Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JComboBox Error

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
like image 933
Lukasz Avatar asked Aug 04 '13 23:08

Lukasz


People also ask

What is JComboBox?

JComboBox is a part of Java Swing package. JComboBox inherits JComponent class . JComboBox shows a popup menu that shows a list and the user can select a option from that specified list . JComboBox can be editable or read- only depending on the choice of the programmer .

What is the difference between JList and JComboBox?

A JComboBox is a component that displays a drop-down list and gives users options that we can select one and only one item at a time whereas a JList shows multiple items (rows) to the user and also gives an option to let the user select multiple items.

How do I make JComboBox not editable?

u can make ur jcombobox uneditable by calling its setEnabled(false). A JComboBox is unEditable by default. You can make it editable with the setEditable(boolean) command. If you are talking about enabled or disabled you can set that by the setEnabled(boolean) method.

Which listener is implemented for JComboBox?

Adds a PopupMenu listener which will listen to notification messages from the popup portion of the combo box. Initializes the editor with the specified item. Sets the properties on this combobox to match those in the specified Action . This method is public as an implementation side effect.


1 Answers

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

like image 178
Reimeus Avatar answered Oct 20 '22 08:10

Reimeus