我不熟悉在JAVA中实现GUI组件,也不熟悉JTables。我正在尝试通过JTextField(在此代码中称为searchTable)内的用户文本来搜索在此代码中称为myTable的JTable。
我尝试在我的代码中实现一个RowSorter,当用户按下Enter时,该元素将通过在JTextField中查找并显示与用户输入匹配的元素来进行更新,然后通过返回其原始状态进行更新状态,显示用户执行其他操作时JTable中的所有元素。
以下是我当前用于JTable的GUI元素的代码:
(我也尝试过两个for循环,一个循环遍历行,一个循环遍历行。这对我来说并不起作用,因此请在下面注释一下)。
package westminsterlibraryrun;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import javax.swing.*;
import javax.swing.RowFilter.*;
/*
*
* @author w1640643
*/
public class LibraryItemTableGUI extends JFrame implements ActionListener {
JTable myTable;
JTextField searchTable;
LibraryItemTable tableModel;
ArrayList<LibraryItem> itemList;
public LibraryItemTableGUI(ArrayList<LibraryItem> itemList) {
this.itemList = itemList;
tableModel = new LibraryItemTable(itemList);
myTable = new JTable(tableModel);
setBounds(10, 10, 400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myTable.setAutoCreateRowSorter(true);
myTable.getColumnModel().getColumn(4).setCellRenderer(new DefaultTableCellRenderer() {
Color originalcolor = null;
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component renderer = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (value == "Borrowed") {
renderer.setBackground(Color.red);
} else {
renderer.setBackground(Color.GREEN);
}
return renderer;
}
});
searchTable = new JTextField("Enter Item TItle You Wish to Search");
searchTable.addActionListener(this);
JScrollPane scrollPane = new JScrollPane(myTable);
scrollPane.setPreferredSize(new Dimension(380, 280));
JPanel panel = new JPanel();
panel.add(scrollPane);
panel.add(searchTable);
add(scrollPane, BorderLayout.CENTER);
add(searchTable, BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent e) {
RowFilter<LibraryItemTable, String> filterJTable = new RowFilter<LibraryItemTable, String>() {
@Override
public boolean include(Entry<? extends LibraryItemTable, ? extends String> entry) {
for (int i = entry.getValueCount() - 1; i >= 0; i--) {
if (entry.getStringValue(i).equalsIgnoreCase(searchTable.getText())) {
return true;
}
}
System.out.println("False");
return false;
}
};
}
}
// @Override
// public void actionPerformed(ActionEvent e) {
// for (int i = 0; i < myTable.getRowCount(); i++) {
// for (int j = 0; j < myTable.getColumnCount(); j++) {
// String jtableChecker = searchTable.getText();
// if (myTable.getModel().getValueAt(i,
//j).equals(jtableChecker)) {
// searchTable.setText("Item Search: " + //myTable.getModel().getValueAt(i, j));
下面是我的表模型代码:
package westminsterlibraryrun;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
/**
*
* @author w1640643
*/
public class LibraryItemTable extends AbstractTableModel {
private String[] columnTitles = {"Item ISBN", "Item Title", "Item Type", "Item Category", /* "Item Published Date", */ "Item Status"};
private ArrayList<LibraryItem> itemList;
public LibraryItemTable(ArrayList<LibraryItem> itemList) {
this.itemList = itemList;
}
@Override
public int getRowCount() {
return itemList.size();
}
@Override
public int getColumnCount() {
return columnTitles.length;
}
@Override
public Object getValueAt(int rows, int cols) {
Object tb = null;
if (cols == 0) {
tb = itemList.get(rows).getISBN();
} else if (cols == 1) {
tb = itemList.get(rows).getTitle();
} else if (cols == 2) {
tb = itemList.get(rows).getItem();
} else if (cols == 3) {
tb = itemList.get(rows).getCategory();
// } else if (cols == 4) {
// tb = itemList.get(rows).getPublishedDate();
} else if (cols == 4) {
tb = itemList.get(rows).getStatus();
}
return tb;
}
@Override
public String getColumnName(int cols) {
return columnTitles[cols];
}
} //end
我将要运行的指针以及RowSorter(我试图了解RowSorter的给定API页面而对它的工作原理没有真正的了解)的整体工作原理将不胜感激。
谢谢。
更新:
我使用以下代码修复了我的代码:
package westminsterlibraryrun;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import javax.swing.*;
import javax.swing.RowFilter.*;
/*
*
* @author w1640643
*/
public class LibraryItemTableGUI extends JFrame implements ActionListener {
JTable myTable;
JTextField searchTable;
LibraryItemTable tableModel;
ArrayList<LibraryItem> itemList;
public LibraryItemTableGUI(ArrayList<LibraryItem> itemList) {
this.itemList = itemList;
tableModel = new LibraryItemTable(itemList);
myTable = new JTable(tableModel);
setBounds(10, 10, 400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myTable.setAutoCreateRowSorter(true);
myTable.getColumnModel().getColumn(4).setCellRenderer(new DefaultTableCellRenderer() {
Color originalcolor = null;
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component renderer = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (value == "Borrowed") {
renderer.setBackground(Color.red);
} else {
renderer.setBackground(Color.GREEN);
}
return renderer;
}
});
searchTable = new JTextField("Enter Item TItle You Wish to Search");
searchTable.addActionListener(this);
JScrollPane scrollPane = new JScrollPane(myTable);
scrollPane.setPreferredSize(new Dimension(380, 280));
JPanel panel = new JPanel();
panel.add(scrollPane);
panel.add(searchTable);
add(scrollPane, BorderLayout.CENTER);
add(searchTable, BorderLayout.SOUTH);
}
public void searchJTable(String query) {
TableRowSorter<TableModel> table = new TableRowSorter<>(tableModel);
myTable.setRowSorter(table);
table.setRowFilter(RowFilter.regexFilter(query));
}
@Override
public void actionPerformed(ActionEvent e) {
String query = searchTable.getText();
searchJTable(query);
}
}
// @Override
// public void actionPerformed(ActionEvent e) {
// for (int i = 0; i < myTable.getRowCount(); i++) {
// for (int j = 0; j < myTable.getColumnCount(); j++) {
// String jtableChecker = searchTable.getText();
// if (myTable.getModel().getValueAt(i, j).equals(jtableChecker)) {
// searchTable.setText("Item Search: " + myTable.getModel().getValueAt(i, j));
但是,上面的代码现在是好的做法吗? 我仍然对为什么它以前不起作用感到好奇?