Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Why I can't add a JLabel object into JList/JComboBox?

Reply
Thread Tools

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

 
 
RC
Guest
Posts: n/a
 
      02-22-2006
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,bor der=,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!
 
Reply With Quote
 
 
 
 
VisionSet
Guest
Posts: n/a
 
      02-22-2006

"RC" <(E-Mail Removed)> wrote in message
news:dthn8j$rni$(E-Mail Removed)...

>
> 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/tutor....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/tutor....html#renderer

--
Mike W


 
Reply With Quote
 
 
 
 
Paul Hamaker
Guest
Posts: n/a
 
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why my applet can't display components like JButton or JTextField, but JLabel is well feofao@gmail.com Java 5 12-26-2005 02:24 AM
HOWTO add Text on image created using JLabel ?? VJ Java 1 03-05-2005 01:28 AM
JLabel behaves differently printing than in GUI Cindy Huyser Java 1 08-07-2003 09:53 PM



Advertisments