简体   繁体   English

如何从java中的beantablemodel中删除多行?

[英]How to delete multiple rows from beantablemodel in java?

I want to delete multiple rows at a times on selection.我想在选择时一次删除多行。

Here is my code:这是我的代码:

            int[] indexList = queryTable.getSelectedRows();
            queryTableModel.removeRows(indexList);
            queryTable.clearSelection();
            SwingUtilities.updateComponentTreeUI(queryTable);

Please help.请帮忙。

Here is simple example of removing selected rows from model:这是从模型中删除选定行的简单示例:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class TestFrame extends JFrame {

    private DefaultTableModel model;

    public TestFrame() {
        init();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void init() {
        final JTable t = new JTable(model = new DefaultTableModel(0,1));
        for(int i =0;i<10;i++){
            model.addRow(new Object[]{i});
        }
        JButton removeSelected = new JButton("remove");
        removeSelected.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int[] selectedRows = t.getSelectedRows();
                for(int i=selectedRows.length-1;i >= 0;i--){
                    model.removeRow(selectedRows[i]);;
                }
            }
        });
        add(new JScrollPane(t));
        add(removeSelected,BorderLayout.SOUTH);
    }


    public static void main(String args[]) {
        new TestFrame();
    }

}

Assuming you are using DefaultTableModel as table model, this should be enough:假设您使用DefaultTableModel作为表模型,这应该足够了:

int[] viewIndexes = table.table.getSelectedRows();
for(int i = viewIndexes.length - 1; i >= 0; i-- ) {
    int modelIndex = table.convertRowIndexToModel(viewIndexes[i]);
    ((DefaultTableModel)table.getModel()).removeRow(modelIndex);
}

Never forget to convert the selected indexes from view to model .永远不要忘记将选定的索引从view转换为model Otherwise you'll have problems if your table is sorted.否则,如果您的表格已排序,您就会遇到问题。

If you are using a custom TableModel , the process is almost the same, there won't be great differences.如果您使用的是自定义TableModel ,则过程几乎相同,不会有很大差异。

In addition, you don't have to do nothing to update the view after adding/deleting/updating data, the model will notify the view in such events and this last one will be updated accordingly.此外,在添加/删除/更新数据后,您无需执行任何操作来更新视图模型会在此类事件中通知视图,并且最后一个将相应地更新。

See How to use Tables tutorial for further details.有关更多详细信息,请参阅如何使用表格教程。

How to delete multiple rows from beantablemodel in java?如何从java中的beantablemodel中删除多行?

There is no BeanTableModel in Java. Java 中没有 BeanTableModel。

If you happen to be referring to this Bean Table Model then you can use the removeRows(...) method.如果您碰巧指的是这个Bean 表模型,那么您可以使用removeRows(...)方法。

// Selected rows by the user
int selectedRows[] = table.getSelectedRows();

if (selectedRows.length > 0) {
    // Every time a row is removed, the array will fill the empty gap moving 1 position all 
    // items that come after the removed item, so the next position to remove will be 
    // index + 1, then +2, +3, etc...
    int compensation = 0;

    // Removes row from selected index and compensates the misalignment
    for (int row : selectedRows) {
        model.removeRow(row - compensation++);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM