skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Thursday, September 20, 2012

Scrollable JList in Java Swing

Posted by Admin at 12:04 PM – 0 comments
 

Using this code snippet, we can create scrollable List box in Java Swing


                JScrollPane scrollPane = new JScrollPane();
  scrollPane.setBounds(22, 24, 265, 155);
  panel_2.add(scrollPane);
  
  // Create DefualtListModel and JList
  private DefaultListModel roomListModel = new DefaultListModel();
  private JList roomsJList;
  
  // Add data to the JList using DefaultListModel
  roomsJList = new JList(roomListModel);
  scrollPane.setViewportView(roomsJList);
  
  //Setting auto scroll to true
  roomsJList.setAutoscrolls(true);

  
  // Adding value changed Action listener to List items.
  roomsJList.addListSelectionListener(new ListSelectionListener() {
   public void valueChanged(ListSelectionEvent arg0) {
    if(roomsJList.getSelectedValue() !=null){
     // Add code here which will run on List Selection 
    }

   }
  });



Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger