jtable中表格第一列全部为checkbox(布尔型),删除时如何判断全部没有选中

jframe中画了一个jtable,点击删除按钮,这是需要做一个判断,全部没有选中时,对话框提示“请至少选中一项”intm;for(m=DetailTable.getRo... jframe中画了一个jtable,点击删除按钮,这是需要做一个判断,全部没有选中时,对话框提示“请至少选中一项”
int m;
for(m=DetailTable.getRowCount()-1;m>-1;m--){

//Boolean n = (Boolean)DetailTable.getValueAt(m, 0);
......
如果都没有选中,则提示:
JOptionPane.showMessageDialog(null,"请选择要删除的行!");
展开
 我来答
百度网友d505c33c3
2009-08-03 · TA获得超过250个赞
知道小有建树答主
回答量:115
采纳率:0%
帮助的人:68万
展开全部
放个计数器
int c=0;
for(int m=DetailTable.getRowCount()-1;m>-1;m--){

//Boolean n = (Boolean)DetailTable.getValueAt(m, 0);
if(n)c++;
......
如果都没有选中,则提示:
JOptionPane.showMessageDialog(null,"请选择要删除的行!");
if(0==c){就没选中呗}
呵呵,见笑
百度网友2a024da8f
2009-08-03
知道答主
回答量:7
采纳率:0%
帮助的人:0
展开全部
package src;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class table1 extends javax.swing.JFrame {

public table1() {
initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
DetailTable = new javax.swing.JTable();
checkAllBox = new javax.swing.JCheckBox();
TuikaBotton = new javax.swing.JButton();
DeleteDitail = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("管理系统");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});

jLabel1.setFont(new java.awt.Font("Simsun", 1, 24)); // NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("11111111111");

jLabel2.setFont(new java.awt.Font("Simsun", 0, 12)); // NOI18N
jLabel2.setText("1:");

jLabel3.setFont(new java.awt.Font("Simsun", 0, 12)); // NOI18N
jLabel3.setText("2:");

jTextField2.setText("00000001");
jTextField2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField2ActionPerformed(evt);
}
});

jTextField1.setText("2008/08/08");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});

jButton1.setFont(new java.awt.Font("Simsun", 0, 12)); // NOI18N
jButton1.setText("查找");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setFont(new java.awt.Font("Simsun", 0, 12));
jButton2.setText("确认");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jButton3.setFont(new java.awt.Font("Simsun", 0, 12));
jButton3.setText("打印");

jButton4.setFont(new java.awt.Font("Simsun", 0, 12));
jButton4.setText("Exit");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});

jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "查找结果", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Simsun朝", 0, 12))); // NOI18N

DetailTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {

},
new String [] {
"", "1", "2", "3", "4", "5", "6", "7", "8"
}
) {
Class[] types = new Class [] {
java.lang.Boolean.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
};
boolean[] canEdit = new boolean [] {
true, false, false, false, false, false, false, false, false
};

public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}

public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
DetailTable.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
DetailTable.getTableHeader().setReorderingAllowed(false);
jScrollPane1.setViewportView(DetailTable);
DetailTable.getColumnModel().getColumn(0).setResizable(false);
DetailTable.getColumnModel().getColumn(0).setPreferredWidth(22);
DetailTable.getColumnModel().getColumn(1).setResizable(false);
DetailTable.getColumnModel().getColumn(2).setResizable(false);
DetailTable.getColumnModel().getColumn(3).setResizable(false);
DetailTable.getColumnModel().getColumn(4).setResizable(false);
DetailTable.getColumnModel().getColumn(5).setResizable(false);
DetailTable.getColumnModel().getColumn(6).setResizable(false);
DetailTable.getColumnModel().getColumn(7).setResizable(false);
DetailTable.getColumnModel().getColumn(8).setResizable(false);

checkAllBox.setFont(new java.awt.Font("Simsun朝", 0, 12));
checkAllBox.setText("全选");
checkAllBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
checkAllBoxActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(checkAllBox)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 691, Short.MAX_VALUE))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(checkAllBox)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 332, Short.MAX_VALUE)
.addContainerGap())
);

TuikaBotton.setFont(new java.awt.Font("Times New Roman", 0, 12));
TuikaBotton.setText("Add Test");
TuikaBotton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
TuikaBottonActionPerformed(evt);
}
});

