久久久久久久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:獲取當前星期幾的值)
主站蜘蛛池模板: 成人黄色网址大全 | 男人天堂国产 | 国产 欧美 日韩 一区 | 99精品在线观看 | 日韩综合在线 | 青青草在线播放 | 国产 日韩 欧美 中文 在线播放 | 精品一区二区三区在线观看国产 | 国产精品视频一区二区三区 | 久久高清精品 | 亚洲在线一区 | 国产97久久| 91高清视频在线观看 | 老牛影视av一区二区在线观看 | 成年免费大片黄在线观看一级 | 天天拍天天射 | 综合久| 亚洲精品永久免费 | 久久精品一区二 | 99久久国产免费 | 国产98在线 | 免费, | 日本一区二区电影 | 成人国产综合 | 成人一区二区三区视频 | 日韩欧美中文字幕在线观看 | 久久久亚洲 | 国产精品日韩一区二区 | 高清黄色 | 成人网在线看 | 91成人免费看片 | 日韩五月天 | 国产a级黄色录像 | 日韩免费高清视频 | 欧美一级黄带 | 午夜影院在线观看 | a国产一区二区免费入口 | 亚洲精品日韩欧美 | 99视频 | 麻豆av一区二区三区久久 | 男人天堂网av | 免费av在线 |