开发者

JList that contains JTextAreas displays the JTextAreas' toString() instead of the JTextArea

I am using a JList, and I'm trying to use JTextAreas (that implement ListCellRenderer) for the cells. It isn't working. The cells simply display the ListCellRenderer.toString() instead of the actual JTextArea. Cou开发者_StackOverflow社区ld someone help? Thanks.


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

class ButtonListener implements ActionListener() {
  public void actionPerformed(ActionEvent e){
    listModel.clear();
    for (String s : stringArray) {
      listModel.addElement(new Listm(s));
    }
  }
}

class Listm extends JTextArea implements ListCellRenderer {
  protected Listm(String text) {
   setText(text); //Outputting the text element displays the desired String
  }

  public Component getListCellRendererComponent(JList list, Object object, int number, boolean bool, boolean bool2) {
    setPreferredSize(new Dimension(x, y));
    return this;
    }
  }
}


You should post compilable code only, and your code is a bit confusing. You shouldn't pass text into the renderer's constructor as this one constructor will be used for the single renderer that renders all items in the list (unless you want all to use the same code). You shouldn't ignore the Object parameter that is passed into your getListCellRendererComponent method, for this is the data that the renderer item displays. For example:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyGui extends JPanel {
   public static final String[] DATA = {"One\n1", "Two\n2", "Three\n3"};
   private DefaultListModel listModel = new DefaultListModel();
   private JList list = new JList(listModel);

   public MyGui() {
      list.setCellRenderer(new Listm(3, 30));
      add(new JScrollPane(list));
      for (String datum : DATA) {
         listModel.addElement(datum);
      }
   }

   private class Listm extends JTextArea implements ListCellRenderer {
      protected Listm(int rows, int cols) {
         super(rows, cols);
         setBorder(BorderFactory.createLineBorder(Color.blue));
      }

      public Component getListCellRendererComponent(JList list, Object value,
               int index, boolean isSelected, boolean cellHasFocus) {
         setText(value.toString());
         if (cellHasFocus) {
            setBackground(FOCUSED_COLOR);
         } else if (isSelected) {
            setBackground(SELECTED_COLOR);
         } else {
            setBackground(null);
         }
         return this;
      }
   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("MyGui");
      frame.getContentPane().add(new MyGui());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}


Verify that you are invoking setCellRenderer(), which sets "the delegate that is used to paint each cell in the list."


You should use renderer. I do not have a code right now but it is pretty simple. The default renderer of JList calls toString() and displays the result.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