gpt4 book ai didi

java - JTable 排序不显示图像

转载 作者:行者123 更新时间:2023-11-29 05:56:44 27 4
gpt4 key购买 nike

所以我已经成功地在我的小程序中实现了搜索功能,但是当我单击按钮进行排序时,它工作正常但图像不显示。这是我为分拣机添加的代码,它工作正常,但每行的图像没有显示。当我取出这段代码时,图像出现了,但排序不起作用。排序时我可以让图像显示吗?

    // Sorter Code. Images show up when this code gets taken out.
table = new JTable(model);
final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
table.setRowSorter(sorter);
search_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = search.getText();
if (text.length() == 0) {
sorter.setRowFilter(null);
} else {
sorter.setRowFilter(RowFilter.regexFilter(text));
}
}
});
// sorter code ends here.

最佳答案

  • 必须将 JTable 的 View 与其模型同步,

  • 查看方法convertXxxIndexToXxx

  • int modelRow = convertRowIndexToModel(row); 添加到您的 RendererprepareRenderer

  • example convertRowIndexToModel

编辑

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.regex.PatternSyntaxException;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.*;

public class TableIcon extends JFrame implements Runnable {

private static final long serialVersionUID = 1L;
private JTable table;
private JLabel myLabel = new JLabel("waiting");
private int pHeight = 40;
private boolean runProcess = true;
private int count = 0;
private JTextField filterText = new JTextField(15);

public TableIcon() {
ImageIcon errorIcon = (ImageIcon) UIManager.getIcon("OptionPane.errorIcon");
ImageIcon infoIcon = (ImageIcon) UIManager.getIcon("OptionPane.informationIcon");
ImageIcon warnIcon = (ImageIcon) UIManager.getIcon("OptionPane.warningIcon");
String[] columnNames = {"Picture", "Description"};
Object[][] data = {{errorIcon, "About"}, {infoIcon, "Add"}, {warnIcon, "Copy"},};
DefaultTableModel model = new DefaultTableModel(data, columnNames) {

private static final long serialVersionUID = 1L;
// Returning the Class of each column will allow different
// renderers to be used based on Class

@Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
table = new JTable(model);
table.setRowHeight(pHeight);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
table.setRowSorter(sorter);
filterText.setMaximumSize(new Dimension(400, 30));
filterText.setFont(new Font("Serif", Font.BOLD, 20));
filterText.setForeground(Color.BLUE);
filterText.getDocument().addDocumentListener(new DocumentListener() {

private void searchFieldChangedUpdate(DocumentEvent evt) {
String text = filterText.getText();
if (text.length() == 0) {
sorter.setRowFilter(null);
table.clearSelection();
} else {
try {
sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
} catch (PatternSyntaxException pse) {
JOptionPane.showMessageDialog(null, "Bad regex pattern", "Bad regex pattern", JOptionPane.ERROR_MESSAGE);
}
}
}

@Override
public void insertUpdate(DocumentEvent evt) {
searchFieldChangedUpdate(evt);
}

@Override
public void removeUpdate(DocumentEvent evt) {
searchFieldChangedUpdate(evt);
}

@Override
public void changedUpdate(DocumentEvent evt) {
searchFieldChangedUpdate(evt);
}
});
add(filterText, BorderLayout.NORTH);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane, BorderLayout.CENTER);
myLabel.setPreferredSize(new Dimension(200, pHeight));
myLabel.setHorizontalAlignment(SwingConstants.CENTER);
add(myLabel, BorderLayout.SOUTH);
new Thread(this).start();
}

public void run() {
while (runProcess) {
try {
Thread.sleep(1250);
} catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
ImageIcon myIcon = (ImageIcon) table.getModel().getValueAt(count, 0);
String lbl = "JTable Row at : " + count;
myLabel.setIcon(myIcon);
myLabel.setText(lbl);
count++;
if (count > 2) {
count = 0;
}
}
});
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
TableIcon frame = new TableIcon();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLocation(150, 150);
frame.pack();
frame.setVisible(true);
}
});
}
}

关于java - JTable 排序不显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11814482/

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