簡體   English   中英

禁用 JList 單元格選擇屬性

[英]Disable JList Cell Selection Property

我試圖在JList中顯示一個strings array ,然后使用Java Swing將其添加到JPanel中。 我在Jlists中顯示數據沒有問題,但是我想刪除允許用戶在Jlist中選擇項目的默認屬性 我試圖簡單地向用戶顯示數據。 不幸的是,我無法找到允許我禁用此功能的屬性。 我所指的選擇屬性的示例可以在1中看到。

也許我使用了錯誤的Java Swing組件來顯示這些數據,但是我研究了JTextAreaJTable等,並且JList似乎符合我的需求。 任何幫助深表感謝。

公共靜態 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示例:

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

你必須記住:如果用戶不能選擇任何東西,他也不能復制粘貼任何東西。 此外,鍵盤滾動行為有點奇怪。

JList應該是可選擇的。 請注意ListSelectionModel接口中缺少NO_SELECTION

如果您只想顯示項目,最好的選擇是在面板中顯示JLabel列表而不是使用JList

在較新版本的 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM