久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

更改 JTable 單元格顏色

Changing JTable cell color(更改 JTable 單元格顏色)
本文介紹了更改 JTable 單元格顏色的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號(hào)..

這簡(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ù)還包含以 BigDecimalInteger 作為類的值,則您必須為每種類類型(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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時(shí)間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉(zhuǎn)換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉(zhuǎn)換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當(dāng)前星期幾的值)
主站蜘蛛池模板: 激情欧美一区二区三区 | 国产激情亚洲 | 综合久久综合久久 | 国产h视频 | 亚洲精品一区二区三区中文字幕 | 久久福利网站 | 欧美日韩久久 | 在线视频国产一区 | 国产精品久久av | www.国产精 | 一本色道精品久久一区二区三区 | 亚洲人成人一区二区在线观看 | a视频在线观看 | 成人在线视频观看 | 日韩一区二区免费视频 | 国产日韩视频 | 午夜精品网站 | 色播久久 | 欧美黄色一区 | 久久久久国产一区二区三区 | 久久国产精品视频免费看 | 午夜资源 | 亚洲毛片在线观看 | 亚洲精品日韩综合观看成人91 | av小说在线| 欧美激情在线精品一区二区三区 | 国产精品视频一区二区三区 | 免费在线观看成人 | 国产激情精品一区二区三区 | 91资源在线观看 | 国产精品美女久久久久aⅴ国产馆 | 天天操网| 国产激情一区二区三区 | 亚洲精品91 | av黄色在线 | 国产区精品 | www.中文字幕 | 一片毛片 | 日本久久网 | 日韩精品免费一区 | 欧美一级视频 |