删除已选中的jTable行,并在按下按钮

时间:2015-11-08 04:40:22

标签: java swing jtable

我需要删除按下按钮后选择的行。

这是我的代码:我有一个复选框,然后检查它是否已选中并将相应的行添加到列表中。

DefaultTableModel model = (DefaultTableModel) jTableLayouts.getModel();

// gets the number of rows that were selected
Public ArrayList<Integer> selectedLines = new ArrayList<Integer>();

for (int j = 0; j <= jTableLayouts.getModel().getRowCount(); j++) {
    if ((Boolean) jTableLayouts.getModel().getValueAt(j,2)){ //checkbox
          selectedLines.add(jTableLayouts.getSelectedRow());     
        }
         model.removeRow(selectedLines.get(j));           
    }

使用此代码我可以逐个删除。有什么想法,按下按钮后如何删除所有行?

编辑:我只需要删除复选框中标记为true的行。示例:行0,1,4,5标记为true,之后我将在列表中添加这些行,然后在按下按钮后同时删除列表中的行。

1 个答案:

答案 0 :(得分:0)

@MadProgrammer 已经注释时,您可以通过让ActionListener执行删除操作并从已删除的行添加已删除的行来完成此操作。

在按下该按钮的此片段中,左侧已选中的行将被删除并添加到右侧的表格中。检查createDeleteButton方法是否完成此操作。

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

@SuppressWarnings("serial")
public class TestTableDeleteRows extends JPanel {
    private JTable tblLeft;
    private JTable tblRight;

    public TestTableDeleteRows() {
        initialize();
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(600,400);
    }

    private void initialize() {
        setLayout(new BorderLayout());
        add(createHeaderPane(),BorderLayout.NORTH);
        add(createTablePane(),BorderLayout.CENTER);
        add(createDeleteButton(),BorderLayout.SOUTH);
    }

    private JPanel createHeaderPane() {
        JPanel p = new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.LINE_AXIS));
        p.add(new JScrollPane(new JLabel("Source",SwingConstants.CENTER)));
        p.add(new JScrollPane(new JLabel("Deleted",SwingConstants.CENTER)));
        return p;
    }

    private JPanel createTablePane() {
        JPanel p = new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
        tblLeft = createTable(stdRows);
        tblRight = createTable(new Vector<Vector<Object>>());
        p.add(new JScrollPane(tblLeft));
        p.add(new JScrollPane(tblRight));
        return p;
    }

    private static final int COL_CHECK=0;
    private static final int COL_TEXT=1;
    private static final Vector<Vector<Object>> stdRows;
    private static final Vector<Object> cols; 
    static {
        stdRows = new Vector<Vector<Object>>();
        for(int i=0;i!=100;++i)
            stdRows.add(new Vector<Object>(Arrays.asList(new Object[]{Boolean.FALSE,"This is text line number "+(i+1)})));
        cols = new Vector<>(Arrays.asList(new Object[]{"Check me","Silly text"}));
    }

    private static JTable createTable(Vector<Vector<Object>> rows) {
        JTable t = new JTable(rows,cols) {
            @Override
            public Class<?> getColumnClass(int column) {
                if(getRowCount()>0)
                    return getValueAt(0, column).getClass();
                return super.getColumnClass(column);
            }
        };
        t.getColumnModel().getColumn(COL_CHECK).setPreferredWidth(50);
        t.getColumnModel().getColumn(COL_TEXT).setPreferredWidth(200);
        return t;
    }

    private JButton createDeleteButton() {
        JButton b = new JButton("Delete checked rows");
        b.addActionListener(new ActionListener() {
            @SuppressWarnings("unchecked")
            @Override
            public void actionPerformed(ActionEvent e) {
                Vector<Vector<Object>> removedRows = new Vector<>();
                DefaultTableModel modelLeft = (DefaultTableModel) tblLeft.getModel();
                for(int r=0;r!=modelLeft.getRowCount();++r)
                    if((Boolean) modelLeft.getValueAt(r,COL_CHECK)) {
                        removedRows.add((Vector<Object>) modelLeft.getDataVector().get(r));
                        modelLeft.removeRow(r--);
                    }

                DefaultTableModel modelRight = (DefaultTableModel) tblRight.getModel();
                for(Vector<Object> row : removedRows)
                    modelRight.addRow(row);
            }
        });
        return b;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame("Delete checked rows in JTable from button");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setContentPane(new TestTableDeleteRows());
                f.pack();
                f.setVisible(true);
            }
        });
    }
}

结果:

A run of the snippet