0

I am thinking if it is possible to put or setmnemonics on a jlist item so far i search for jlist tutorials and almost all of them is having the event mouse click so i am thinking if it is possible to put mnemonics for each item in the list i am working on this code right now

public class CheckList
{  
   public static void main(String args[])
   {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Create a list containing CheckListItem's
      
      JList list = new JList(new CheckListItem[] {
            new CheckListItem("apple"), 
            new CheckListItem("orange"), 
            new CheckListItem("mango"), 
            new CheckListItem("paw paw"), 
            new CheckListItem("banana")});
      
      // Use a CheckListRenderer (see below) 
      // to renderer list cells
      
      list.setCellRenderer(new CheckListRenderer());
      list.setSelectionMode(
         ListSelectionModel.SINGLE_SELECTION);
      
      // Add a mouse listener to handle changing selection
      
      list.addMouseListener(new MouseAdapter()
      {
         public void mouseClicked(MouseEvent event)
         {
            JList list = (JList) event.getSource();
            
            // Get index of item clicked
            
            int index = list.locationToIndex(event.getPoint());
            CheckListItem item = (CheckListItem)
               list.getModel().getElementAt(index);
            
            // Toggle selected state
            
            item.setSelected(! item.isSelected());
            
            // Repaint cell
            
            list.repaint(list.getCellBounds(index, index));
         }
      });   

      frame.getContentPane().add(new JScrollPane(list));
      frame.pack();
       frame.setVisible(true);
   } 
}

from this link

Any idea or suggestion is appreciated

Community
  • 1
  • 1
Brownman Revival
  • 3,620
  • 9
  • 31
  • 69
  • Short answer, no. I'm also worried about you `MouseListener` as well...it would be kind of counter intuitive... – MadProgrammer Nov 27 '14 at 04:21
  • @MadProgrammer Is there a way to create checkbox that are in group but can select or check multiple items?i am running out of idea on how to make this one work – Brownman Revival Nov 27 '14 at 04:25
  • That would come down to you model... – MadProgrammer Nov 27 '14 at 04:27
  • @MadProgrammer can you check this [link](http://stackoverflow.com/questions/27146086/select-many-checkbox-in-a-checkboxgroup-java-swing). and further explain the idea you have if you can provide a snippet that would be better – Brownman Revival Nov 27 '14 at 04:30
  • (if is there focus owner in window) then you have look at (and to override) JList#getNextMatch(standard searching in JList implemented in API), then you can to create an mnemonics (note still, it should be limited to only one char) form sequence of chars in the JList view (e.g. if you override this method correctly, then you can to select all Items, startwith or contains specifics char e.g. mnemonics "a" should be select "apple" or search for all Items contains "a", faster an safe) – mKorbel Nov 27 '14 at 06:01
  • @mKorbel it is a bit complicated for me to understand can you provide a snippet?or a sample link that does what you are trying to say? – Brownman Revival Nov 27 '14 at 07:02
  • override KeyBindings that returns (e.g. CTRL + char / whitespace char to avoiding concurency by/from users input), [then ....](http://www.coderanch.com/t/617005/GUI/java/JList-modifying-keyboard-search) – mKorbel Nov 27 '14 at 07:09

0 Answers0