Results 1 to 4 of 4
Thread: can i have a JList of Jlabels?
- 07-03-2010, 06:47 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 4
- Rep Power
- 0
can i have a JList of Jlabels?
I create a vector of JLabels and I construct a JList using the vector.
v.add(new JLabel("<html><font color=red>chayan</font></html>"));
f = new JList(v);
s = new JScrollPane(f,ScrollPaneConstants.VERTICAL_SCROLLB AR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR _AS_NEEDED);
Then I add the scrollpane to the content pane of a JFrame.
pane.add(s,BorderLayout.CENTER);
But when I run the program, the list doesnot display the label, but the string representation of the label like this (javax.swing.jlabel[,0,0,0x0,invalid,alignmentX ... etc etc]).
The labels will not have simple text, but some html components as well.How can I display the labels as it is..
- 07-03-2010, 01:05 PM #2
What does the API doc say for how a JScrollPane is to display the component when the component is a JList? It looks like it calls the toString() method for all the items in the JList.
Looks like you need to have a container of some kind that you add the JLabels to and then add that to the JScrollPane.
-
Have you tried making and using a custom ListCellRenderer?
Please have a look here for more details: How to use Lists
- 07-04-2010, 12:19 AM #4
> The labels will not have simple text, but some html components as well.
JList by default uses a DefaultListCellRenderer which is a subclass of JLabel and handles HTML just fine.dbJava Code:import javax.swing.*; public class HTMLList { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new HTMLList().makeUI(); } }); } public void makeUI() { String[] data = {"<html>Plain</html>", "<html><b>Bold</b></html>", "<html><i>Italic</i></html>", "<html><u>Underlined</u></html>"}; JList list = new JList(data); JFrame frame = new JFrame(); frame.add(new JScrollPane(list)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 400); frame.setLocationRelativeTo(null); frame.setVisible(true); } }
Similar Threads
-
new JLabels not showing in JPanel (doing revalidate&repaint)
By r00tb33r in forum AWT / SwingReplies: 6Last Post: 06-16-2010, 06:03 AM -
Overlapping JLabels
By techbossmb in forum AWT / SwingReplies: 3Last Post: 09-21-2009, 03:21 PM -
Adhoc selection & removal of JLabels
By dan0 in forum AWT / SwingReplies: 5Last Post: 03-10-2009, 06:31 PM -
problem with JLabels
By geork in forum New To JavaReplies: 3Last Post: 01-31-2008, 02:30 PM -
Having Trouble Aligning JLabels
By Mark_Petrov in forum AWT / SwingReplies: 0Last Post: 01-20-2008, 05:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks