2

I have been learning Java from thenewboston on YouTube, and after following the two JComboBox tutorials here and here, I have found three warnings in my program.


FirstClass.java

import javax.swing.JFrame;

public class FirstClass {

    public static void main(String args[]) {

        Scrabble go = new Scrabble();
        go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        go.setSize(300,200);
        go.setVisible(true);

    }

}

Scrabble.java

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

public class Scrabble extends JFrame {

    private JComboBox box;
    private JLabel picture;

    private static String[] filename = {"Coraline_Small.jpg", "fish.jpg"};
    private Icon[] pics = {new ImageIcon(getClass().getResource(filename[0])), new ImageIcon(getClass().getResource(filename[1]))};

    public Scrabble() {

        super("JComboBox");
        setLayout(new FlowLayout());

        box = new JComboBox(filename);

        box.addItemListener(

            new ItemListener() {

                public void itemStateChanged(ItemEvent event) {

                    if(event.getStateChange() == ItemEvent.SELECTED);
                        picture.setIcon(pics[box.getSelectedIndex()]);

                };
            }

        );

        add(box);
        picture = new JLabel(pics[0]);
        add(picture);               

    }   
}

The first warning appears on line 5 of Scrabble.java,

public class Scrabble extends JFrame {

saying:

The serializable class Scrabble does not declare a static final serialVersionUID field of type long


The second warning appears on line 7 of Scrabble.java,

private JComboBox box;

saying:

JComboBox is a raw type. References to generic type JComboBox should be > > parameterized


The third and final warning appears on line 18 of Scrabble.java,

box = new JComboBox(filename);

saying:

Multiple markers at this line - Type safety: The constructor JComboBox(Object[]) belongs to the raw type JComboBox. References to generic type JComboBox should be
parameterized - JComboBox is a raw type. References to generic type JComboBox should be parameterized


Also, the program runs successfully.


All help is appreciated and thanks in advance,

Jeremy

  • 3
    [What does it mean: The serializable class does not declare a static final serialVersionUID field?](http://stackoverflow.com/questions/2288937/what-does-it-mean-the-serializable-class-does-not-declare-a-static-final-serial) | [JComboBox is a raw type. References to generic type JComboBox should be parameterized](http://stackoverflow.com/questions/20596020/jcombobox-is-a-raw-type-references-to-generic-type-jcomboboxe-should-be-param) – BackSlash Aug 04 '15 at 09:46

1 Answers1

2

To understand the first warning read about Serializing. See here for example.

The second warning is about declaring a JComboBox without specifying a type. To avoid the warning, add a type like this:

private JComboBox<String> box;
c0der
  • 15,550
  • 6
  • 26
  • 53