Java 如何在 JTable 中隐藏(使不可见)行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/944006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I hide (make invisible) row in JTable?
提问by joycollector
Is there any way to do it?
有什么办法吗?
采纳答案by Michael Borgwardt
The besteasiest way would be to remove the corresponding element from the model.
在最好的最简单的方法是从模型中删除相应的元素。
回答by willcodejavaforfood
Check out Sun's Tutorial for JTablesand look at the Sorting and Filtering section.
查看Sun 的 JTables 教程并查看排序和过滤部分。
回答by willcodejavaforfood
There is the RowFilter<DefaultTableModel, Object>
class you can use to filter out rows. The DefaultTableModel can be replaced by your own model. To filter, implement the method
还有就是RowFilter<DefaultTableModel, Object>
,你可以用它来过滤掉行类。DefaultTableModel 可以替换为您自己的模型。要过滤,实现方法
@Override
public boolean include(Entry entry) {
// All rows are included if no filter is set
if (filterText.isEmpty())
return true;
// If any of the column values contains the filter text,
// the row can be shown
for (int i = 0; i < entry.getValueCount(); i++) {
String value = entry.getStringValue(i);
if (value.toLowerCase().indexOf(filterText) != -1)
return true;
}
return false;
}
When accessing rows, for instance listening to ListSelectionEvents, do not forget to translate the visible row index to the complete row index in your model. Java provides a function for this as well:
访问行时,例如侦听 ListSelectionEvents,不要忘记将可见行索引转换为模型中的完整行索引。Java 也为此提供了一个函数:
public void valueChanged(ListSelectionEvent e) {
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
int visibleRowIndex = ... // Get the index of the selected row
// Convert from the selection index based on the visible items to the
// internal index for all elements.
int internalRowIndex = tableTexts
.convertRowIndexToModel(visibleRowIndex);
...
}
回答by Mike
You could set up a arraylists for each column that are populated by values filtered for and implement these in a custom renderer. If a cells entire row values are not met the renderer recursively calls itself with row+1.
您可以为每个列设置一个数组列表,这些列由过滤的值填充,并在自定义渲染器中实现这些值。如果未满足单元格的整行值,渲染器会使用 row+1 递归调用自身。
if the cells row does meet criteria, it gets rendered, another arraylist stores the row numbers already rendered, its best explained by example this method is in customer renderer extends JLabel
如果单元格行确实符合条件,它会被渲染,另一个数组列表存储已经渲染的行号,最好通过示例来解释这个方法是在客户渲染器中扩展 JLabel
public Component getTableCellRendererComponent(JTable table, Object color,
boolean isSelected, boolean hasFocus, int row, int column) {
Object value;
String s;
try {
if (row == 0) {
drawn[column].clear();
}// drawn is arraylist which stores cols rend
if (row > table.getModel().getDataVector.size()) {
return null;
}// if we go too far
if (drawn[column].contains(Integer.toString(row)) == true) {
// already rendered?
return getTableCellRendererComponent(table, color, isSelected,
hasFocus, (row + 1), column);
}// render row+1
for (int i = 0; i < filters.length; i++) {
value = table.getModel().getValueAt(row, i);
s = (i == 1) ? df.format(value) : value.toString();
if (filters[i].contains(s) != true) {
//try and put in next row, if succeeds when it reaches column 8 it adds row to
return getTableCellRendererComponent(table, color,
isSelected, hasFocus, (row + 1), column);
}
}
value = table.getModel().getValueAt(row, column);
setFont(getFont().deriveFont(Font.BOLD));
if ((isSelected == false)) {
if ((column == 1)) {
setForeground(Color.magenta);
}// just formatting
else {
setForeground(Color.black);
setBackground(Color.white);
}
} else {
setBackground(Color.blue);
setForeground(Color.white);
}
if ((column == 1))// col 1 is a date, other columns strings
{
setText((value == null) ? "" : df.format(value));
} else {
setText((value == null) ? "" : value.toString());
}
todayStr = df.format(new java.util.Date());
dateval = table.getModel().getValueAt(row, 1);
String datevalStr = df.format(dateval);
if (datevalStr.equals(todayStr)) {
setForeground(Color.red);
}
drawn[column].add(Integer.toString(row));// mark row as rendered
} catch (Exception e) {
e.getMessage();
return null;
}
return this;
}