Why I can't add a JLabel object into JList/JComboBox?

Discussion in 'Java' started by RC, Feb 22, 2006.

  1. RC

    RC Guest

    I'm try to make a list with different background colors.

    Here is what I do


    JLabel label1 = new JLabel("Red Background");
    JLabel label2 = new JLabel("Blue Background");
    label1.setBackground(Color.red);
    label2.setBackground(Color.blue);

    Dimension d = new Dimension(80, 20);
    label1.setPreferredSize(d);
    label1.setMaximumSize(d);
    label1.setMinimumSize(d);
    label2.setPreferredSize(d);
    label2.setMaximumSize(d);
    label2.setMinimumSize(d);

    DefaultListModel listModel = new DefaultListModel();
    JList list = new JList(listModel);

    listModel.addElement(label1);
    listModel.addElement(label2);
    list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    list.setVisibleRowCount(4);
    list.setLayoutOrientation(JList.VERTICAL);
    list.setDragEnabled(true);

    JScrollPane scrollPane = new JScrollPane(list);

    There are no compiling error messages.
    But here is the run-time error adding to the JList instead
    the JLabels

    javax.swing.JLabel[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=java.awt.Dimension[width=.....]

    Can anyone tell me how to achieve my goal, have different background
    colors in a list?

    Thank Q very much in advance!
    RC, Feb 22, 2006
    #1
    1. Advertising

  2. RC

    VisionSet Guest

    "RC" <> wrote in message
    news:dthn8j$rni$...

    >
    > Can anyone tell me how to achieve my goal, have different background
    > colors in a list?
    >


    You can't add Components directly because swing optimises the drawing
    process by using one component to render all elements. ie as it iterates
    through the elements in order to draw it take a single component and changes
    its state then draws it to the graphics object.
    So you need a different approach that uses swings optimised mechanism.

    see
    http://java.sun.com/docs/books/tutorial/uiswing/components/list.html#renderer

    All list/table type components work the same way, so other tutorials will be
    just as good to get the gist.

    http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html#renderer

    --
    Mike W
    VisionSet, Feb 22, 2006
    #2
    1. Advertising

  3. RC

    Paul Hamaker Guest

    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VJ
    Replies:
    1
    Views:
    2,904
    Dotty
    Mar 5, 2005
  2. Replies:
    5
    Views:
    3,015
    Andrew Thompson
    Dec 26, 2005
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,768
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    7
    Views:
    40,085
    rex64
    Oct 29, 2006
  5. albert kao
    Replies:
    10
    Views:
    2,954
    Eric Sosman
    Mar 5, 2011
Loading...

Share This Page