从表格中获取所有已选中的值

huangapple 未分类评论45阅读模式
英文:

Get all checked value from the table

问题

这是我需要的输出:

从表格中获取所有已选中的值

这是我目前编写的代码:

TableModel model = table_test.getModel();
for (int i = 0; i < model.getRowCount(); ++i) {
    Boolean isChecked = Boolean.valueOf(model.getValueAt(i, 0).toString());
    String col = model.getValueAt(i, 1).toString();
    if (isChecked == true) {
        try {
            textarea.setText(col);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }
}

这段代码只会在文本区域显示一个值。

希望你能帮我解决这个问题。

英文:

I'm trying to get all value from a table where the checkbox is checked, I've make codes but it only gets one output and not working if I checked randomly you have to start checking from the start to make it work.

This the Output I need:

从表格中获取所有已选中的值

this my current codes I've make:

TableModel model = table_test.getModel();
 for(int i=0; i&lt;model.getRowCount();++i)
 {
 Boolean isChecked = Boolean.valueOf(model.getValueAt(i, 0).toString());    
 String col = model.getValueAt(i,1).toString();
  if (isChecked==true) {
      try{
      textarea.setText(col);
     } catch (Exception e) {
         JOptionPane.showMessageDialog(null, e);
     }
 }     
 }

this codes only display is one value at the text area.

I hope you can help me with this problem.

答案1

得分: -1

你可以尝试以下代码:

TableModel model = table_test.getModel();

for (int i = 0; i < model.getRowCount(); ++i) {
    Boolean isChecked = Boolean.valueOf(model.getValueAt(i, 0).toString());
    String col = model.getValueAt(i, 1).toString();
    if (isChecked == true) {
        try {
            textarea.append(col);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }
}
英文:
You can try below.





  TableModel model = table_test.getModel();

     for(int i=0; i&lt;model.getRowCount();++i)
     {
     Boolean isChecked = Boolean.valueOf(model.getValueAt(i, 0).toString());    
     String col = model.getValueAt(i,1).toString();
      if (isChecked==true) {
          try{
           textarea.append(col);
         
         } catch (Exception e) {
             JOptionPane.showMessageDialog(null, e);
         }
     }     
     }

huangapple
  • 本文由 发表于 2020年4月7日 12:05:10
  • 转载请务必保留本文链接:https://java.coder-hub.com/61072650.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定