Results 1 to 4 of 4
  1. #1
    chayan is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Question 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..

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    > 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.
    Java 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);
      }
    }
    db

Similar Threads

  1. Replies: 6
    Last Post: 06-16-2010, 06:03 AM
  2. Overlapping JLabels
    By techbossmb in forum AWT / Swing
    Replies: 3
    Last Post: 09-21-2009, 03:21 PM
  3. Adhoc selection & removal of JLabels
    By dan0 in forum AWT / Swing
    Replies: 5
    Last Post: 03-10-2009, 06:31 PM
  4. problem with JLabels
    By geork in forum New To Java
    Replies: 3
    Last Post: 01-31-2008, 02:30 PM
  5. Having Trouble Aligning JLabels
    By Mark_Petrov in forum AWT / Swing
    Replies: 0
    Last Post: 01-20-2008, 05:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •