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

更改 JTable 單元格顏色

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

問題描述

限時送ChatGPT賬號..

這簡直把我逼瘋了.

我知道,要使用 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());

如果您的數據還包含以 BigDecimalInteger 作為類的值,則您必須為每種類類型(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模板網!

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

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
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 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: www国产精 | 欧美精品久久 | 欧美高清免费 | 天天看天天操 | 毛片一级黄色 | 日韩欧美在线一区 | 999久久久 | 久在草| 日韩精品区 | 亚洲国产一区二区三区, | 精品久久久久久久久久久院品网 | 日韩在线观看网站 | 日韩精品成人av | 成人精品免费视频 | 丝袜美腿一区 | 成人午夜免费福利视频 | 日本不卡高字幕在线2019 | 一级毛片在线播放 | 亚洲视频一区在线 | 国产视频久久久 | 亚洲视频在线一区 | 韩国主播午夜大尺度福利 | 啪啪网页| 免费在线观看黄网站 | av片网| 一区在线观看 | av中文字幕在线观看 | 可以免费观看的av片 | 日韩网站在线观看 | 皇色视频在线 | 亚洲免费在线播放 | 91观看 | 在线观看免费黄色片 | 免费在线色| 国产高清在线视频 | 91久久爽久久爽爽久久片 | 亚洲一区二区三区四区五区中文 | 麻豆亚洲| 午夜电影福利 | 久久精品国产久精国产 | 黑人成人网 |