禁用 JList 单元格选择 属性

Disable JList Cell Selection Property

我正在尝试在 JList 中显示 stringsarray,然后使用 Java Swing 添加到 JPanel。我在 Jlists 中显示数据没有问题,但是我想 删除允许用户在 select 中的默认 属性 项目18=]。我试图简单地向用户显示数据。不幸的是,我无法找到允许我禁用此功能的 属性。我所指的 selection 属性 的示例可以在 1.

中看到

也许我使用了错误的 Java Swing 组件来显示此数据,但我研究了 JTextAreaJTable 等,并且 JList 似乎适合我的需要。非常感谢任何帮助。

public static JComponent createList(ArrayList inputData) {

    JPanel panel = new JPanel(false);
    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    panel.setBackground(Color.white);

    String[] displayData= {Data.get(0),Data.get(1),Data.get(2),Data.get(3)};
    JList<String> displayDataList= new JList<String>(displayData);
    displayDataList.setFont(sysDataList.getFont().deriveFont(Font.PLAIN)); 
    panel.add(displayDataList);

    return panel;
}   

JList 应该是可选择的。注意ListSelectionModel界面中没有NO_SELECTION

如果您只想显示项目,最好的选择是在面板中显示 JLabel 的列表,而不是使用 JList

我通过实施 NoSelection SelectionModel 实现了这一点。 SelectionModels 负责处理 selection 事件,参见 ListSelectionModel 示例:

public final class Main {

 public static void main(String[] args) {
   SwingUtilities.invokeLater(new Runnable() {
     public void run() {
       JFrame frame = new JFrame();
       frame.setSize(500, 500);
       frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       JList<Object> view = new JList<Object>();
       view.setSelectionModel(new NoSelectionModel());
       view.setListData(new Object[] {"String 1 ", "String 2"});
       frame.getContentPane().add(new JScrollPane(view));

       frame.setVisible(true);
     }
   });
 }

 private static class NoSelectionModel extends DefaultListSelectionModel {

   @Override
   public void setAnchorSelectionIndex(final int anchorIndex) {}

   @Override
   public void setLeadAnchorNotificationEnabled(final boolean flag) {}

   @Override
   public void setLeadSelectionIndex(final int leadIndex) {}

   @Override
   public void setSelectionInterval(final int index0, final int index1) { }
 }
} 

你必须记住:如果用户不能select任何东西,他也不能复制粘贴任何东西。此外,键盘滚动行为有点奇怪。

在 Java 的较新版本中,当 setEnabled() 设置为 false 时,选择也会被阻止。尽管当面板 (JScrollPane) 中有一个 JList 并且您尝试递归禁用其中的所有元素时有一个特殊性。 这里有一个方法可以帮助你:

public void setComponentStatus(JComponent panel, Boolean status) {
        try {
            panel.setEnabled(status);
            Component[] components = panel.getComponents();
            
            for (Component component : components) {
                if (component instanceof JPanel) {
                    setComponentStatus((JComponent) component, status);
                } else {
                    if (component instanceof JScrollPane)
                        ((JScrollPane) component).getViewport().getView().setEnabled(status);
                    component.setEnabled(status);
                }
            }
            
        } catch (Exception e) {
            throw new RuntimeException("Impossible to complete change the status of the panel: " + panel + " or one of its components to status:" + status, e);
        }
    }

我想指出的核心特征是:

((JScrollPane) component).getViewport().getView().setEnabled(status);