JTabel中有JCheckBox如何导出Excel

3 篇文章 0 订阅

 今天,我想把“登陆管理"中的人员清单,写程序用Excel导出来,原本想,这很简单啊,不就是一个Jtabel中的内容吗!结果还是有些小问题让我困扰,后来,才发现是数据类型转换的问题造成的



"访客权限","管理员权限",这4个栏位都是用JCheckBox,在AbstractTableModel中有一个方法返回的是Boolean.class,在Jtabel表格中就是
true与false,true就是会打勾

        public Class getColumnClass(int columnIndex) {  
                  // 返回每一列的数据类型  
           return typeArray.get(columnIndex);
       }


经过排查,是以下的程序导出Excel时有问题
             for(int i =0;i<b ;i++){
   
 for(int j=1;j<=a;j++){
 
     String str = null;     
     str = (String) table.getValueAt(j-1, i);  
     jxl.write.Label labelN = new jxl.write.Label(i, j, str);
     try {
      ws.addCell(labelN);
     } catch (RowsExceededException e) {
      e.printStackTrace();
     } catch (WriteException e) {
      e.printStackTrace();
     }
 }
   } 
   //写入工作表
   wwb.write();

修改之后:
for(int j=1;j<=a;j++){
 
     String str = null;
     if (i== 5 || i== 6 || i== 7 || i== 8){
  //因为是 Boolean类型,所以我先转为object,再转为string
                                 //这样,程序就可以正常的导出Excel了 
 
     Object stry =table.getValueAt(j-1, i);     
                               //强转为string
     str = stry.toString();
     if (str.equals("false")){         
        str = "";
     }

     }else{
        str = (String) table.getValueAt(j-1, i);
     }
 
     jxl.write.Label labelN = new jxl.write.Label(i, j, str);
     try {
      ws.addCell(labelN);
     } catch (RowsExceededException e) {
      e.printStackTrace();
     } catch (WriteException e) {
      e.printStackTrace();
     }
 }
   } 
   //写入工作表
    wwb.write();


Java Swing中,处理JList中JCheckBox选中的事件通常需要使用`ListSelectionListener`和`ItemListener`。首先,你需要为JList添加这两个监听器,然后定义当选择发生变化时要执行的方法。 以下是处理JList中JCheckBox选中事件的基本步骤: 1. 创建一个实现了`ListSelectionListener`和`ItemListener`接口的类(例如MyListener): ```java public class MyListener implements ListSelectionListener, ItemListener { private final ActionListener listener; public MyListener(ActionListener listener) { this.listener = listener; } @Override public void valueChanged(ListSelectionEvent e) { // 处理列表选择变化 if (e.getValueIsAdjusting()) { return; } int index = e.getFirstIndex(); if (index != -1) { boolean isChecked = ((JCheckBox)e.getItem()).isSelected(); listener.actionPerformed(new ActionEvent(null, 0, "Checkbox at index " + index + " is now " + (isChecked ? "checked" : "unchecked"))); } } @Override public void itemStateChanged(ItemEvent e) { // 处理单个项的选中状态改变 JCheckBox checkBox = (JCheckBox) e.getItem(); listener.actionPerformed(new ActionEvent(null, 0, "Checkbox with label: " + checkBox.getText() + " is now " + (checkBox.isSelected() ? "checked" : "unchecked"))); } } ``` 2. 在初始化JList时,将其与`MyListener`关联起来: ```java ActionListener actionListener = new ActionListener() { // 定义你想在事件触发时执行的操作 }; JList<?> list = ...; list.getSelectionModel().addListSelectionListener(new MyListener(actionListener)); list.getModel(). addItemListener(new MyListener(actionListener)); ``` 这样,每当JList中的JCheckBox选中或取消选中时,你指定的actionListener会接收到通知并执行相应的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值