Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Please help resolve warning re parameterized types / Jlist ListCellRenderer

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Location
    California
    Posts
    9
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help resolve warning re parameterized types / Jlist ListCellRenderer

    I'm developing a desktop app using JDK 1.7.0 on a Windows 7 machine, not that that should matter. My question is about parameterized types and specifically a ListCellRenderer for a JList. I have included both an example of the the warnings I am receiving and the code that the compiler is complaining about.

    I admit that I'm still very new to Swing and not all that comfortable with parameterized types beyond simple cases. Any words of wisedom regarding these warnings will be appreciated as will recommendations for a good tutorial on parameterized types.

     

    warning: [unchecked] unchecked call to setCellRenderer(ListCellRenderer<? super E>) as a member of the raw type JList
    scrollingList.setCellRenderer(new MyListCellRenderer());
    where E is a type-variable:
    E extends Object declared in class JList




    class MyListCellRenderer extends JLabel 
                             implements ListCellRenderer<Object> {
     
        public MyListCellRenderer() {
            setOpaque(true);
        }
     
        @Override
        public Component getListCellRendererComponent(JList<?> list,
                                                      Object value,
                                                      int index,
                                                      boolean isSelected,
                                                      boolean cellHasFocus) {
     
            setText(((JLabel)value).getText());
            setIcon(((JLabel)value).getIcon());
     
            Color background;
            Color foreground;
     
            // check if this cell represents the current DnD drop location
            JList.DropLocation dropLocation = list.getDropLocation();
            if (dropLocation != null
            && !dropLocation.isInsert()
            && dropLocation.getIndex() == index) {
                background = Color.PINK.darker();
                foreground = Color.CYAN.brighter();
            }
            else if (isSelected) {
                background = list.getSelectionBackground();
                foreground = list.getSelectionForeground();
            }
            else {
                background = Color.WHITE;  // getBackground is ugly
                foreground = list.getForeground();
            }
     
            setBackground(background);
            setForeground(foreground);
     
            return this;
        }
    }


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Please help resolve warning re parameterized types / Jlist ListCellRenderer

    Quote Originally Posted by mageorge View Post
     

    warning: [unchecked] unchecked call to setCellRenderer(ListCellRenderer<? super E>) as a member of the raw type JList
    scrollingList.setCellRenderer(new MyListCellRenderer());
    where E is a type-variable:
    E extends Object declared in class JList


    To avoid the warning, scrollingList must be:

    JList<Object> scrollingList = new JList<Object>();

    Quote Originally Posted by mageorge View Post
            setText(((JLabel)value).getText());
            setIcon(((JLabel)value).getIcon());
    Speaking in general, it's not very good that the "value" is a JLabel. It should be a simple "bean" class with some properties (in your case for a String and an Icon). Not a GUI component.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. [SOLVED] I want us to help me to resolve my duty in JavaScript
    By arwa qashqari in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 17th, 2014, 12:35 PM
  2. Cannot Resolve Symbol Error
    By tyeeeee1 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 6th, 2012, 05:12 PM
  3. can I create an array in a parameterized class
    By mia_tech in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 7th, 2012, 07:13 PM
  4. What's wrong with my code?? (it said cannot resolve symbol)
    By sakura_smile in forum What's Wrong With My Code?
    Replies: 23
    Last Post: June 11th, 2012, 06:55 AM
  5. Not Generic; cannot be parameterized
    By javaoo in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 15th, 2010, 09:53 AM