包含 JTextAreas 的 JList 显示 JTextAreas' toString() 而不是 JTextArea

发布于 2024-11-25 12:57:36 字数 841 浏览 0 评论 0原文

我正在使用 JList,并且尝试对单元格使用 JTextAreas (实现 ListCellRenderer)。它不起作用。这些单元格仅显示 ListCellRenderer.toString() 而不是实际的 JTextArea。有人可以帮忙吗?谢谢。


  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;
    }
  }
}

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. Could 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;
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蹲墙角沉默 2024-12-02 12:57:37

您应该只发布可编译的代码,并且您的代码有点混乱。您不应将文本传递到渲染器的构造函数中,因为该构造函数将用于渲染列表中所有项目的单个渲染器(除非您希望所有项目都使用相同的代码)。您不应忽略传递到 getListCellRendererComponent 方法中的 Object 参数,因为这是渲染器项显示的数据。例如:

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();
         }
      });
   }
}

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();
         }
      });
   }
}
海拔太高太耀眼 2024-12-02 12:57:37

验证您是否正在调用 setCellRenderer(),它设置“用于绘制列表中每个单元格的委托”。

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

花间憩 2024-12-02 12:57:37

你应该使用渲染器。我现在没有代码,但它非常简单。 JList 的默认渲染器调用 toString() 并显示结果。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文