/*
* ButtonCellRenderer.java
*
* Created on 2006年10月12日, 下午12:34
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import java.awt.*;
import javax.swing.*;
/**
*
* @author Administrator
*/
public class ButtonCellRenderer extends JButton implements ListCellRenderer
{
public Component getListCellRendererComponent(
JList list,
Object value, // value to display
int index, // cell index
boolean isSelected, // is the cell selected
boolean cellHasFocus) // the list and the cell have the focus
{
String s = value.toString();
setText(s);
if (isSelected)
{
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
}
else
{
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setEnabled(list.isEnabled());
setFont(list.getFont());
setOpaque(true);
return this;
}
}
------------------------------------------------------------------------------------
/*
* TestFrame.java
*
* Created on 2006年10月12日, 下午12:38
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import java.awt.*;
import javax.swing.*;
/**
*
* @author Administrator
*/
public class TestFrame extends JFrame
{
private String[] data = {"one", "two", "three", "four"};
private JList dataList;
private JScrollPane jsp;
/** Creates a new instance of TestFrame */
public TestFrame()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dataList = new JList(data);
dataList.setCellRenderer(new ButtonCellRenderer());
jsp = new JScrollPane(dataList);
Container c = this.getContentPane();
c.add(jsp, BorderLayout.CENTER);
this.setVisible(true);
}
public static void main(String[] args){
new TestFrame();
}
}