jlist check boxes setmnemonics java swing

93 views Asked by At

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

0

There are 0 answers