禁用JList单元格选择属性 [英] Disable JList Cell Selection Property

查看:87
本文介绍了禁用JList单元格选择属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 JList 字符串数组 c $ c>,然后使用 Java Swing 将其添加到 JPanel 中。我在显示 Jlists 中的数据时没有问题,但是我想删除允许用户选择<$ c中的项目的默认属性。 $ c> Jlist 。我试图将数据简单地显示给用户。不幸的是,我无法找到允许我禁用此功能的属性。我所指的选择属性的示例可以在 1 中看到。

I am attempting to display an array of strings in a JList, which is then added to a JPanel using Java Swing. I am not having a problem displaying the data in the Jlists, however I would like to remove the default property that allows a user to select items in the Jlist. I am attempting to simply display the data to the user. Unfortunately I am unable to locate the property that would allow me to disable this feature. A example of the selection property that I am referring to can be seen here in 1.

也许我使用了错误的 Java Swing 组件来显示此数据,但是我研究了 JTextArea JTable 等,而 JList 似乎很适合我的需求。

Perhaps I am using the wrong Java Swing component to display this data, but I have research JTextArea, JTable, etc., and the JList seems to fit my needs. Any help is much appreciated.

公共静态JComponent createList(ArrayList inputData){

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


推荐答案

我通过实现一个NoSelection SelectionModel。 SelectionModels负责处理选择事件,请参见 ListSelectionModel 示例:

I achived this by implementing a NoSelection SelectionModel. SelectionModels are responsible for handling selection events, see ListSelectionModel Example:

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) { }
 }
} 

您必须记住:如果用户无法选择任何内容,那么他​​也将无法复制粘贴任何内容。此外,键盘滚动行为并不奇怪。

You have to remember: If the user cannot select anything he can also not copy paste anything. Furthermore, the keyboard scroll behavior is little strange.

这篇关于禁用JList单元格选择属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