private void deleteActionPerformed() { // 获取关键字 String keyword = this.UserNameTxt.getText().trim(); // 创建表格行排序器 TableRowSorter<TableModel> sorter = new TableRowSorter<>(UserTable.getModel()); UserTable.setRowSorter(sorter); // 设置过滤器 RowFilter<TableModel, Integer> filter = new RowFilter<TableModel, Integer>() { public boolean include(Entry<? extends TableModel, ? extends Integer> entry) { String value = entry.getStringValue(4); return value.contains(keyword); } }; sorter.setRowFilter(filter); // 设置行过滤器 // 获取过滤后的结果的行数 int count = UserTable.getRowCount(); if (count == 0) { JOptionPane.showMessageDialog(this, "无法删除!", "提示", JOptionPane.INFORMATION_MESSAGE); return; } DefaultTableModel tableModel = (DefaultTableModel) UserTable.getModel(); tableModel.removeRow(count); }这个应该怎么修改才能实现上述功能,我现在输入它只显示特定行
时间: 2024-03-25 16:35:56 浏览: 59
根据您提供的代码,我看到您是通过添加行过滤器来实现对特定行的删除。如果您想要实现删除 JTable 中的任意行,可以直接删除 JTable 模型中的数据,而不需要使用行过滤器。
以下是一个修改过的代码示例,可以删除 JTable 中选中的行:
```java
private void deleteActionPerformed() {
// 获取选中的行号
int selectedRow = UserTable.getSelectedRow();
if (selectedRow == -1) {
JOptionPane.showMessageDialog(this, "请先选择要删除的行!", "提示", JOptionPane.INFORMATION_MESSAGE);
return;
}
// 从表格模型中删除选中的行
DefaultTableModel tableModel = (DefaultTableModel) UserTable.getModel();
tableModel.removeRow(selectedRow);
}
```
其中,`UserTable` 是您的 JTable 实例。
需要注意的是,这种方式只会删除 JTable 中的行,而不会删除数据源中的数据。如果您希望同时删除数据源中的数据,需要在删除 JTable 中的行之后,手动从数据源中删除相应的数据。
相关问题
public class MainActivity extends AppCompatActivity { private EditText searchEdit; private ImageButton searchBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); searchEdit = findViewById(R.id.search_edit); searchBtn = findViewById(R.id.search_btn); searchBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 执行搜索操作 performSearch(); } }); // 设置软键盘的搜索按钮监听器 searchEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH) { // 执行搜索操作 performSearch(); return true; } return false; } }); } private void performSearch() { String keyword = searchEdit.getText().toString().trim(); // 执行搜索操作,比如跳转到搜索结果页面 Intent intent = new Intent(this, SearchResultActivity.class); intent.putExtra("keyword", keyword); startActivity(intent); }}中actionId指的是什么
在这段代码中,`actionId`是指用户在软键盘上点击的动作(Action)的 ID,也就是指用户点击了软键盘上的哪个按钮。在这里,我们可以通过判断`actionId`是否等于 `EditorInfo.IME_ACTION_SEARCH`来判断用户是否点击了软键盘上的搜索按钮。如果用户点击了搜索按钮,就会执行 `performSearch()` 方法,进行搜索操作。
Java Swing图形用户界面编程实验代码按照实验步骤 1.实现页面菜单,菜单项包括按名字读取、按学号读取、按班级读取; 2.实现搜索框,用于输入名字或学号,根据名字或学号查询学生信息; 3.实现班级下拉列表,用于选择班级并按班级查询学生信息。 4.实现学生信息展示功能,展示从数据库中查询到的学生信息; 并使用SwingUtilities.invokeLater(JCUI::createUI);
以下是Java Swing图形用户界面编程实验的示例代码:
```
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class JCUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/student";
private static final String DATABASE_USER = "root";
private static final String DATABASE_PASSWORD = "password";
private JMenuBar menuBar;
private JMenu fileMenu;
private JMenuItem nameMenuItem;
private JMenuItem idMenuItem;
private JMenuItem classMenuItem;
private JPanel searchPanel;
private JTextField searchField;
private JButton searchButton;
private JPanel classPanel;
private JLabel classLabel;
private JComboBox<String> classComboBox;
private JButton classButton;
private JTable table;
private DefaultTableModel tableModel;
private Connection conn;
private Statement stmt;
public JCUI() {
super("学生信息查询");
setSize(800, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建菜单栏
menuBar = new JMenuBar();
fileMenu = new JMenu("文件");
nameMenuItem = new JMenuItem("按名字读取");
idMenuItem = new JMenuItem("按学号读取");
classMenuItem = new JMenuItem("按班级读取");
nameMenuItem.addActionListener(this);
idMenuItem.addActionListener(this);
classMenuItem.addActionListener(this);
fileMenu.add(nameMenuItem);
fileMenu.add(idMenuItem);
fileMenu.add(classMenuItem);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
// 创建搜索框和按钮
searchPanel = new JPanel();
searchPanel.setLayout(new BorderLayout());
searchField = new JTextField();
searchButton = new JButton("搜索");
searchButton.addActionListener(this);
searchPanel.add(searchField, BorderLayout.CENTER);
searchPanel.add(searchButton, BorderLayout.EAST);
searchPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
add(searchPanel, BorderLayout.NORTH);
// 创建班级选择框和按钮
classPanel = new JPanel();
classPanel.setLayout(new BorderLayout());
classLabel = new JLabel("班级:");
classComboBox = new JComboBox<String>();
classButton = new JButton("查询");
classButton.addActionListener(this);
classPanel.add(classLabel, BorderLayout.WEST);
classPanel.add(classComboBox, BorderLayout.CENTER);
classPanel.add(classButton, BorderLayout.EAST);
classPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
add(classPanel, BorderLayout.SOUTH);
// 创建表格
tableModel = new DefaultTableModel();
tableModel.addColumn("姓名");
tableModel.addColumn("学号");
tableModel.addColumn("班级");
tableModel.addColumn("性别");
tableModel.addColumn("年龄");
table = new JTable(tableModel);
table.setPreferredScrollableViewportSize(new Dimension(600, 400));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane, BorderLayout.CENTER);
// 连接数据库
try {
Class.forName(DRIVER_CLASS);
conn = DriverManager.getConnection(DATABASE_URL, DATABASE_USER, DATABASE_PASSWORD);
stmt = conn.createStatement();
String sql = "SELECT DISTINCT class FROM student";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String className = rs.getString("class");
classComboBox.addItem(className);
}
rs.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == nameMenuItem) {
String name = JOptionPane.showInputDialog(this, "请输入姓名:");
if (name != null && !name.trim().isEmpty()) {
searchByName(name.trim());
}
} else if (e.getSource() == idMenuItem) {
String id = JOptionPane.showInputDialog(this, "请输入学号:");
if (id != null && !id.trim().isEmpty()) {
searchById(id.trim());
}
} else if (e.getSource() == classMenuItem) {
String className = JOptionPane.showInputDialog(this, "请输入班级:");
if (className != null && !className.trim().isEmpty()) {
searchByClass(className.trim());
}
} else if (e.getSource() == searchButton) {
String keyword = searchField.getText().trim();
if (!keyword.isEmpty()) {
searchByNameOrId(keyword);
}
} else if (e.getSource() == classButton) {
String className = (String) classComboBox.getSelectedItem();
if (className != null && !className.trim().isEmpty()) {
searchByClass(className.trim());
}
}
}
private void searchByName(String name) {
clearTable();
try {
String sql = "SELECT * FROM student WHERE name LIKE '%" + name + "%'";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String[] row = new String[5];
row[0] = rs.getString("name");
row[1] = rs.getString("id");
row[2] = rs.getString("class");
row[3] = rs.getString("gender");
row[4] = String.valueOf(rs.getInt("age"));
tableModel.addRow(row);
}
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void searchById(String id) {
clearTable();
try {
String sql = "SELECT * FROM student WHERE id='" + id + "'";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String[] row = new String[5];
row[0] = rs.getString("name");
row[1] = rs.getString("id");
row[2] = rs.getString("class");
row[3] = rs.getString("gender");
row[4] = String.valueOf(rs.getInt("age"));
tableModel.addRow(row);
}
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void searchByClass(String className) {
clearTable();
try {
String sql = "SELECT * FROM student WHERE class='" + className + "'";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String[] row = new String[5];
row[0] = rs.getString("name");
row[1] = rs.getString("id");
row[2] = rs.getString("class");
row[3] = rs.getString("gender");
row[4] = String.valueOf(rs.getInt("age"));
tableModel.addRow(row);
}
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void searchByNameOrId(String keyword) {
clearTable();
try {
String sql = "SELECT * FROM student WHERE name LIKE '%" + keyword + "%' OR id='" + keyword + "'";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String[] row = new String[5];
row[0] = rs.getString("name");
row[1] = rs.getString("id");
row[2] = rs.getString("class");
row[3] = rs.getString("gender");
row[4] = String.valueOf(rs.getInt("age"));
tableModel.addRow(row);
}
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void clearTable() {
int rowCount = tableModel.getRowCount();
for (int i = rowCount - 1; i >= 0; i--) {
tableModel.removeRow(i);
}
}
public static void createUI() {
JCUI ui = new JCUI();
ui.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(JCUI::createUI);
}
}
```
注意:这只是一个示例代码,实际项目中需要根据具体需求进行修改和完善。
阅读全文