-1

I've looked for the answer to this on SO and elsewhere but can't find an answer. Is it possible to select an entire jlist rather than a specific item? For example if a user were to click on the white area of a jlist I could call a method which would put that jlist's name in a HashMap or call another method to alter the list as a whole. Hopefully the code gives you an idea. Any info would be helpful. Thanks.

final JList list_1 = new JList(list1);
    list_1.addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(FocusEvent arg0) {
            //make entire list disappear when selected 
            list_1.setVisible(false);
        }
    });
    GridBagConstraints gbc_list_1 = new GridBagConstraints();
    gbc_list_1.gridheight = 3;
    gbc_list_1.insets = new Insets(0, 0, 5, 5);
    gbc_list_1.fill = GridBagConstraints.BOTH;
    gbc_list_1.gridx = 5;
    gbc_list_1.gridy = 0;
    panel.add(list_1, gbc_list_1);
V.C.4
  • 19
  • 1
  • 3
  • 1
    *"Is it possible to select an entire jlist rather than a specific item?"* Yes. Check the [Java Docs](https://docs.oracle.com/javase/8/docs/api/javax/swing/JList.html#setSelectedIndices-int:A-).. For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Jan 22 '15 at 15:12
  • @AndrewThompson I'm sorry I could've explained myself better. I want to make a JList disappear when it's selected (whitespace, not one of it's selectable items). Is that possible? – V.C.4 Jan 22 '15 at 15:53

1 Answers1

1

Is it possible to select an entire jlist rather than a specific item?

Yes, as long as the ListSelectionModel allows you to do that. See Selecting Items in a List.

For example if a user were to click on the white area of a jlist...

Better than that is to have a "Select All" kind of button and work with the ListSelectionModel so you don't have to mess with the focus subsystem at all. For example:

final JList list = new JList();
...
Action selectAllAction = new AbstractAction("Select All") {
   @Override
   public void actionPerformed(ActionEvent e) {
       list.getSelectionModel().setSelectionInterval(0, list.getModel().getSize() - 1);
    }
};
...
JButton selectAllButton = new JButton(selectAllAction);

...I could call a method which would put that jlist's name in a HashMap or call another method to alter the list as a whole.

I don't have any idea what do you mean alter the list as whole or why do you want to put that jlist's name in a HashMap, so I'm not able to help you any further. For better help please include a SSCE

dic19
  • 17,821
  • 6
  • 40
  • 69
  • Sorry my explanation was not good. I want to set the Jlist's visibility to false when it is selected, not when someone clicks on an item in the Jlist (then it should behave normally). Is that possible? – V.C.4 Jan 22 '15 at 15:56
  • 2
    *"I want to set the Jlist's visibility to false when it is selected"* Sounds like yet another unusable GUI in the making. What you are suggesting is not the 'path of least surprise' for the end user, so for their sake I hope you change your mind (or just don't achieve that goal). – Andrew Thompson Jan 22 '15 at 16:03
  • 1
    *"Is that possible?"* I hope not. Let's say the user mistakenly clicks over the white area and hides the list. How does the user make the list visible again? Now, let's say you figure out a way to make the list visible. How is it useful to the end user? Just makes no sense to me. I suspect there is a [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) here. Probably if you state the use case you need to implement in a clearer way you'll get better help. – dic19 Jan 23 '15 at 11:57
  • Perhaps you are looking for something like [collapsible panes](http://stackoverflow.com/a/25703228/1795530) that allow dinamically collapse-expand panels. Again it's just a wild guess until you clearly state what you want to achieve with this. @V.C.4 – dic19 Jan 23 '15 at 12:02