有关java swing 中 用JCheckBox 控制 JTable增加或删除列

抱歉,我讲错了,是删除行,不是删除列。当checkbox选中时,就增加某行,当checkbox取消选中时,就删除某行。这个我自己做过,但是有时候会报错,总会多出一行,请高... 抱歉,我讲错了,是删除行,不是删除列。
当checkbox选中时,就增加某行,
当checkbox取消选中时,就删除某行。
这个我自己做过,但是有时候会报错,总会多出一行,请高手帮忙做个DEMO
展开
 我来答
daay1986
2011-11-10 · TA获得超过6018个赞
知道大有可为答主
回答量:2208
采纳率:0%
帮助的人:1476万
展开全部
忘记你的Jtable把,你要处理的是数据。
public class Frame extends javax.swing.JFrame {

TableModel model = new TableModel();

/** Creates new form Frame */
public Frame() {
initComponents();
this.jTable1.setModel(model);
}

class TableModel extends AbstractTableModel {

String[] values = {"乌龟", "海龟", "鸵鸟", "老鸟"};
private int n = 3;

@Override
public int getRowCount() {
return (n + 1) * 2;
}

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}

public void setIndex(int n) {
this.n = n;
this.fireTableStructureChanged();
}

@Override
public int getColumnCount() {
return (n + 1) * 2;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return values[n];
}
}

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

jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jPanel1 = new javax.swing.JPanel();
乌龟 = new javax.swing.JButton();
海龟 = new javax.swing.JButton();
鸵鸟 = new javax.swing.JButton();
老鸟 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jScrollPane1.setName("jScrollPane1"); // NOI18N

jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jTable1.setName("jTable1"); // NOI18N
jScrollPane1.setViewportView(jTable1);

jPanel1.setName("jPanel1"); // NOI18N

乌龟.setText("乌龟");
乌龟.setName("乌龟"); // NOI18N
乌龟.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
乌龟ActionPerformed(evt);
}
});

海龟.setText("海龟");
海龟.setName("海龟"); // NOI18N
海龟.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
海龟ActionPerformed(evt);
}
});

鸵鸟.setText("鸵鸟");
鸵鸟.setName("鸵鸟"); // NOI18N
鸵鸟.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
鸵鸟ActionPerformed(evt);
}
});

老鸟.setText("老鸟");
老鸟.setName("老鸟"); // NOI18N
老鸟.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
老鸟ActionPerformed(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()
.addGap(21, 21, 21)
.addComponent(乌龟)
.addGap(18, 18, 18)
.addComponent(海龟)
.addGap(18, 18, 18)
.addComponent(鸵鸟)
.addGap(18, 18, 18)
.addComponent(老鸟)
.addContainerGap(117, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(乌龟)
.addComponent(海龟)
.addComponent(鸵鸟)
.addComponent(老鸟))
.addContainerGap(16, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE))
);

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

private void 乌龟ActionPerformed(java.awt.event.ActionEvent evt) {
model.setIndex(0);
}

private void 海龟ActionPerformed(java.awt.event.ActionEvent evt) {
model.setIndex(1);
}

private void 鸵鸟ActionPerformed(java.awt.event.ActionEvent evt) {
model.setIndex(2);
}

private void 老鸟ActionPerformed(java.awt.event.ActionEvent evt) {
model.setIndex(3);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new Frame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JButton 乌龟;
private javax.swing.JButton 海龟;
private javax.swing.JButton 老鸟;
private javax.swing.JButton 鸵鸟;
// End of variables declaration
}
匿名用户
2011-11-10
展开全部
这个,就是每个 checkbox 加 itemchange 事件
哪个被选中了,就显示表格中的哪列
哪个取消选中,就隐藏表格中的哪列

隐藏列,你百度搜索 JTable 隐藏列 就可以了
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式