gpt4 book ai didi

java - 在 JTable 中搜索 - 未获得正确的输出

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

我在 JTable 中搜索时遇到问题。我在Stack Overflow上找到了一个方法,但似乎不起作用。当我现在搜索时,我什么也没得到。

说实话,我不太擅长reg。表达式。

相关代码位于我的 searchTF keylistener 中,但我也想向您展示其余部分,以便您可以了解我正在尝试做什么。

我需要能够在我的 JTable 中进行搜索。

Implement search textField into jTable

这是我尝试实现的代码。我可能忘记了什么。

package gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import domains.Member;
import mappers.MemberMapper;

public class ShowMembersGUI {
private JFrame showMembersFrame;
private JButton editMemberBtn, closeBtn, removeMemberBtn;
private JTable table;
private JScrollPane scrollPane;
private JPanel p;
private Object id, fName, lName;
private int index;
private final MyTableModel myTableModel;
private JTextField searchTF;
private JLabel searchLbl;

public ShowMembersGUI() {
MemberMapper mMapper = new MemberMapper();
List<Member> data = mMapper.selectAllMembers();
myTableModel = new MyTableModel(data);

table = new JTable(myTableModel);
final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(
myTableModel);

scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setRowSorter(sorter);

searchLbl = new JLabel("Søg");
searchTF = new JTextField(10);
searchTF.addKeyListener(new KeyListener() {

@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {

searchTF.getDocument().addDocumentListener(
new DocumentListener() {

private void searchFieldChangedUpdate(
DocumentEvent evt) {
String text = searchTF.getText();
if (text.length() == 0) {
sorter.setRowFilter(null);
table.clearSelection();
} else {
try {
sorter.setRowFilter(RowFilter
.regexFilter("(?i)" + text,
4));
table.clearSelection();

} catch (Exception e) {
e.printStackTrace();
}
}
}

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

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

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

@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
});

editMemberBtn = new JButton("Rediger Medlem");
editMemberBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
EditMemberGUI editMemberGUI = new EditMemberGUI(
ShowMembersGUI.this);
int row = table.getSelectedRow();
// int column = table.getSelectedColumn();
id = (Object) table.getValueAt(row, 0);
Object fName = (Object) table.getValueAt(row, 1);
Object lName = (Object) table.getValueAt(row, 2);
Object address = (Object) table.getValueAt(row, 3);
Object zipCode = (Object) table.getValueAt(row, 4);
Object city = (Object) table.getValueAt(row, 5);
Object email = (Object) table.getValueAt(row, 6);
Object phoneNo = (Object) table.getValueAt(row, 7);

index = (Integer) id;
editMemberGUI.getfNameTF().setText(fName.toString());
editMemberGUI.getlNameTF().setText(lName.toString());
editMemberGUI.getAddressTF().setText(address.toString());
editMemberGUI.getZipCodeTF().setText(zipCode.toString());
editMemberGUI.getCityTF().setText(city.toString());
editMemberGUI.getEmailTF().setText(email.toString());
editMemberGUI.getPhoneNoTF().setText(phoneNo.toString());
editMemberGUI.showGUI();

}
});

removeMemberBtn = new JButton("Fjern Medlem");
removeMemberBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
// int column = table.getSelectedColumn();
id = (Object) table.getValueAt(row, 0);
fName = (Object) table.getValueAt(row, 1);
lName = (Object) table.getValueAt(row, 2);

Object[] options = { "Ja", "Nej" };
int n = JOptionPane
.showOptionDialog(null,
"Er du sikker du vil slette medlem?\n\n"
+ "Medlemsnr: " + id + "\n\n" + fName
+ " " + lName, "Advarsel",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options,
options[0]);

if (n == JOptionPane.YES_OPTION) {
MemberMapper mMapper = new MemberMapper();
int i = (Integer) id;
mMapper.removeMember(i);
myTableModel.removeMemberFromTable(row);
myTableModel.fireTableDataChanged();
} else {
}

}
});

closeBtn = new JButton("Close");
closeBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
showMembersFrame.setVisible(false);
}
});

