問題描述
這簡(jiǎn)直把我逼瘋了.
我知道,要使用 JTable 更改表格單元格的格式,我必須使用自己的渲染器.但我似乎無法正確實(shí)施.
I know that, to change the formatting of table cells with JTable, I have to use my own renderer. But I cannot seem to implement this properly.
這是我目前的設(shè)置:
public class MyClass
{
public static void main(String args[])
{
JTable myTable = new JTable(10, 10);
myTable.setDefaultRenderer ([I dont know what to put here], new CustomRenderer());
}
}
class CustomRenderer extends DefaultTableCellRenderer
{
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);
// Formatting
return c;
}
}
setDefaultRenderer
的第一個(gè)參數(shù)需要用什么?API 只是說類".我不知道該放什么.
What do I need to use for the first parameter of setDefaultRenderer
? The API just says 'class'. I have no idea what to put there.
有人能用最簡(jiǎn)單的術(shù)語(yǔ)解釋一下我如何實(shí)現(xiàn)這個(gè)嗎?請(qǐng)?zhí)峁┮粋€(gè)示例,說明如何從 main()
方法中更改格式.
Could someone just explain, in the simplest of terms, how I go about implementing this? Please provide an example of how I can change the formatting from within the main()
method as well.
推薦答案
在 setDefaultRenderer
的第一個(gè)參數(shù)中,將要覆蓋的 Class 的 class literal渲染.即,如果您的數(shù)據(jù)包含所有字符串,則可以放置
In the first parameter for setDefaultRenderer
, put the class literal for the Class that you want to override rendering. I.e., if your data consist all of strings, you can put
myTable.setDefaultRenderer(String.class, new CustomRenderer());
如果您的數(shù)據(jù)還包含以 BigDecimal
或 Integer
作為類的值,則您必須為每種類類型(BigDecimal.class)多次調(diào)用該方法
或 Integer.class
在每種情況下).
If your data also consists of values with BigDecimal
or Integer
as classes, you have to invoke that method several times for each class type (BigDecimal.class
or Integer.class
in each case).
最后,要在渲染器中更改背景顏色:
And finally, to change the background color you do this in your renderer:
class CustomRenderer extends DefaultTableCellRenderer
{
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);
c.setBackground(new java.awt.Color(255, 72, 72));
return c;
}
}
如果您編寫的渲染器應(yīng)該適用于接口的所有類,您還需要修改 表模型的 getColumnClass
函數(shù) 并讓它返回所有實(shí)現(xiàn)該接口的對(duì)象的接口類:
If you write a renderer that should work for all classes of an interface, you will also need to modify the getColumnClass
function of your table model and let it return the interface class for all objects that implement this interface:
public Class<? extends Object> getColumnClass(int c) {
Object object = getValueAt(0, c);
if(object == null) {
return Object.class;
if(getValueAt(0, c) instanceof IColorable) {
return ICarPart.class;
} else {
return getValueAt(0, c).getClass();
}
}
這樣就可以為 IColorable.class 注冊(cè)一個(gè)渲染器,并且不需要為每個(gè)實(shí)現(xiàn)注冊(cè)一個(gè)單獨(dú)的渲染器.
With that one can register a renderer for IColorable.class and does not need to register a separate renderer for each implementation.
這篇關(guān)于更改 JTable 單元格顏色的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!