Forum Stats

  • 3,839,855 Users
  • 2,262,546 Discussions
  • 7,901,081 Comments

Discussions

List Box programs not running

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited Aug 28, 2015 11:46AM in New To Java

Hi,

i am trying to create List Box. I have tried different programs downloaded from internet but i am getting Xlint:unchecked warning.


G:\Zulfi\java prog\2015\gui\ListBox>javac ListBox1.java

Note: ListBox1.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

G:\Zulfi\java prog\2015\gui\ListBox>javac -Xlint:unchecked ListBox1.java

ListBox1.java:17: warning: [unchecked] unchecked call to JList(E[]) as a member

of the raw type JList

        JList list = new JList(options);

                     ^

  where E is a type-variable:

    E extends Object declared in class JList

1 warning

G:\Zulfi\java prog\2015\gui\ListBox>javac ListBox1.java

Note: ListBox1.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

G:\Zulfi\java prog\2015\gui\ListBox>type ListBox1.java

import java.awt.*;

import javax.swing.*;

public class ListBox1 extends Object{

    public static void main(String[] args) {

        JFrame frame = new JFrame("Test Frame");

        frame.setSize(new Dimension(300,200));

        frame.setLocation(100,100);

        Container contentPane = frame.getContentPane();

        JLabel label = new JLabel("HERE IS A LABEL");

        contentPane.add(label, BorderLayout.NORTH);

        JButton button = new JButton("BUTTON");

        contentPane.add(button, BorderLayout.SOUTH);

        String[] options = {"Option 1", "Option 2",

                            "Option 3"};

        JList list = new JList(options);

        contentPane.add(list, BorderLayout.CENTER);

        frame.setVisible(true);

    }

}

However, i have tried and the program is running. Some body please guide me.

Zulfi.

Answers

  • Unknown
    edited Aug 28, 2015 11:46AM
    i am trying to create List Box. I have tried different programs downloaded from internet but i am getting Xlint:unchecked warning.
    
    G:\Zulfi\java prog\2015\gui\ListBox>javac ListBox1.java
    Note: ListBox1.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    
    
    G:\Zulfi\java prog\2015\gui\ListBox>javac -Xlint:unchecked ListBox1.java
    ListBox1.java:17: warning: [unchecked] unchecked call to JList(E[]) as a member
    of the raw type JList
            JList list = new JList(options);
                         ^
      where E is a type-variable:
        E extends Object declared in class JList
    1 warning
    

    That is a WARNING for the reason given in the exception. You are using a GENERIC class without declaring what TYPE of objects the JList should contain.

    See the Java API

    JList (Java Platform SE 7 )

    Class JList<E>

    
    java.lang.Object
    
    
    java.awt.Component
    
    
    java.awt.Container
    
    
    javax.swing.JComponent
    
    
    javax.swing.JList<E>
    
    
    
    
    
    
    
    
    
    
    Type Parameters:
    E - the type of the elements of this list
    All Implemented Interfaces:
    ImageObserver, MenuContainer, Serializable, Accessible, Scrollable

    
    public class JList<E> extends JComponent implements Scrollable, Accessible

    See that class declaration? See that 'JList<E>' in it? See the description of the Type Parameter above.

    E - the type of the elements of this list
    

    You did NOT tell the compiler what class E is; that is, what type of objects are ALLOWED in the JList. Is this a list of Container? JLabel? JButton? JFrame? String? Integer? What?

    You passed a parameter named 'options' that is a String array but you could put ANYTHING in that list since it is just a list of Object types.

    JList<String>
    JList<JButton>
    

    That first declaration tells the compiler this is a list of strings and there should be NOTHING but instances of the String class in it.

    The second one declares a list of buttons.

    Now the compiler can check that your code doesn't try to put other objects in that list. Try to put a string in the button list and the compiler will give you an ERROR, not a warning.

    However, i have tried and the program is running. Some body please guide me.
    

    Correct - that is a 'warning' not an error to remind you that code could possibly put any other kind of object in that list.

    If you don't want to see those warnings then do what the exception says and recompile with '-Xlint'.

This discussion has been closed.