問題描述
這簡直把我逼瘋了.
我知道,要使用 JTable 更改表格單元格的格式,我必須使用自己的渲染器.但我似乎無法正確實施.
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.
這是我目前的設置:
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
的第一個參數需要用什么?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.
有人能用最簡單的術語解釋一下我如何實現這個嗎?請提供一個示例,說明如何從 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
的第一個參數中,將要覆蓋的 Class 的 class literal渲染.即,如果您的數據包含所有字符串,則可以放置
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());
如果您的數據還包含以 BigDecimal
或 Integer
作為類的值,則您必須為每種類類型(BigDecimal.class)多次調用該方法
或 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;
}
}
如果您編寫的渲染器應該適用于接口的所有類,您還需要修改 表模型的 getColumnClass
函數 并讓它返回所有實現該接口的對象的接口類:
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 注冊一個渲染器,并且不需要為每個實現注冊一個單獨的渲染器.
With that one can register a renderer for IColorable.class and does not need to register a separate renderer for each implementation.
這篇關于更改 JTable 單元格顏色的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!