0

我希望在从 jtable2 中选择内容并单击 >> 时将其粘贴到 jtable3 上,然后单击 << Jtable3 中的选定行被删除,但是当我单击 << 按钮时;它给出了一个索引错误;我希望在 jtabel3 中单击 << btton thar 行消失,但它给出了 aaray out of index 错误

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
}
4

1 回答 1

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] - i 是“太聪明了”。

于 2012-10-07T16:42:19.147 回答