Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 求助!JTable中显示JCheckBox(浪费大家时间了,我找到答案了)=>又有新问题row selection
joelwx





发贴: 97
积分: 2
于 2006-09-26 11:21 user profilesend a private message to usersend email to joelwxsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我的JTable里有一列是on/off,想用个JCheckBox去显示它.
http://javaalmanac.com/egs/javax.swing.table/ComboBox.html 这里倒是有个相似的例子,可试了没用啊.
问题:是否我在JTable.getColumnModel(index).setCellRenderer()里设置了MyCellRenderer(extends JCheckBox implements TableCellRenderer)后,这一列中所有行都会显示为一个JCheckBox呢??还是我要一行一行得设?很头晕!!
希望哪位大侠给个简短点的例子!
==========================
private class MyTableCellRenderer extends JCheckBox implements TableCellRenderer{
    public MyTableCellRenderer(Object value){
      super();
      setValue(value);
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
      if (isSelected) {
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}

// Select the current value
setValue(value);
return this;
      
    }
    protected void setValue(Object value){
      this.setSelected(value.toString().trim().equals("1"));  
    }
  }
===================
答案:
DefaultTableModel model=new DefaultTableModel(){
public Class getClass(int index){
if(index==2)
return Boolean.class;
return super.getClass(index);
}
}
=====================
新问题:
在JTable上右击后会先选中鼠标所在的row,再出来一个PopupMenu.
public void mouseClicked(MouseEvent e){
int row_Selected_Index=JTable.rowAtPoint(new Point(e.getX(),e.getY()));
JTable.setRowSelectioninterval(row_Selected_Index,row_Selected_Index);
PopupMenu.show(e.getComponent(),e.getX(),e.getY());
System.out.println(JTable.getSelectedRow());
}
========================
从输出上看结果是对的,点哪个出哪个,可显示不对.
被选中那一行并没有SelectionBackground.
从界面上看,这一行并没有被选中(虽然已被选中了)......
repaint也不行~~
只有当PopupMenu消失以后,SelectionBackground就出来了...
不知道原因,请指教!!
google了一下,没找到答案!


joelwx edited on 2006-09-26 14:36

好多不会唉...
作者 标题太长?? [Re:joelwx]
joelwx





发贴: 97
积分: 2
于 2006-09-26 14:44 user profilesend a private message to usersend email to joelwxsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
JTable.getRowSelectionAllowed()值为true;
JTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);



好多不会唉...
作者 新发现 [Re:joelwx]
joelwx





发贴: 97
积分: 2
于 2006-09-26 14:50 user profilesend a private message to usersend email to joelwxsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
public void mouseClicked(MouseEvent e){
int row_Selected_Index=JTable.rowAtPoint(new Point(e.getX(),e.getY()));
JTable.setRowSelectioninterval(row_Selected_Index,row_Selected_Index);
//PopupMenu.show(e.getComponent(),e.getX(),e.getY());
System.out.println(JTable.getSelectedRow());
}
把PopupMenu去掉就好了....
难不成我要先等一下再PopupMenu.show()???
继续探索中...



好多不会唉...
作者 终于找到了办法了~~ [Re:joelwx]
joelwx





发贴: 97
积分: 2
于 2006-10-11 16:11 user profilesend a private message to usersend email to joelwxsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
把PopupMenu换成JPopupMenu就行了~~~


好多不会唉...
作者 Re:求助!JTable中显示JCheckBox [Re:joelwx]
cxp108





发贴: 363
积分: 12
于 2006-10-12 08:36 user profilesend a private message to usersend email to cxp108search all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
J开头的用户界面类是改良过的,在Swing编程里最好还是用J开头的。



flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923