Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable JList Cell Selection Property

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;
}   
like image 342
Jonathandgorman Avatar asked Jul 28 '15 06:07

Jonathandgorman


People also ask

Which method is used to set the selection mode for the JList?

The selection mode can be changed on the selection model directly, or via JList 's cover method.

How do you remove an element from a JList in Java?

To actually remove the item, we use the syntax . remove(int); where the integer is the index of the item you wish to remove. That is how we add and remove things from a JList, and this concludes our tutorial on the JList.

How do you check if a JList is selected?

To check if there are any selected items, use the following: boolean res = ! list. isSelectionEmpty();

What is the purpose of JList in Java?

JList is part of Java Swing package . JList is a component that displays a set of Objects and allows the user to select one or more items . JList inherits JComponent class. JList is a easy way to display an array of Vectors .


1 Answers

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.

like image 136
morpheus05 Avatar answered Oct 25 '22 12:10

morpheus05