JComboBox Error
Asked Answered
F

3

5

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
Figuration answered 4/8, 2013 at 23:2 Comment(1)
Are you sure there is an error? That sounds like a warning regarding type safety. If that is indeed the case, then you can use @SuppressWarnings("unchecked") as noted here: #1130295Vindicate
E
15

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

Eyeleen answered 4/8, 2013 at 23:5 Comment(0)
M
0

Oracle, as of the present, has in their Swing tutorials this:

https://docs.oracle.com/javase/tutorial/uiswing/examples/layout/CardLayoutDemoProject/src/layout/CardLayoutDemo.java

Running this causes the following line to throw this warning:

JComboBox cb = new JComboBox(comboBoxItems);

Possibly because the code hasn't been updated since 2008? This solution helped me clean up the warning and compile smoothly, so kudos.

Mogul answered 18/10, 2019 at 10:24 Comment(0)
S
0

This is a warning not an error due to imageComboBox = new JComboBox( names );

run the program again and it will be working fine

Here is an image of the run

Santalaceous answered 13/2, 2020 at 12:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.