JTable中的一列我渲染成了CheckBox,但是当鼠标点击后,不能显示出选中的状态。请高手帮忙解决一下啊。在线等!
JTable的model:class EntitySynchroTableModel extends AbstractTableModel{
private List<Boolean> checkBoxValues = new ArrayList<Boolean>();
private List<ComparisonResultVO> datas;
private List<ComparisonResultVO> selectdatas;
public EntitySynchroTableModel(){
datas = new ArrayList<ComparisonResultVO>();
selectdatas = new ArrayList<ComparisonResultVO>();
}
public int getRowCount() {
return datas == null ? 0 : datas.size();
} public int getColumnCount() {
return columns == null ? 0 : columns.length;
} public Object getValueAt(int rowIndex, int columnIndex) {
if(null==datas || rowIndex>datas.size()-1)
return null;
ComparisonResultVO tempVO = datas.get(rowIndex);
if(getColumnName(columnIndex).equals(CHOOSE)){
return this.checkBoxValues.get(rowIndex);
}
if(getColumnName(columnIndex).equals(CURNET_RESOURCE)){
return tempVO.getLocalEntity();
}
if(getColumnName(columnIndex).equals(NOTE)){
return tempVO.getNote();
}
for(Map.Entry<LocalNetVO,RemoteEntityStatusVO> entity : tempVO.getRemoteEntityMap().entrySet()){
if(getColumnName(columnIndex).equals(entity.getKey().getFromNetlName())){
return entity.getValue();
}
}
return null;
}
public Class<?> getColumnClass(int columnIndex){
if(getColumnName(columnIndex).equals(CHOOSE)){
return Boolean.class;
}
if(getColumnName(columnIndex).equals(CURNET_RESOURCE))
return BaseVO.class;
for(LocalNetVO vo : localNetVOs){
if(getColumnName(columnIndex).equals(vo.getFromNetlName()))
return RemoteEntityStatusVO.class;
}
if(getColumnName(columnIndex).equals(NOTE)){
return String.class;
}
return null;
}
public String getColumnName(int column){
return columns == null ? "" : columns[column];
}
public void clear(){
if(null != datas){
datas.clear();
fireTableDataChanged();
}
}
public void setData(List<ComparisonResultVO> datas){
this.datas = datas;
this.checkBoxValues.clear();
for(ComparisonResultVO result : datas){
this.checkBoxValues.add(false);
}
fireTableDataChanged();
}
public void removeRow(int rowIndex){
if(null != datas){
datas.remove(rowIndex);
fireTableDataChanged();
}
}
public int getColumnIndexByName(String columnName){
for(int i = 0 ; i < columns.length ; i++){
if (columnName.equals(columns[i])){
return i;
}
}
return -1;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
if(this.getColumnName(columnIndex).equals(CHOOSE)){
return true;
}
return false;
}
}CheckBoxRenderer及Editor:class CheckBoxRender extends JCheckBox implements TableCellRenderer {
public CheckBoxRender(){
super();
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JCheckBox box = new JCheckBox();
box.setHorizontalAlignment(JLabel.CENTER);
box.setSelected(Boolean.TRUE.equals(value));
return box;
}
}
class CheckBoxEditor extends DefaultCellEditor {
public CheckBoxEditor() {
this(new JCheckBox());
}
public CheckBoxEditor(JCheckBox checkBox) {
super(checkBox);
checkBox.setHorizontalAlignment(JLabel.CENTER);
}
}
JTable的model:class EntitySynchroTableModel extends AbstractTableModel{
private List<Boolean> checkBoxValues = new ArrayList<Boolean>();
private List<ComparisonResultVO> datas;
private List<ComparisonResultVO> selectdatas;
public EntitySynchroTableModel(){
datas = new ArrayList<ComparisonResultVO>();
selectdatas = new ArrayList<ComparisonResultVO>();
}
public int getRowCount() {
return datas == null ? 0 : datas.size();
} public int getColumnCount() {
return columns == null ? 0 : columns.length;
} public Object getValueAt(int rowIndex, int columnIndex) {
if(null==datas || rowIndex>datas.size()-1)
return null;
ComparisonResultVO tempVO = datas.get(rowIndex);
if(getColumnName(columnIndex).equals(CHOOSE)){
return this.checkBoxValues.get(rowIndex);
}
if(getColumnName(columnIndex).equals(CURNET_RESOURCE)){
return tempVO.getLocalEntity();
}
if(getColumnName(columnIndex).equals(NOTE)){
return tempVO.getNote();
}
for(Map.Entry<LocalNetVO,RemoteEntityStatusVO> entity : tempVO.getRemoteEntityMap().entrySet()){
if(getColumnName(columnIndex).equals(entity.getKey().getFromNetlName())){
return entity.getValue();
}
}
return null;
}
public Class<?> getColumnClass(int columnIndex){
if(getColumnName(columnIndex).equals(CHOOSE)){
return Boolean.class;
}
if(getColumnName(columnIndex).equals(CURNET_RESOURCE))
return BaseVO.class;
for(LocalNetVO vo : localNetVOs){
if(getColumnName(columnIndex).equals(vo.getFromNetlName()))
return RemoteEntityStatusVO.class;
}
if(getColumnName(columnIndex).equals(NOTE)){
return String.class;
}
return null;
}
public String getColumnName(int column){
return columns == null ? "" : columns[column];
}
public void clear(){
if(null != datas){
datas.clear();
fireTableDataChanged();
}
}
public void setData(List<ComparisonResultVO> datas){
this.datas = datas;
this.checkBoxValues.clear();
for(ComparisonResultVO result : datas){
this.checkBoxValues.add(false);
}
fireTableDataChanged();
}
public void removeRow(int rowIndex){
if(null != datas){
datas.remove(rowIndex);
fireTableDataChanged();
}
}
public int getColumnIndexByName(String columnName){
for(int i = 0 ; i < columns.length ; i++){
if (columnName.equals(columns[i])){
return i;
}
}
return -1;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
if(this.getColumnName(columnIndex).equals(CHOOSE)){
return true;
}
return false;
}
}CheckBoxRenderer及Editor:class CheckBoxRender extends JCheckBox implements TableCellRenderer {
public CheckBoxRender(){
super();
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JCheckBox box = new JCheckBox();
box.setHorizontalAlignment(JLabel.CENTER);
box.setSelected(Boolean.TRUE.equals(value));
return box;
}
}
class CheckBoxEditor extends DefaultCellEditor {
public CheckBoxEditor() {
this(new JCheckBox());
}
public CheckBoxEditor(JCheckBox checkBox) {
super(checkBox);
checkBox.setHorizontalAlignment(JLabel.CENTER);
}
}
解决方案 »
- 关于泛型
- jdk1.5下的concurrent线程池的实现原理是怎样的,和1.5前实现的线程池有什么区别。
- jNative 访问C++动态库(dll)
- Jbuilder 2007 如何吧.class打包成jar文件?
- 类中方法返回对象是否破坏封装原则?
- 求,查找节的快速算法
- 请给位高手 给我指点一下 我要学oracle+java 给我提个路线
- 如何使JOptionPane.showConfirmDialog(null,"thisi asdf","sdffsd",JOptionPane.YES_NO_OPTION);在客户端显示?
- 执行顺序
- 关于数据存储的问题
- JAVA图片处理的问题
- his系统有没有用java开发的?
在getTableCellRendererComponent是不是应该改成
{
this.setHorizontalAlignment(JLabel.CENTER);
this.setSelected(Boolean.TRUE.equals(value));
return this;
}
super();
}
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());
}
this.setSelected((Boolean)value);
return this;
}
} public class MyComboBoxEditor extends DefaultCellEditor {
public MyComboBoxEditor() {
super(new JCheckBox());
}
}
2列实验
DefaultTableModel model = new DefaultTableModel();
model.addColumn("A", new Boolean[]{true});
model.addColumn("B", new Boolean[]{false});
jTable1.setModel(model);
for (int vColIndex = 0; vColIndex < jTable1.getColumnCount(); vColIndex++) {
TableColumn col = jTable1.getColumnModel().getColumn(vColIndex);
col.setCellEditor(new MyComboBoxEditor()); col.setCellRenderer(new MyComboBoxRenderer()); }这段代码我验证过了,是可以的,你的model我没仔细看,不知道你有没有特殊要求,如果有,用你的试试,如果不行说明问题可能在model里,之后再说,如果model没有特殊作用,你不需要自己写,直接用DefaultTableModel 就好了
师兄:能帮我看看我的model不?按照你的思路,确实我的model有问题!小弟刚做Swing开发,对这个还不是很熟悉。谢谢。能告诉我QQ不?我加你!
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
class JtableComboBoxTest extends JFrame
{
private JTable table;
public JtableComboBoxTest(){
String[] array=new String[11];
for(int i=0;i <10;i++)
array[i]=""+(i+1);
array[10]="未选择";
JComboBoxCellEditor editor=new JComboBoxCellEditor(array);
table=new JTable(new Object[][]{{"未选择",1},{"未选择",2},{"未选择",3},{"未选择",4},{"未选择",5},{"未选择",6}},
new Object[]{"Column1","Column2"});
TableColumnModel colModel=table.getColumnModel();
colModel.getColumn(0).setCellEditor(editor);
this.add(new JScrollPane(table),BorderLayout.CENTER);
this.setSize(200,200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new JtableComboBoxTest();
}
class JComboBoxCellEditor extends DefaultCellEditor
{
private Object needRemove=null;//下次点击JComboBox时,需要删除的选项
private Object needAdd=null;//下次点击JComboBox时,需要添加的选项
private Object value;//当前comboBox显示的值
private JComboBox comboBox=null;
public JComboBoxCellEditor(Object[] value){
super(new JComboBox(value));
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column){
if(comboBox==null){//初始化comboBox
comboBox=(JComboBox)super.getTableCellEditorComponent(table, value, isSelected, row, column);
comboBox.addItemListener(new ItemHandler());//添加事件监听
comboBox.setSelectedIndex(10);
}
this.value=value;//当前JComboBox显示的选项
if(needAdd!=null)
comboBox.addItem(needAdd);
if(needRemove!=null)
comboBox.removeItem(needRemove);
return comboBox;
}
private class ItemHandler implements ItemListener
{
public void itemStateChanged(ItemEvent e){
needRemove=comboBox.getSelectedItem();
//当JComboBox显示的选项与用户选择的选项不同时
//需要将原来的显示的选项加入JComoboBox
if(!comboBox.getSelectedItem().equals(value)&&!"未选择".equals(value)){
needAdd=value;
}
else
needAdd=null;
}
}
}
}
看看 这个例子 希望对你 有帮助!
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
class JtableComboBoxTest extends JFrame
{
private JTable table;
public JtableComboBoxTest(){
String[] array=new String[11];
for(int i=0;i <10;i++)
array[i]=""+(i+1);
array[10]="未选择";
JComboBoxCellEditor editor=new JComboBoxCellEditor(array);
table=new JTable(new Object[][]{{"未选择",1},{"未选择",2},{"未选择",3},{"未选择",4},{"未选择",5},{"未选择",6}},
new Object[]{"Column1","Column2"});
TableColumnModel colModel=table.getColumnModel();
colModel.getColumn(0).setCellEditor(editor);
this.add(new JScrollPane(table),BorderLayout.CENTER);
this.setSize(200,200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new JtableComboBoxTest();
}
class JComboBoxCellEditor extends DefaultCellEditor
{
private Object needRemove=null;//下次点击JComboBox时,需要删除的选项
private Object needAdd=null;//下次点击JComboBox时,需要添加的选项
private Object value;//当前comboBox显示的值
private JComboBox comboBox=null;
public JComboBoxCellEditor(Object[] value){
super(new JComboBox(value));
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column){
if(comboBox==null){//初始化comboBox
comboBox=(JComboBox)super.getTableCellEditorComponent(table, value, isSelected, row, column);
comboBox.addItemListener(new ItemHandler());//添加事件监听
comboBox.setSelectedIndex(10);
}
this.value=value;//当前JComboBox显示的选项
if(needAdd!=null)
comboBox.addItem(needAdd);
if(needRemove!=null)
comboBox.removeItem(needRemove);
return comboBox;
}
private class ItemHandler implements ItemListener
{
public void itemStateChanged(ItemEvent e){
needRemove=comboBox.getSelectedItem();
//当JComboBox显示的选项与用户选择的选项不同时
//需要将原来的显示的选项加入JComoboBox
if(!comboBox.getSelectedItem().equals(value)&&!"未选择".equals(value)){
needAdd=value;
}
else
needAdd=null;
}
}
}
}