java 板


LINE

※ 引述《prjryu (再努力一点)》之铭言: : 在此附上我的 table cell renderer 的码 : public class MainWindow extends javax.swing.JFrame : { ... : public static double[] dbLow = new double[11]; : public static double[] dbHeigh = new double[11]; : ... : jTable.setDefaultRenderer(Object.class,new ColorTable()); : ... : } : class ColorTable extends DefaultTableCellRenderer : { : private static final long serialVersionUID = 1L; : public Component getTableCellRendererComponent(JTable table,Object : value,boolean isSelected,boolean hasFocus,int row,int col) : { : super.getTableCellRendererComponent(table,value,isSelected,hasFocus, : row,col); : Color foreground, background; : double dbValue = 0; : dbValue = Double.parseDouble( MainWindow.jTable.getValueAt( row, : col).toString()); : if( dbValue < MainWindow.dbLow[col] || dbValue > MainWindow.dbHeigh[col] ) : { : foreground = Color.WHITE; : background = Color.RED; : } : else : { : foreground = Color.BLACK; : background = Color.GRAY; : } : setForeground(foreground); : setBackground(background); : //System.out.println( "COLOR cell: " + row + ", " + col ); : return this; : } : } : 不确定问题是否在这边,因为我的认知是table开到哪cell就会画到哪才对... : 所以在这边我有试着印出有被我改颜色的cell的row和column号, : 有发现JTable里被我塞double的obj cell没有印位置出来 : (正常来说是要通通会印出来才对) 拼凑所有你提供的码来看,有一些部分要厘清。 1) 你若使用 DefaultTableModel,这个 model 会把所有 column 都当作 Object type的数据,当你为 Object type 设定 default cell renderer 後,所有的 column 都会使用同一个 cell renderer 来绘制 cell 外观。 但实际上放进 model 的数据中,某些 column 的部分是使用 String,某些 column 是 Double,那麽你设计的 cell renderer(ColorTable) 必须能该处理这些不同的 数据型别才行。 2) ColorTable::getTableCellRendererComponent 一律试着把每个 cell 内的 数据转成 String 後以 Double.parseDouble interpret 成 double 数值,在依值 来决定要显示的颜色。 这个作法必须要放 String 数据的 column 里的 cell value(String) 的内容都 是可以 parse 成 double 的(如 "10" 非 "abc")。 说起来非 Double 数据的 column 出错机会比较高,但你的观察显示似乎是 Double column 没有使用 ColorTable 来做 rendering,有一种可能性在是你非直接使用 DefaultTableModel 而是使用自己改写过的 table model,你有 override getColumnClass method,让放置 Double 数值的 column(s) 传回非 Object.class,因为程式只为 Object type 设定 default cell renderer,所以 不是 Object type 的 column 就会使用 JTable 自己预设准备好的 renderer(为 Object type column 设定 default renderer R,不代表没有明确设定 default renderer 的非 Object type column 都会使用 R,尽管 Object 是任一种 class 的 base class)。 另外,建议在 getTableCellRendererComponent method 里直接使用 value 这个 parameter 带进来的值,不需要再透过 row, col 值从 table or model 去取值, 会比较不易出错(table, model 都有 getValueAt method,但是两者的参数 row 与 col 的意义不一致)。 --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 218.173.236.127 ※ 编辑: sbrhsieh 来自: 218.173.236.127 (10/18 23:39) ※ 编辑: sbrhsieh 来自: 218.173.236.127 (10/18 23:46)







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Soft_Job站内搜寻

TOP