无法从JTable中删除空白行

时间:2015-03-31 19:45:58

标签: java swing jtable rowfilter

我创建了一个JTable,有时候会有空行。我想自动删除它们。我知道你可以通过model.removeRow(0)手动完成,这会删除第一行,但是这个表需要能够自动完成。但是,当我在下面运行以下代码时,它什么也没做。它是为了查看行中的第一个单元格,如果它是空的,在这种情况下,行的其余部分也是如此,它应该删除该行。我非常感谢克服这个问题的任何帮助

//CompData...
nextPosition=0
String[] aHeaders = {"Athlete ID","Forename","Surname","On The Team"};

    model = new DefaultTableModel(compTableData,aHeaders)
    {
        @Override
        public boolean isCellEditable(int row, int column) 
        {
            if (column < 3) 
            {
                return false;
            } 

            else 
            {
                return true;
            }
        }

        public boolean removeRow(int row, int column)
        {
            for(int i=0;i<nextPosition;i++)
            {
                if(athTable.getModel().getValueAt(i,0).equals(""))
                {
                    return row == i;
                }
            }
            return false;
        }
    };

    athTable = new JTable(model);

1 个答案:

答案 0 :(得分:2)

您使用RowFilter根据您的条件过滤/退出行,与模型无关

Filter

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                DefaultTableModel model = new DefaultTableModel(new String[]{"Fruit"}, 0);
                model.addRow(new String[]{"Banana"});
                model.addRow(new String[]{""});
                model.addRow(new String[]{"Apple"});
                model.addRow(new String[]{""});
                model.addRow(new String[]{"Strewberry"});
                model.addRow(new String[]{""});
                model.addRow(new String[]{"Grape"});
                model.addRow(new String[]{""});

                JTable table = new JTable(model);
                table.setAutoCreateRowSorter(true);
                TableRowSorter sorter = (TableRowSorter) table.getRowSorter();
                sorter.setRowFilter(new RowFilter<TableModel, Integer>() {
                    @Override
                    public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry) {
                        boolean included = true;
                        Object cellValue = entry.getModel().getValueAt(entry.getIdentifier(), 0);
                        if (cellValue == null || cellValue.toString().trim().isEmpty()) {
                            included = false;
                        }
                        return included;
                    }
                });


                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

请参阅 How to Use TablesHow to use tables, Sorting and Filtering了解详情