Saturday, August 24, 2013

JList

JList represents a list of items. You can select one or a number of items from the list. JList can generate the list selection event so you need to implement the ListSelectionListener interface to receive the event. This interface has a method called valueChanged(ListSelectEvent e) that needs to be overridden. Below are the summary of JList constructors and its useful methods.

Constructors of the JList class:
-JList()
creates an empty list.
-JList(ListModel model)
creates a list with a list mode that maintains the list items.
-JList(Object[] items)
creates a list that its items are taken from an array of items array.
-JList(vector<?> items)
creates a list that its items are taken from the vector items.

Useful methods of the JList class:
-getSelectedIndex():int
returns the index of the selected item.
-getSelectedIndices():int[]
returns the array of selected indices.
-getSelectedValue():Object
returns the selected item.
-getSelectedValues():Object[]
returns the array of selected items.
-isSelectedIndex(int index):boolean
returns true if the item at the specified index is selected.
-setCellRenderer(ListCellRender render):void
specifies the cell render for the list.
-setFixedCellHeight(int h):void
specifies a value to be the height of every cell.
-setFixedCellWidth(int w):void
specifies a value to be the width of every cell.
-setListData(Object[] items):void
specifies the data items of the list.
-setSelectedIndex(int index):void
selects a single cell at the specified index.
-setSelectedIndices(int[] indices):void
selects multiple cells at the indices.
-setSelectedValue(Object item, boolean scroll):void
selects an item from the list.

Other useful methods are not in the JList class. They are in the DefaultListModel class. It is a list model class that implements the ListModel interfaces.
-AddElement(Object item):void
adds an item to end of the list.
-clear():void
removes all items of the list.
-contains(Object item):boolean
Tests whether or not an item is in the list.
-getElementAt(int index):Object
returns the element at the specified index in the list.
-getSize():int
returns the number of items in the list.
-removeElementAt(int index):void
delete the item at the specified index of the list.
-removeElement(Object item):void
delete the specified item of the list.
-toArray():Object[]
converts the list to an array of objects.


Example:
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

class JListShow extends JFrame implements ListSelectionListener{
JLabel lbl;
JListShow(String title){
setTitle(title);
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contpane=getContentPane();
contpane.setLayout(new GridLayout(1,2));
String[] fname=readAvailableFonts();
DefaultListModel model=new DefaultListModel();
for(String name:fname){
model.addElement(name);
}
JList list=new JList(model);
list.addListSelectionListener(this);
list.setVisibleRowCount(10);
JScrollPane scroll=new JScrollPane(list);
contpane.add(scroll);
lbl=new JLabel();
contpane.add(lbl);
setVisible(true);
}

public void valueChanged(ListSelectionEvent e){
JList lst=(JList)e.getSource();
String selectedFontName=(String)lst.getSelectedValue();
lbl.setText(selectedFontName);
lbl.setFont(new Font(selectedFontName, Font.PLAIN,20));
}

public String[] readAvailableFonts(){
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] f=ge.getAvailableFontFamilyNames();
return f;
}


}

public class JFrameSwing {
public static void main(String[] args){
new JListShow("JList");
}
}

JList Swing

ListCellRenderer interface specifies components that will be used to render the cells of the list (created by JList class). For example, a class that implements the ListCellRender interface can specify labels to render the cells of the list so you are able to apply the font name, font size, text color, and background color, and image icon to the cells of the list as you do with the labels (created by JLabel class).

Example:
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

class ListCellRendererShow extends JFrame implements ListSelectionListener{
JLabel lbl;
ListCellRendererShow(String title){
setTitle(title);
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contpane=getContentPane();
contpane.setLayout(new GridLayout(1,2));
String[] fname=listDate();
DefaultListModel model=new DefaultListModel();
for(String name:fname){
model.addElement(name);
}
JList list=new JList(model);
list.addListSelectionListener(this);
list.setVisibleRowCount(10);
list.setCellRenderer(new CellRenderer());
JScrollPane scroll=new JScrollPane(list);
contpane.add(scroll);
lbl=new JLabel();
contpane.add(lbl);
setVisible(true);
}

public void valueChanged(ListSelectionEvent e){
JList lst=(JList)e.getSource();
String selectedFontName=(String)lst.getSelectedValue();
lbl.setText(selectedFontName);
lbl.setFont(new Font(selectedFontName, Font.PLAIN,20));
}

public String[] listDate(){
String[] data={"Abandon","Abate","abdomen","babble","baboon","baby"};
return data;
}

class CellRenderer extends JLabel implements ListCellRenderer {
    public CellRenderer() {
        setOpaque(true);
    }

    public Component getListCellRendererComponent(JList list,
                                                  Object value,
                                                  int index,
                                                  boolean isSelected,
                                                  boolean cellHasFocus) {
   
        setText(value.toString());
        setFont(new Font("Tahoma", Font.PLAIN,15));
        Color backcolor;
        Color forecolor;      
        if (isSelected) {
            backcolor = Color.YELLOW;
            forecolor = Color.RED;
            setIcon(new ImageIcon("d:/iconlist.png"));
        } else {
            backcolor = Color.WHITE;
            forecolor = Color.BLACK;
            setIcon(null);
        }
        setBackground(backcolor);
        setForeground(forecolor);      
        return this;
    }
}

}

public class JFrameSwing {
public static void main(String[] args){
new ListCellRendererShow("ListCellRenderer");
}
}

ListCellRender Swing

1 comment:

  1. Hi there, I found your blog via Google while searching for such kinda informative post and your post looks very interesting for me.writers have great importance in the writing company because they are the ones who solve customer's problems with respect to their needs. write my essay

    ReplyDelete