swing表格记录无法选中
时间: 2023-09-08 15:12:26 浏览: 44
如果你指的是在 `JTable` 中选中某个单元格或某一行时没有效果,可能是因为你没有设置 `JTable` 的选择模式。你可以使用 `setSelectionMode()` 方法设置 `ListSelectionModel`,如下所示:
```java
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 只能选择单个单元格或行
// 或者
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); // 可以选择多个不连续的单元格或行
```
如果你已经设置了选择模式,但仍然无法选中单元格或行,那可能是因为你的 `TableModel` 中的 `isCellEditable()` 方法返回了 `false`。你需要在 `TableModel` 中重写 `isCellEditable()` 方法,使其返回 `true`,才能进行编辑或选择。
相关问题
swing表格左键选中点击按钮删除
您可以通过以下步骤实现在swing表格中使用左键选中后点击按钮删除选中行的功能:
1. 创建一个JTable对象并将其添加到JScrollPane中。
2. 为JTable添加一个ListSelectionListener,以便在选择行时更新删除按钮的状态。您可以使用getSelectedRow()方法获取选中行的索引。
3. 创建一个删除按钮,并为其添加一个ActionListener。在ActionListener中,获取选中行的索引,然后使用JTable的removeRow()方法删除该行。
以下是一个示例代码片段,可以帮助您开始实现此功能:
```
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
int selectedRow = table.getSelectedRow();
deleteButton.setEnabled(selectedRow != -1);
}
});
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
int selectedRow = table.getSelectedRow();
((DefaultTableModel)table.getModel()).removeRow(selectedRow);
}
});
```
在这个例子中,`data`是表格的数据,`columnNames`是表格的列名,`deleteButton`是删除按钮。请注意,您需要使用DefaultTableModel来删除表格中的行。
Java swing表格添加滚动条
要在Java Swing表格中添加滚动条,可以使用JScrollPane组件。以下是添加滚动条的步骤:
1. 创建一个JTable对象并将其添加到JScrollPane中:
```
JTable table = new JTable(data, headers);
JScrollPane scrollPane = new JScrollPane(table);
```
其中,data是表格中的数据,headers是表头。
2. 设置JScrollPane的尺寸和位置:
```
scrollPane.setPreferredSize(new Dimension(500, 200));
scrollPane.setBounds(10, 10, 500, 200);
```
3. 将JScrollPane添加到容器中:
```
JFrame frame = new JFrame();
frame.getContentPane().add(scrollPane);
```
完整的示例代码如下:
```
import javax.swing.*;
import java.awt.*;
public class TableExample {
public static void main(String[] args) {
Object[][] data = {
{"John", "Doe", 30},
{"Jane", "Doe", 25},
{"Bob", "Smith", 50},
{"Alice", "Jones", 45}
};
String[] headers = {"First Name", "Last Name", "Age"};
JTable table = new JTable(data, headers);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(500, 200));
scrollPane.setBounds(10, 10, 500, 200);
JFrame frame = new JFrame();
frame.getContentPane().add(scrollPane);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)