gpt4 book ai didi

java - JTable RowFilter 不过滤初始调用

转载 作者:行者123 更新时间:2023-12-01 12:29:41 24 4
gpt4 key购买 nike

毫无疑问,这将是一个简单的解决方案,但像往常一样,我被难住了。

我正在尝试使用 JCheckBox 过滤掉 JTable 中不可渲染的字体系列名称。 JTable 包含已安装的字体系列名称(第 0 列)、示例字母表(第 1 列)和示例数字(第 2 列)。

第一列和第二列以其相应的字体呈现,并且如果文本在单元格无法渲染,使用 JTable.setValueAt(Object, int, int) 将文本替换为“不可渲染”。

然后我使用“不可渲染”作为过滤器。但是,仅过滤可见的行。当我滚动到表格底部并应用过滤器时,整个表格都会根据需要进行过滤。

我还注意到,当我移动水平分隔线时,分隔线在更改的单元格上移动时不会重新绘制自身。对此的任何帮助也将不胜感激。

import java.awt.*;
import java.awt.event.*;

import java.util.*;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;

class run
{
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
Viewer viewer = new Viewer();

JFrame jframe = new JFrame();
jframe.add(viewer);
jframe.createBufferStrategy(1);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setLocation(50,50);
jframe.pack();
jframe.setVisible(true);
}
});
}
}

//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

class Viewer extends JPanel
{
final static long serialVersionUID = 0;

String string_alphabet = "abcdefghijklmnopqrstuvwxyz";
String string_digits = "0123456789";
String string_punctuation = "`!:\";\',.-/(){}[]<>?";
String string_symbols = "~@#$%^&*_+=|\\";

String string_installedFontsColumnNames[] = {"installed fonts","sample","sample"};
String string_installedFonts[];

DefaultTableModel defaultTableModel_installedFonts;
JTable jtable_installedFonts;

JSplitPane jsplitPane_horizontal;
JSplitPane jsplitPane_vertical;

JCheckBox jcheckBox_nonRenderable;

TableRowSorter<TableModel> tableRowSorter;

public Viewer()
{
this.setLayout(new BorderLayout());

JPanel jpanel_installedFonts = new JPanel(new BorderLayout());

GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();

string_installedFonts = graphicsEnvironment.getAvailableFontFamilyNames();

Object object_tableData[][] = new Object[string_installedFonts.length][string_installedFontsColumnNames.length];

for (int row = 0; row < object_tableData.length; row++)
{
Object object_columnData[] = new Object[string_installedFontsColumnNames.length];

object_columnData[0] = string_installedFonts[row];
object_columnData[1] = string_alphabet;
object_columnData[2] = string_digits;

object_tableData[row] = object_columnData;
}

defaultTableModel_installedFonts = new DefaultTableModel(object_tableData,string_installedFontsColumnNames);

tableRowSorter = new TableRowSorter<TableModel>(defaultTableModel_installedFonts);

jtable_installedFonts = new JTable(defaultTableModel_installedFonts);
jtable_installedFonts.setRowSorter(tableRowSorter);

jtable_installedFonts.getColumnModel().getColumn(0).setCellRenderer(new ColumnRenderer0());
jtable_installedFonts.getColumnModel().getColumn(1).setCellRenderer(new ColumnRenderer1());
jtable_installedFonts.getColumnModel().getColumn(2).setCellRenderer(new ColumnRenderer1());

JScrollPane jscrollPane_installedFonts = new JScrollPane(jtable_installedFonts);

jpanel_installedFonts.add(jscrollPane_installedFonts,BorderLayout.CENTER);

JPanel jpanel_visibilityOptions = new JPanel(new GridLayout(1,2));
jpanel_visibilityOptions.setBackground(Color.gray);

JPanel jpanel_nonRenderable = new JPanel();
jpanel_nonRenderable.setBackground(Color.lightGray);

jcheckBox_nonRenderable = new JCheckBox("hide non-renderable fonts",false);
jcheckBox_nonRenderable.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent actionEvent)
{
RowFilter<Object,Object> filter = null;

if (jcheckBox_nonRenderable.isSelected())
{
filter = RowFilter.notFilter(RowFilter.regexFilter("non-renderable"));
}

tableRowSorter.setRowFilter(filter);
}
});

