1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.HeadlessException;
import java.awt.Dimension;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
/**
* Created by IntelliJ IDEA.
* User: bebe
* Date: 20-Jun-2006
* Time: 19:12:20
* To change this template use File | Settings | File Templates.
*/
public class ListRenderer extends JFrame {
public ListRenderer() throws HeadlessException {
Vector<String> v = new Vector<String>();
for (int i = 0; i < 50; i++) {
v.add(new StringBuilder().append("nom ").append(i).append("\tprenom ").append(i).toString());
}
JList myList = new JList(v);
myList.setCellRenderer(new MyListRenderer());
add(new JScrollPane(myList));
setPreferredSize(new Dimension(400, 300));
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
public void run() {
ListRenderer l = new ListRenderer();
l.pack();
l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l.setVisible(true);
//To change body of implemented methods use File | Settings | File Templates.
}
};
EventQueue.invokeLater(runnable);
}
}
class MyListRenderer extends JTextArea implements ListCellRenderer {
public MyListRenderer() {
setTabSize(8);
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
setText((String) value);
int selectedIndex = list.getSelectedIndex();
if (index == selectedIndex) {
// {exec:UIDefaults.getListKeys("List")}
//List.dropLineColor
//List.font
//List.foreground
//ListUI
//List.timeFactor
//List.background
//List.selectionForeground
//List.focusCellHighlightBorder
//List.cellRenderer
//List.selectionBackground
//List.focusInputMap
//List.focusInputMap.RightToLeft
//List.dropCellBackground
setBackground((Color)UIManager.get("List.selectionBackground"));
} else {
setBackground((Color)UIManager.get("List.background"));
}
return this; //To change body of implemented methods use File | Settings | File Templates.
}
} |
Partager