日期:2014-05-20 浏览次数:20879 次
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.TableCellRenderer; class EvenOddRenderer implements TableCellRenderer { public static final DefaultTableCellRenderer DEFAULT_RENDERER = new DefaultTableCellRenderer(); public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Component renderer = DEFAULT_RENDERER.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column); Color foreground, background; if (isSelected) { foreground = Color.YELLOW; background = Color.GREEN; } else { if (row % 2 == 0) { foreground = Color.BLUE; background = Color.WHITE; } else { foreground = Color.WHITE; background = Color.BLUE; } } renderer.setForeground(foreground); renderer.setBackground(background); return renderer; } } public class ResizeTable { public static void main(String args[]) { final Object rowData[][] = { { "1", "one", "I" }, { "2", "two", "II" }, { "3", "three", "III" } }; final String columnNames[] = { "#", "English", "Roman" }; final JTable table = new JTable(rowData, columnNames); JScrollPane scrollPane = new JScrollPane(table); table.setDefaultRenderer(Object.class, new EvenOddRenderer()); JFrame frame = new JFrame("Resizing Table"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); } }
------解决方案--------------------
渲染器:
比如说一个Grid 中的,我要渲染Grid 中的ROW信息
1。给Grid 一个Model
2。写个类实现Grid 以前渲染Row中的接口
3。进行渲染
原理是:
通过Model 中的每一条数据进行的处理,其中Model 就是一个List<T>
要渲染每次取一个T;
与这个差不多:
for(T t :List<T>)
{
........
}