我希望从jtable2中选择内容并点击>>它会进入jtable3并点击<< Jtable3中的选定行被删除但是当我单击<<按钮;它给出了索引错误;我希望在jtabel3中点击<< btton thar row消失了,但它正在给出aaray索引错误
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.xml.bind.annotation.XmlElement.DEFAULT;
public class chk2 extends javax.swing.JFrame {
int km1 = 0;
DefaultTableModel dt;
/** Creates new form chk2 */
public chk2() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The
* content of this method is always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jScrollPane2 = new javax.swing.JScrollPane();
jTable2 = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jScrollPane3 = new javax.swing.JScrollPane();
jTable3 = new javax.swing.JTable();
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"}));
jScrollPane1.setViewportView(jTable1);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable2.setModel(new javax.swing.table.DefaultTableModel(new Object[][]{
{"dfs"}, {"dsfds"}, {"sdfs"}, {"sfds"}, {"sfds"}, {"sfds"},
{"sf"}, {"sf"}, {"fssf"}, {null}, {null}, {null}, {null},
{null}, {null}, {null}, {null}, {null}, {null}, {null},
{null}, {null}, {null}, {null}, {null}, {null}, {null},
{null}, {null}, {null}, {null}, {null}, {null}, {null},
{null}, {null}, {null}, {null}, {null}, {null}, {null},
{null}, {null}, {null}, {null}, {null}, {null}, {null},
{null}, {null}, {null}, {null}, {null}, {null}, {null},
{null}, {null}, {null}, {null}, {null}, {null}, {null},
{null}, {null}, {null}, {null}, {null}, {null}, {null},
{null}, {null}, {null}, {null}, {null}, {null}, {null},
{null}, {null}, {null}, {null}, {null}, {null}, {null},
{null}, {null}, {null}, {null}, {null}, {null}, {null},
{null}, {null}, {null}, {null}, {null}, {null}, {null},
{null}, {null}, {null}, {null}, {null}, {null}, {null},
{null}, {null}, {null}, {null}, {null}, {null}, {null},
{null}, {null}, {null}, {null}, {null}, {null}, {null},
{null}, {null}, {null}, {null}, {null}, {null}, {null},
{null}, {null}, {null}, {null}, {null}, {null}, {null},
{null}, {null}}, new String[]{"Title 1"}));
jScrollPane2.setViewportView(jTable2);
jButton1.setText(">>");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("<<");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
dt = (new javax.swing.table.DefaultTableModel(new Object[][]{
{null, null, null, null, null}}, new String[]{
"Exercise_Name", "Exercise_Type", "Sets", "Repitition", "Time"}) {
Class[] types = new Class[]{java.lang.String.class,
java.lang.String.class, java.lang.Integer.class,
java.lang.Integer.class, java.lang.String.class};
public Class getColumnClass(int columnIndex) {
return types[columnIndex];
}
public boolean isCellEditable(int r, int c) {
if (c == 0 || c == 1) {
return false;
} else {
return true;
}
}
});
jTable3.setModel(dt);
jScrollPane3.setViewportView(jTable3);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane2,
javax.swing.GroupLayout.PREFERRED_SIZE, 109,
javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addComponent(jButton1).addComponent(jButton2)).addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jScrollPane3,
javax.swing.GroupLayout.PREFERRED_SIZE, 375,
javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap()));
layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup().addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup().addContainerGap().addComponent(
jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 275,
javax.swing.GroupLayout.PREFERRED_SIZE)).addGroup(
layout.createSequentialGroup().addGap(65, 65, 65).addComponent(jButton1).addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jButton2)).addGroup(
layout.createSequentialGroup().addGap(34, 34, 34).addComponent(
jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 275,
javax.swing.GroupLayout.PREFERRED_SIZE))).addContainerGap(258, Short.MAX_VALUE)));
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (jTable2.getSelectedRowCount() == 0) {
JOptionPane.showMessageDialog(null, "Select The Exercise");
} else {
int[] b = jTable2.getSelectedRows();
for (int i = 0; i < b.length; i++) {
dt.addRow(new Object[]{"", "", "", "", ""});
jTable3.setValueAt(jTable2.getValueAt(b[i], 0).toString(), km1, 0);
km1++;
}
}// TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
if (jTable2.getSelectedRowCount() == 0) {
JOptionPane.showMessageDialog(null, "Select The row to be deleted");
} else {
int[] b = jTable2.getSelectedRows();
for (int i = 0; i < b.length; i++) {
dt.removeRow(b[i] - i);
}
km1--;
} // TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new chk2().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTable jTable1;
private javax.swing.JTable jTable2;
private javax.swing.JTable jTable3;
// End of variables declaration
}
答案 0 :(得分:1)
我的猜测:
for (int i = 0; i < b.length; i++)
dt.removeRow(b[i] - i);
km1--;
应该是:
Arrays.sort(b);
for (int b.length - 1; i >= 0; --i) {
dt.removeRow(b[i]);
km1--;
}
也就是说,缺少一种排序。伎俩b [i] - 我“太聪明了”。