java 板


LINE

※ 引述《sbrhsieh (十年一梦)》之铭言: : 拼凑所有你提供的码来看,有一些部分要厘清。 : 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 的意义不一致)。 感谢您的回覆与建议, 您猜的没错,由於需要以数值排序的功能, 所以我override了getColumnClass method, 目的是为了当排序的column型态为double时可以正确以数值大小作为排序依据。 public Class<?> getColumnClass(int column) { Class<?> returnValue; if ((column >= 0) && (column < getColumnCount())) { if(getValueAt(0, column)!=null) { returnValue = getValueAt(0, column).getClass(); } else { returnValue = Object.class; } } else { returnValue = Object.class; } return returnValue; } 根据您的推论 我程式的问题是由於程式只为Object type设定default cell renderer,所以 不是 Object type 的 column 就会使用 JTable 预设准备好的 renderer。 就我的理解, 在此程式的问题在於没有为Double type设定default cell renderer,所以 不是Double type的column就不会使用JTable我自己预设准备好的 renderer。 但DefaultTableCellRenderer中我只找到也只知道getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) 这里可以改写cell的颜色设定, 请问若要在column为Double type的时候可以正确的使用我设定的default cell renderer,要如何去修改我的cell renderer(ColorTable) 呢? -- 邮差又来送羊奶了 --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 60.250.192.238







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灯, 水草

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

TOP