p = new JPanel(new FlowLayout());

JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));

p1.add(searchLbl);
p1.add(searchTF);
p2.add(editMemberBtn);
p2.add(removeMemberBtn);
p2.add(closeBtn);

p.add(p1);
p.add(p2);

}

public void showGUI() {
showMembersFrame = new JFrame();
showMembersFrame.setTitle("Vis Medlemmer");
showMembersFrame.setLayout(new BorderLayout());
showMembersFrame.setSize(1000, 600);

showMembersFrame.add(scrollPane, BorderLayout.CENTER);
showMembersFrame.add(p, BorderLayout.SOUTH);
// showMembersFrame.pack();
showMembersFrame.setVisible(true);
}

public int getIndex() {
return index;
}

public void setIndex(int index) {
this.index = index;
}

public JTable getTable() {
return table;
}

public void setTable(JTable table) {
this.table = table;
}

}

class MyTableModel extends AbstractTableModel {

private static final long serialVersionUID = 1L;
private List<Member> members = new ArrayList<Member>();

public MyTableModel(List<Member> members) {
this.members = members;
}

public void addMember(Member m) {
members.add(m);
int row = members.size() - 1;
fireTableRowsInserted(row, row);
}

public void removeMemberFromTable(int row) {
members.remove(row);
fireTableRowsDeleted(row, row);
}

public void tableUpdate(int row, Member m) {
if (row >= 0 && row < members.size()) {
members.set(row, m);
fireTableRowsUpdated(row, row);
}
}

public List<Member> getMembers() {
return new ArrayList<Member>(members);
}

@Override
public int getColumnCount() {
return 8;
}

@Override
public int getRowCount() {
return members.size();
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Member m = members.get(rowIndex);

switch (columnIndex) {
case 0:
return m.getMemberId();

case 1:
return m.getfName();

case 2:
return m.getlName();

case 3:
return m.getAddress();

case 4:
return m.getZipCode();

case 5:
return m.getCity();

case 6:
return m.getEmail();

case 7:
return m.getPhoneNo();

default:
return "";
}
}

public String getColumnName(int column) {
switch (column) {
case 0:
return "MedlemsID";

case 1:
return "Fornavn";

case 2:
return "Efternavn";

case 3:
return "Adresse";

case 4:
return "Postnummer";

case 5:
return "By";

case 6:
return "Email";

case 7:
return "Telefonnr";
default:
return "";
}
}

public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0:
return String.class;

case 1:
return String.class;

case 2:
return String.class;

case 3:
return String.class;

case 4:
return String.class;

case 5:
return String.class;

case 6:
return String.class;

case 7:
return String.class;

default:
return String.class;
}
}
}

最佳答案

您在下一个设计不佳的代码中遇到的问题:

  searchTF.addKeyListener(new KeyListener() {

@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {

searchTF.getDocument().addDocumentListener(
new DocumentListener() {

private void searchFieldChangedUpdate(
DocumentEvent evt) {
String text = searchTF.getText();
if (text.length() == 0) {
sorter.setRowFilter(null);
} else {
try {
sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text,4));
} catch (Exception e) {
e.printStackTrace();
}
}
}

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

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

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

@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
});

首先,按照 @mKorbel 的建议,使用 DocumentListener 而不是 KeyListener

其次,据我所知,您只需要在按下 ENTER 时使用搜索,为此您可以使用 keyBindings .

并且,searchTF.getDocument().addDocumentListener(...) 在您的类中的这段代码向您的字段添加一个新的 DocumentListener,在构建时执行一次字段。

使用下一个代码来达到您的目的:

    InputMap im = searchTF.getInputMap(JTextField.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = searchTF.getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "ENTER");
am.put("ENTER", new AbstractAction() {

@Override
public void actionPerformed(ActionEvent arg0) {
String text = searchTF.getText();
if (text.isEmpty()) {
sorter.setRowFilter(null);
} else {
try {
sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text, 4));
} catch (Exception e) {
e.printStackTrace();
}
}
}
});

关于java - 在 JTable 中搜索 - 未获得正确的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20215698/

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