jpanel_nonRenderable.add(jcheckBox_nonRenderable,BorderLayout.CENTER);

jpanel_visibilityOptions.add(new JPanel());
jpanel_visibilityOptions.add(jpanel_nonRenderable);

jpanel_installedFonts.add(jpanel_visibilityOptions,BorderLayout.SOUTH);

jsplitPane_horizontal = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jpanel_installedFonts,new JPanel());
jsplitPane_horizontal.setBorder(new EmptyBorder(0,0,0,0));

jsplitPane_vertical = new JSplitPane(JSplitPane.VERTICAL_SPLIT,jsplitPane_horizontal,new JPanel());
jsplitPane_vertical.setBorder(new EmptyBorder(0,0,0,0));

this.add(jsplitPane_vertical,BorderLayout.CENTER);
}
}

//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

class ColumnRenderer0 implements TableCellRenderer
{
JLabel jlabel;

Color color_background;
Color color_foreground;

String string_cellText;

public ColumnRenderer0()
{
jlabel = new JLabel();
jlabel.setBorder(new EmptyBorder(0,2,0,2));
jlabel.setHorizontalAlignment(JLabel.LEFT);
jlabel.setOpaque(true);
}

public Component getTableCellRendererComponent(JTable jtable, Object object, boolean selected, boolean focus, int row, int column)
{
string_cellText = (String)object;

if (selected)
{
color_background = jtable.getSelectionBackground();
color_foreground = jtable.getSelectionForeground();
}
else
{
color_background = jtable.getBackground();
color_foreground = jtable.getForeground();
}

jlabel.setText(string_cellText);
jlabel.setBackground(color_background);
jlabel.setForeground(color_foreground);

return jlabel;
}
}

//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

class ColumnRenderer1 implements TableCellRenderer
{
JLabel jlabel;

Color color_background;
Color color_foreground;

String string_cellText;
String string_fontFamily;

Font font;
Font font_jtable;

public ColumnRenderer1()
{
jlabel = new JLabel();
jlabel.setBorder(new EmptyBorder(0,5,0,5));
jlabel.setHorizontalAlignment(JLabel.CENTER);
jlabel.setOpaque(true);
}

public Component getTableCellRendererComponent(JTable jtable, Object object, boolean selected, boolean focus, int row, int column)
{
string_cellText = (String)object;

if (selected)
{
color_background = jtable.getSelectionBackground();
color_foreground = jtable.getSelectionForeground();
}
else
{
color_background = jtable.getBackground();
color_foreground = jtable.getForeground();
}

string_fontFamily = (String)jtable.getValueAt(row,0);

font_jtable = jtable.getFont();

font = new Font(string_fontFamily,Font.PLAIN,font_jtable.getSize());

if (font.canDisplayUpTo(string_cellText) != -1)
{
font = new Font(font_jtable.getFamily(),Font.BOLD,font_jtable.getSize());
string_cellText = "non-renderable";
jtable.setValueAt(string_cellText,row,column);
color_foreground = Color.red;

}

jlabel.setFont(font);
jlabel.setText(string_cellText);
jlabel.setBackground(color_background);
jlabel.setForeground(color_foreground);

return jlabel;
}
}

最佳答案

问题是,在渲染单元格之前,文本仍保留为您最初设置的内容。不可见的单元格不会被渲染,因此模型不会被更新。由于不同的原因,过滤发生在不同的时间。

您不应该从渲染器内修改模型的状态,而应该在构建表格模型时预先确定字体是否可渲染并相应地设置单元格值...

关于java - JTable RowFilter 不过滤初始调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26035204/

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