java swing JTable中如何变字体颜色? 20
用DefaultTableCellRenderer定义格式,如何将某列所有大于零的单元格字体设置为红色?求代码!...
用DefaultTableCellRenderer定义格式,如何将某列所有大于零的单元格字体设置为红色?求代码!
展开
3个回答
展开全部
调用类MyRenderer
TableModel tableModel = new MyTableModel();
JTable table = new JTable(tableModel);
table.setDefaultRenderer(Integer.class,new MyRenderer());
MyRenderer类执行TableCellRenderer接口
class MyRenderer implements TableCellRenderer{
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column){
JLabel jl=new JLabel();
if((Integer)value>0){
jl.setForeground(Color.RED);
}
jl.setBackground(Color.WHITE);
jl.setOpaque(true);
jl.setText(value.toString());
return jl;
}
}
完整的程序如下:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
public class RedRowCellRenderer extends JFrame {
public RedRowCellRenderer(){
TableModel tableModel = new MyTableModel();
JTable table = new JTable(tableModel);
table.setDefaultRenderer(Integer.class,new MyRenderer());
getContentPane().add(new JScrollPane(table),BorderLayout.CENTER);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new RedRowCellRenderer();
}
}
class MyTableModel extends AbstractTableModel{
private Object[][] cells={{"some",0},{"any",1},{"even",2},
{"text",0},{"and",1},{"text",2}};
private String[] columnNames={"Column 1","Column 2"};
public String getColumnName(int c){
return columnNames[c];
}
public Class<?> getColumnClass(int c){
return cells[0][c].getClass();
}
public int getColumnCount(){
return cells[0].length;
}
public int getRowCount(){
return cells.length;
}
public Object getValueAt(int r,int c){
return cells[r][c];
}
public void setValueAt(Object obj,int r,int c){
cells[r][c] = obj;
}
}
class MyRenderer implements TableCellRenderer{
public Component getTableCellRendererComponent(JTable table,Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column){
JLabel jl=new JLabel();
if((Integer)value>0){
jl.setForeground(Color.RED);
}
jl.setBackground(Color.WHITE);
jl.setOpaque(true);
jl.setText(value.toString());
return jl;
}
}
TableModel tableModel = new MyTableModel();
JTable table = new JTable(tableModel);
table.setDefaultRenderer(Integer.class,new MyRenderer());
MyRenderer类执行TableCellRenderer接口
class MyRenderer implements TableCellRenderer{
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column){
JLabel jl=new JLabel();
if((Integer)value>0){
jl.setForeground(Color.RED);
}
jl.setBackground(Color.WHITE);
jl.setOpaque(true);
jl.setText(value.toString());
return jl;
}
}
完整的程序如下:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
public class RedRowCellRenderer extends JFrame {
public RedRowCellRenderer(){
TableModel tableModel = new MyTableModel();
JTable table = new JTable(tableModel);
table.setDefaultRenderer(Integer.class,new MyRenderer());
getContentPane().add(new JScrollPane(table),BorderLayout.CENTER);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new RedRowCellRenderer();
}
}
class MyTableModel extends AbstractTableModel{
private Object[][] cells={{"some",0},{"any",1},{"even",2},
{"text",0},{"and",1},{"text",2}};
private String[] columnNames={"Column 1","Column 2"};
public String getColumnName(int c){
return columnNames[c];
}
public Class<?> getColumnClass(int c){
return cells[0][c].getClass();
}
public int getColumnCount(){
return cells[0].length;
}
public int getRowCount(){
return cells.length;
}
public Object getValueAt(int r,int c){
return cells[r][c];
}
public void setValueAt(Object obj,int r,int c){
cells[r][c] = obj;
}
}
class MyRenderer implements TableCellRenderer{
public Component getTableCellRendererComponent(JTable table,Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column){
JLabel jl=new JLabel();
if((Integer)value>0){
jl.setForeground(Color.RED);
}
jl.setBackground(Color.WHITE);
jl.setOpaque(true);
jl.setText(value.toString());
return jl;
}
}
展开全部
调用类MyRenderer
TableModel tableModel = new MyTableModel();
JTable table = new JTable(tableModel);
table.setDefaultRenderer(Integer.class,new MyRenderer());
MyRenderer类执行TableCellRenderer接口
class MyRenderer implements TableCellRenderer{
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column){
JLabel jl=new JLabel();
if((Integer)value>0){
jl.setForeground(Color.RED);
}
jl.setBackground(Color.WHITE);
jl.setOpaque(true);
jl.setText(value.toString());
return jl;
TableModel tableModel = new MyTableModel();
JTable table = new JTable(tableModel);
table.setDefaultRenderer(Integer.class,new MyRenderer());
MyRenderer类执行TableCellRenderer接口
class MyRenderer implements TableCellRenderer{
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column){
JLabel jl=new JLabel();
if((Integer)value>0){
jl.setForeground(Color.RED);
}
jl.setBackground(Color.WHITE);
jl.setOpaque(true);
jl.setText(value.toString());
return jl;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
setColor()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询