An example of the JList component in action : List « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Swing JFC » ListScreenshots 
An example of the JList component in action
An example of the JList component in action

/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
*/
// SwingListExample.java
//An example of the JList component in action. This program uses a custom
//renderer (BookCellRenderer.java) to show a list of books with icons of their
//covers.
//

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;

public class SwingListExample extends JPanel {

  private BookEntry books[] {
      new BookEntry("Ant: The Definitive Guide""1.gif"),
      new BookEntry("Database Programming with JDBC and Java",
          "2.gif"),
      new BookEntry("Developing Java Beans""3.gif"),
      new BookEntry("Developing JSP Custom Tag Libraries",
          "4.gif"),
      new BookEntry("Java 2D Graphics""4.gif"),
      new BookEntry("Java and XML""5.gif"),
      new BookEntry("Java and XSLT""1.gif"),
      new BookEntry("Java and SOAP""2.gif"),
      new BookEntry("Learning Java""3.gif") };

  private JList booklist = new JList(books);

  public SwingListExample() {
    setLayout(new BorderLayout());
    JButton button = new JButton("Print");
    button.addActionListener(new PrintListener());

    booklist = new JList(books);
    booklist.setCellRenderer(new BookCellRenderer());
    booklist.setVisibleRowCount(4);
    JScrollPane pane = new JScrollPane(booklist);

    add(pane, BorderLayout.NORTH);
    add(button, BorderLayout.SOUTH);
  }

  public static void main(String s[]) {
    JFrame frame = new JFrame("List Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new SwingListExample());
    frame.pack();
    frame.setVisible(true);
  }

  // An inner class to respond to clicks on the Print button
  class PrintListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      int selected[] = booklist.getSelectedIndices();
      System.out.println("Selected Elements:  ");

      for (int i = 0; i < selected.length; i++) {
        BookEntry element = (BookEntrybooklist.getModel()
            .getElementAt(selected[i]);
        System.out.println("  " + element.getTitle());
      }
    }
  }
}

class BookEntry {
  private final String title;

  private final String imagePath;

  private ImageIcon image;

  public BookEntry(String title, String imagePath) {
    this.title = title;
    this.imagePath = imagePath;
  }

  public String getTitle() {
    return title;
  }

  public ImageIcon getImage() {
    if (image == null) {
      image = new ImageIcon(imagePath);
    }
    return image;
  }

  // Override standard toString method to give a useful result
  public String toString() {
    return title;
  }
}

class BookCellRenderer extends JLabel implements ListCellRenderer {
  private static final Color HIGHLIGHT_COLOR = new Color(00128);

  public BookCellRenderer() {
    setOpaque(true);
    setIconTextGap(12);
  }

  public Component getListCellRendererComponent(JList list, Object value,
      int index, boolean isSelected, boolean cellHasFocus) {
    BookEntry entry = (BookEntryvalue;
    setText(entry.getTitle());
    setIcon(entry.getImage());
    if (isSelected) {
      setBackground(HIGHLIGHT_COLOR);
      setForeground(Color.white);
    else {
      setBackground(Color.white);
      setForeground(Color.black);
    }
    return this;
  }
}

           
       
Related examples in the same category
1. An example of JList with a DefaultListModelAn example of JList with a DefaultListModel
2. A variation on SimpleListA variation on SimpleList
3. A simple example of a JList object built from an array of StringsA simple example of a JList object built from an array of Strings
4. A graphical list selection monitorA graphical list selection monitor
5. Test of the DragGesture classes and JList to see if weTest of the DragGesture classes and JList to see if we
6. Dual JList Dual JList
7. ComplexRenderingSample: ListCellRendererComplexRenderingSample: ListCellRenderer
8. List Sample DemoList Sample Demo
9. Dual Sample: JList and ComboBoxDual Sample: JList and ComboBox
10. Selecting JList SampleSelecting JList Sample
11. Triple List SampleTriple List Sample
12. List with and without ScrollPane List with and without ScrollPane
13. SizingSamples: simple List SizingSamples: simple List
14. List: Shared Data SampleList: Shared Data Sample
15. List Data Event DemoList Data Event Demo
16. How to use the list componentHow to use the list component
17. Create list from list modelCreate list from list model
18. How to create list cell rendererHow to create list cell renderer
19. React to list selection actionReact to list selection action
20. Construct the list componentConstruct the list component
21. Tab list rendererTab list renderer
22. List with textfield inputList with textfield input
23. Sharing a Model between a JList and JComboBoxSharing a Model between a JList and JComboBox
24. Demonstrate Swing ScrollingListDemonstrate Swing ScrollingList
25. Demonstrate Swing JList ListModelDemonstrate Swing JList ListModel
26. Weak ListModel
27. ListModel DemoListModel Demo
28. ModifyModelSample: ListModel DemoModifyModelSample: ListModel Demo
29. ArrayList with a ListModel for ease of use
30. Drag and Drop:JList and ListDrag and Drop:JList and List
w___w___w___._j__ava2___s__._co_m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.