gpt4 book ai didi

java - 隐藏/显示 jtable 中的行

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

我使用 defaulttablemodel 创建了表,上面有组合框。我需要根据组合框值(10,20,30,40)隐藏/显示已经创建和填充的行。

private void newFilter(int numberOfRows) {
RowFilter<TableModel, Integer> filter = new RowFilter<TableModel, Integer>() {
@Override
public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry) {
int i = table.convertRowIndexToView(entry.getIdentifier());

return i < numberOfRows;

}

};

((TableRowSorter) table.getRowSorter()).setRowFilter(filter);
}


combobox.addItemListener(new ItemListener() {

@Override
public void itemStateChanged(ItemEvent e) {

String s = (String) combobox.getSelectedItem();

if (s.equals("10")) {
newFilter(10);
} else if (s.equals("15")) {
newFilter(15);
} else if (s.equals("20")) {
newFilter(20);
} else if (s.equals("30")) {
newFilter(30);
} else if (s.equals("40")) {
newFilter(40);

}

}
});

我尝试了这样的操作,直到我再次选择相同的值(例如 10),它才会起作用,然后它什么也不做。我该怎么办?

最佳答案

int i = table.convertRowIndexToView(entry.getIdentifier());

当行被过滤后,上述语句将返回 -1。因此,一旦过滤了一行,它就会保持过滤状态。

然后我尝试使用:

((TableRowSorter) table.getRowSorter()).setRowFilter(null);
((TableRowSorter) table.getRowSorter()).setRowFilter(filter);

尝试重置过滤器,但这似乎不起作用,因为现在“i”的值返回 0。

所以最后我尝试了:

private void newFilter(int numberOfRows)
{
final RowFilter<TableModel, Integer> filter = new RowFilter<TableModel, Integer>()
{
@Override
public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry)
{
int i = table.convertRowIndexToView(entry.getIdentifier());

//System.out.println(i + " : " + numberOfRows + " : " + entry.getIdentifier());

return i < numberOfRows;
}

};

((TableRowSorter) table.getRowSorter()).setRowFilter(null);

SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
((TableRowSorter) table.getRowSorter()).setRowFilter(filter);
}
});

}

而且它似乎有效。

希望有人有更好的解决方案。

这是我用于测试的完整代码:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
//import java.util.List;
import javax.swing.*;
import javax.swing.event.*;
//import javax.swing.text.*;
//import java.beans.*;
import javax.swing.border.*;
//import javax.swing.plaf.*;
//import javax.swing.text.*;
import javax.swing.table.*;
//import java.io.*;
//import javax.imageio.*;
//import java.awt.geom.*;

public class SSCCE extends JPanel
{
private JTable table;

public SSCCE()
{
setLayout( new BorderLayout() );

JComboBox<Integer> comboBox = new JComboBox<Integer>();
comboBox.addItem( new Integer(10) );
comboBox.addItem( new Integer(20) );
comboBox.addItem( new Integer(30) );
comboBox.addItem( new Integer(40) );

comboBox.addItemListener( new ItemListener()
{
@Override
public void itemStateChanged(ItemEvent e)
{
Integer value = (Integer)comboBox.getSelectedItem();
newFilter( value );
}
});
add(comboBox, BorderLayout.NORTH);

table = new JTable(40, 3);
table.setAutoCreateRowSorter(true);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane, BorderLayout.CENTER);
}

private void newFilter(int numberOfRows)
{
final RowFilter<TableModel, Integer> filter = new RowFilter<TableModel, Integer>()
{
@Override
public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry)
{
int i = table.convertRowIndexToView(entry.getIdentifier());

//System.out.println(i + " : " + numberOfRows + " : " + entry.getIdentifier());

return i < numberOfRows;
}

};

((TableRowSorter) table.getRowSorter()).setRowFilter(null);

SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
((TableRowSorter) table.getRowSorter()).setRowFilter(filter);
}
});

}

private static void createAndShowGUI()
{
JPanel panel = new JPanel();

JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SSCCE());
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}

public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}

关于java - 隐藏/显示 jtable 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30444941/

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