Java 如何选择JList中的行,其中每个单元格包含包含JTextArea的JPanel

Java 如何选择JList中的行,其中每个单元格包含包含JTextArea的JPanel,java,swing,jlist,Java,Swing,Jlist,我的代码目前是这样写的,因为我的目的是在JList的每个单元格中包装文本。 现在,该部分在JList渲染的面板中成功使用了JTextArea,但我有一个不同的问题。 我似乎无法像简单的JList实现那样让每一行都可以选择 如何使每一行都可选择?或者您是否建议使用不同的组件将文本包装到JList中 import java.awt.BorderLayout; import java.awt.Component; import javax.swing.BorderFactory; import ja

我的代码目前是这样写的,因为我的目的是在JList的每个单元格中包装文本。 现在,该部分在JList渲染的面板中成功使用了JTextArea,但我有一个不同的问题。 我似乎无法像简单的JList实现那样让每一行都可以选择

如何使每一行都可选择?或者您是否建议使用不同的组件将文本包装到JList中

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class JListPractice3 {

public static void main(final String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new JListPractice3();
        }
    });
}

public JListPractice3() {
    JFrame f = new JFrame("JList Practice");
    f.setResizable(true);
    f.setVisible(true);
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().setLayout(new BorderLayout());

    JPanel listPanel = this.buildJListPanel();

    f.add(listPanel);

    f.pack();
}

/**
 * Build JList panel
 * @return JPanel
 */
private JPanel buildJListPanel() {
    JPanel listPanel = new JPanel();
    listPanel.setLayout(new BorderLayout());

    JList jList = new JList(getData());
    jList.setVisibleRowCount(1);
    jList.setFixedCellHeight(50);
    jList.setFixedCellWidth(250);
    jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jList.setCellRenderer(new MyCellRenderer());

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.getViewport().setView(jList);
    listPanel.add(scrollPane, BorderLayout.CENTER);
    return listPanel;
}

/**
 * Get Alias data
 * @return String[]
 */
private String[] getData() {
    //TODO: remove hard code
    String[] data = {"123456789012345678901234567890123456789012345678901234567890123456789"
            + "012345678901234567890123456789012345678901234567890123456789012",
            "two two two two two two two two two two two twotwo two two two two two two two two two two twotwo two two end",
            "two two two two two two two two two two two twotwo two two two two two two two end",
            "five", "six"};
    return data;
}

private class MyCellRenderer implements ListCellRenderer {
    private JPanel p;
    private JPanel linePanel;
    private JLabel lineLabel;
    private JTextArea textArea;

    public MyCellRenderer() {
        p = new JPanel();
        p.setLayout(new BorderLayout());

        linePanel = new JPanel(new BorderLayout());
        linePanel.setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3));
        lineLabel = new JLabel("Test");
        linePanel.add(lineLabel, BorderLayout.NORTH);
        p.add(linePanel, BorderLayout.WEST);

        textArea = new JTextArea();
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        p.add(textArea, BorderLayout.CENTER);
    }

    @Override
    public Component getListCellRendererComponent(final JList list,
            final Object value, final int index, final boolean isSelected,
            final boolean hasFocus) {

        textArea.setText((String) value);
        int width = list.getWidth();
        // this is just to lure the ta's internal sizing mechanism into action
        if (width > 0) {
            textArea.setSize(width, Short.MAX_VALUE);
        }
        return p;
    }
  }
}

如果决定使用接口ListCellRender,则必须实现all effect,因此为了澄清起见,ListCellRender是一个用于设置值和all Listener的组件(例如JLabel)(在本例中,您已经在组件中添加了Listener)

所以在您的解决方案中,组件是可选择的,但您的代码并没有绘制它,这是YouCellRender的my实现

private class MyCellRenderer extends JTextArea implements ListCellRenderer{
    private JPanel p;
    private JPanel linePanel;
    private JLabel lineLabel;

    public MyCellRenderer() {
        p = new JPanel();
        p.setLayout(new BorderLayout());

        linePanel = new JPanel(new BorderLayout());
        linePanel.setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3));
        lineLabel = new JLabel("Test");
        linePanel.add(lineLabel, BorderLayout.NORTH);
        p.add(linePanel, BorderLayout.WEST);

        this.setLineWrap(true);
        this.setWrapStyleWord(true);
        p.add(this, BorderLayout.CENTER);
    }

    public Component getListCellRendererComponent(final JList list,
            final Object value, final int index, final boolean isSelected,
            final boolean hasFocus) {
        if(isSelected){
            this.setBackground(Color.BLUE);
        }else{
            this.setBackground(Color.GRAY);
        }
        this.setText((String) value);
        int width = list.getWidth();
        // this is just to lure the ta's internal sizing mechanism into action
        if (width > 0) {
            this.setSize(width, Short.MAX_VALUE);
        }
        return this;
    }
  }
结果是

如何使每一行都可选择?或者您是否建议使用不同的组件将文本包装到JList中

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class JListPractice3 {

public static void main(final String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new JListPractice3();
        }
    });
}

public JListPractice3() {
    JFrame f = new JFrame("JList Practice");
    f.setResizable(true);
    f.setVisible(true);
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().setLayout(new BorderLayout());

    JPanel listPanel = this.buildJListPanel();

    f.add(listPanel);

    f.pack();
}

/**
 * Build JList panel
 * @return JPanel
 */
private JPanel buildJListPanel() {
    JPanel listPanel = new JPanel();
    listPanel.setLayout(new BorderLayout());

    JList jList = new JList(getData());
    jList.setVisibleRowCount(1);
    jList.setFixedCellHeight(50);
    jList.setFixedCellWidth(250);
    jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jList.setCellRenderer(new MyCellRenderer());

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.getViewport().setView(jList);
    listPanel.add(scrollPane, BorderLayout.CENTER);
    return listPanel;
}

/**
 * Get Alias data
 * @return String[]
 */
private String[] getData() {
    //TODO: remove hard code
    String[] data = {"123456789012345678901234567890123456789012345678901234567890123456789"
            + "012345678901234567890123456789012345678901234567890123456789012",
            "two two two two two two two two two two two twotwo two two two two two two two two two two twotwo two two end",
            "two two two two two two two two two two two twotwo two two two two two two two end",
            "five", "six"};
    return data;
}

private class MyCellRenderer implements ListCellRenderer {
    private JPanel p;
    private JPanel linePanel;
    private JLabel lineLabel;
    private JTextArea textArea;

    public MyCellRenderer() {
        p = new JPanel();
        p.setLayout(new BorderLayout());

        linePanel = new JPanel(new BorderLayout());
        linePanel.setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3));
        lineLabel = new JLabel("Test");
        linePanel.add(lineLabel, BorderLayout.NORTH);
        p.add(linePanel, BorderLayout.WEST);

        textArea = new JTextArea();
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        p.add(textArea, BorderLayout.CENTER);
    }

    @Override
    public Component getListCellRendererComponent(final JList list,
            final Object value, final int index, final boolean isSelected,
            final boolean hasFocus) {

        textArea.setText((String) value);
        int width = list.getWidth();
        // this is just to lure the ta's internal sizing mechanism into action
        if (width > 0) {
            textArea.setSize(width, Short.MAX_VALUE);
        }
        return p;
    }
  }
}
对于我们的请求,我的答复是取决于

如果您只需要visive效果,我认为这是一个很好的解决方案,
DefaultListCellRenderer
没有管理多行