import java.awt.Color
import java.awt.Component
import java.awt.Container
import java.text.DateFormat
import java.util.ArrayList
import java.util.Calendar
import java.util.List
import javax.swing.JFrame
import javax.swing.JScrollPane
import javax.swing.JTable
import javax.swing.table.DefaultTableCellRenderer
import javax.swing.table.DefaultTableModel
public class MainFrame extends JFrame {
private static final long serialVersionUID = 1L
private List<String> colNames
private List<List<Object>> data
private JTable table
private Calendar shieldCalendar
public MainFrame() {
super()
initData()
Container c = getContentPane()
table = new JTable()
table.setModel(new MTableModel())
table.getColumnModel().getColumn(0).setCellRenderer(
new MTableCellRenderer())
table.getColumnModel().getColumn(1).setCellRenderer(
new MTableCellRenderer())
table.getColumnModel().getColumn(2).setCellRenderer(
new MTableCellRenderer())
c.add(new JScrollPane(table))
setSize(600, 400)
setDefaultCloseOperation(EXIT_ON_CLOSE)
setLocationRelativeTo(null)
setVisible(true)
}
private void initData() {
colNames = new ArrayList<String>()
colNames.add("ID")
colNames.add("Name")
colNames.add("Date")
data = new ArrayList<List<Object>>()
for (int i = 0 i < 20 i++) {
List<Object> record = new ArrayList<Object>()
record.add(String.valueOf(i + 1))
record.add("item" + (i + 1))
Calendar c = Calendar.getInstance()
c.set(2010, 3, i + 1)
record.add(c)
data.add(record)
}
shieldCalendar = Calendar.getInstance()
shieldCalendar.set(2010, 3, 10)
}
private class MTableModel extends DefaultTableModel {
private static final long serialVersionUID = 1L
@Override
public int getColumnCount() {
return colNames.size()
}
@Override
public int getRowCount() {
return data.size()
}
@Override
public String getColumnName(int column) {
return colNames.get(column)
}
@Override
public Object getValueAt(int row, int column) {
return data.get(row).get(column)
}
@Override
public boolean isCellEditable(int row, int column) {
return false
}
}
private class MTableCellRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Component c = super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column)
if (value instanceof Calendar) {
Calendar cal = (Calendar) value
if (cal.after(shieldCalendar)) {
setBackground(Color.red)
} else if (cal.equals(shieldCalendar)) {
setBackground(Color.blue)
} else {
setBackground(Color.green)
}
DateFormat f = DateFormat.getDateInstance(DateFormat.LONG)
String s = f.format(cal.getTime())
setText(s)
} else {
setBackground(Color.cyan)
}
return c
}
}
public static void main(String[] args) {
new MainFrame()
}
}
在 Java 中,可以使用 TableCellRenderer 和 TableCellEditor 接口来设置表格单元格的格式。其中,TableCellRenderer 用于设置单元格在表格中的展示效果,而 TableCellEditor 则用于设置单元格在被编辑时的效果。这两个接口都有一个方法 getTableCellRendererComponent() 或 getTableCellEditorComponent(),可以通过这些方法设置单元格的样式、字体、颜色等属性。
具体实现时,需要自定义一个类,实现 TableCellRenderer 或 TableCellEditor 接口,并重写其方法来进行单元格样式的设置。然后将该类对象赋给要设置的表格单元格即可。例如:
class MyCellRenderer
implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
Component c = DefaultTableCellRenderer.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column)// 调用默认渲染器
c.setBackground(Color.YELLOW)// 设置背景色为黄色
return c
}
}
// 使用 MyCellRenderer 对象对表格第二列进行单元格样式设置
table.getColumnModel().getColumn(1).setCellRenderer(new MyCellRenderer())
上述代码中,自定义了一个 MyCellRenderer 类,并实现了 TableCellRenderer 接口中的 getTableCellRendererComponent() 方法来设置单元格样式。最后,使用 table.getColumnModel().getColumn(columnIndex).setCellRenderer(renderer) 来将 MyCellRenderer 对象应用到相应的表格列中。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)