gpt4 book ai didi

java - 无法从 JTable 中删除空白行

转载 作者:行者123 更新时间:2023-11-30 03:29:38 25 4
gpt4 key购买 nike

我创建了一个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);

最佳答案

您可以使用 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了解更多详情

关于java - 无法从 JTable 中删除空白行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29377213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com