DeleteDitail.setFont(new java.awt.Font("Times New Roman", 0, 12));
DeleteDitail.setText("Delete Test");
DeleteDitail.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
DeleteDitailActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton4))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(TuikaBotton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(DeleteDitail)
.addGap(26, 26, 26)
.addComponent(jButton3)
.addGap(18, 18, 18)
.addComponent(jButton2)))
.addGap(58, 58, 58))
.addGroup(layout.createSequentialGroup()
.addGap(37, 37, 37)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(36, 36, 36))
.addGroup(layout.createSequentialGroup()
.addGap(214, 214, 214)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(233, 233, 233))
.addGroup(layout.createSequentialGroup()
.addGap(238, 238, 238)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE)
.addGap(264, 264, 264))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(34, 34, 34)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addGap(18, 18, 18)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jButton3)
.addComponent(TuikaBotton)
.addComponent(DeleteDitail))
.addGap(18, 18, 18)
.addComponent(jButton4)
.addContainerGap())
);

pack();
}// </editor-fold>

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}

private void formWindowOpened(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:

}

private void checkAllBoxActionPerformed(java.awt.event.ActionEvent evt) {
int count=DetailTable.getRowCount();
int i;
if(checkAllBox.isSelected()){
for(i=0;i<count;i++){
DetailTable.getModel().setValueAt(new Boolean(true), i, 0);
}
}else {
for(i=0;i<count;i++){
DetailTable.getModel().setValueAt(new Boolean(false), i, 0);
}

}
}

private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void TuikaBottonActionPerformed(java.awt.event.ActionEvent evt) {
Object obj[]={ new Boolean(false),"1","1","1","1","1","1","1","1"};
( (DefaultTableModel) DetailTable.getModel()).addRow(obj);
}

private void DeleteDitailActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int[] rows=DetailTable.getSelectedRows();
int m;
for(m=DetailTable.getRowCount()-1;m>-1;m--){
Boolean n = (Boolean)DetailTable.getValueAt(m, 0);
if(n==false){

JOptionPane.showMessageDialog(null,"请选择要删除的行!");

}
else {
int flag=JOptionPane.showConfirmDialog(null, "确定删除?");
if(flag==JOptionPane.YES_OPTION){
int i;
for(i=DetailTable.getRowCount()-1;i>-1;i--){
Boolean a = (Boolean)DetailTable.getValueAt(i, 0);
if(a){
((DefaultTableModel)DetailTable.getModel()).removeRow(i);
// i=0;
}
}
}

}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
table1 a = new table1();
a.setVisible(true);
}
});
}
private javax.swing.JButton DeleteDitail;
private javax.swing.JTable DetailTable;
private javax.swing.JButton TuikaBotton;
private javax.swing.JCheckBox checkAllBox;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
}
----------------------------------------------------------------------
package src;
public class TableBean {
Object getGoodsName() {
throw new UnsupportedOperationException("Not yet implemented");
}
Object getUnitPrice() {
throw new UnsupportedOperationException("Not yet implemented");
}
Object getCount() {
throw new UnsupportedOperationException("Not yet implemented");
}
Object getSumPrice() {
throw new UnsupportedOperationException("Not yet implemented");
}
Object getDetailDate() {
throw new UnsupportedOperationException("Not yet implemented");
}
}
----------------------------------------------------------------------
package src;
class Table {
static int getRowCount() {
throw new UnsupportedOperationException("Not yet implemented");
}
static Object getModel() {
throw new UnsupportedOperationException("Not yet implemented");
}
}
----------------------------------------------------------------------
package src;

class Err {
void setErrLabel(String errMess) {
throw new UnsupportedOperationException("Not yet implemented");
}
void setSize(int i, int i0) {
throw new UnsupportedOperationException("Not yet implemented");
}
void setVisible(boolean b) {
throw new UnsupportedOperationException("Not yet implemented");
}
}
----------------------------------------------------------------------
package src;

import javax.swing.JTable;
import src.TableBean;

public interface ButtonInterface {
boolean isInt(String str);
void showErr();
void insertDetilTalbeRow(TableBean orderDetailBean,JTable DetailTable);
public void deleteJTableRow(JTable table);

}
----------------------------------------------------------------------
package src;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import src.Err;

public class ButtonImp implements ButtonInterface{

//数字判断
public boolean isInt(String str){
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() )
{
return false;
}
return true;
}
//
public void showErr() {
//throw new UnsupportedOperationException("Not supported yet.");
Err err = new Err();
String errMess;
errMess=" int please!";
err.setErrLabel(errMess);
err.setSize(500,80);
err.setVisible(true);
}

public void insertDetilTalbeRow(TableBean orderDetailBean,JTable DetailTable) {

Object obj[] = { new Boolean(false),orderDetailBean.getGoodsName(),
orderDetailBean.getUnitPrice(),orderDetailBean.getCount(),
orderDetailBean.getSumPrice(),orderDetailBean.getDetailDate()};
( (DefaultTableModel) DetailTable.getModel()).addRow(obj);

}

public void deleteJTableRow(JTable table){
int i;
for(i=table.getRowCount()-1;i>-1;i--){
Boolean bool = (Boolean) table.getValueAt(i, 0);
if(bool){
((DefaultTableModel)table.getModel()).removeRow(i);
}
}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式