7

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.

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.

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;
}   
Jonathandgorman
  • 102
  • 1
  • 10
  • Show your code which you tried to solve this. – Prabhat Singh Jul 28 '15 at 07:00
  • Also there are many solutions on this very site, like this duplicate http://stackoverflow.com/questions/17863780/make-jlist-values-unselectable that already has duplicates. – milez Jul 28 '15 at 07:04

3 Answers3

7

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.

morpheus05
  • 4,772
  • 2
  • 32
  • 47
  • – That has worked perfectly and is much appreciated. I simply had to call the following: displayDataList.setSelectionModel(new NoSelectionModel()); – Jonathandgorman Jul 28 '15 at 07:13
  • This solution doesn't work if you hold the touch Ctrl while selecting items.. Is there a way to prevent that ? – Jack' Nov 08 '17 at 13:03
  • 1
    Yes, there is, the solution is in the comment of the accepted answer of this post : https://stackoverflow.com/questions/17863780/make-jlist-values-unselectable --- we need to set the mode SINGLE_INTERVAL_SELECTION to the list. – Jack' Nov 08 '17 at 14:11
0

In newer versions of Java, the selection is also blocked when setEnabled() is set to false. Although there is a particularity when you have a JList inside a panel (JScrollPane) and you try to recursively disable all elements inside of it. Here is a method that will help you with that:

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

The core feature I'm trying to point out is this:

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

White_King
  • 748
  • 7
  • 21
-1

JList is supposed to be selectable. Note the absence of NO_SELECTION in ListSelectionModel interface.

If you want to just display the items, the best option is to display a list of JLabels in a panel rather than using JList.

Dakshinamurthy Karra
  • 5,353
  • 1
  • 17
  • 28
  • 4
    Really ? You would even use hundreds of JLabels if you have a considerable ArrayList of objects to display ? – Jack' Nov 08 '17 at 12:53