java jtable 将双击的数据存入到静态
时间: 2024-03-28 11:42:35 浏览: 79
可以通过添加 JTable 的 MouseListener 监听器,来监听 JTable 的双击事件,然后将双击的数据存入静态变量中。具体步骤如下:
1. 在 JTable 上添加 MouseListener 监听器,可以使用以下代码实现:
```java
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) { // 判断是否双击
int row = table.getSelectedRow(); // 获取选中的行
Object data = table.getValueAt(row, 0); // 获取选中的数据
YourStaticClass.staticVariable = data; // 存入静态变量
}
}
});
```
其中,YourStaticClass 是你自己定义的一个静态类,staticVariable 是该类中定义的一个静态变量,用于存储双击的数据。
2. 在静态类中定义一个静态变量,用于存储双击的数据,例如:
```java
public class YourStaticClass {
public static Object staticVariable;
}
```
这样,当 JTable 被双击时,选中的数据就会被存入 YourStaticClass.staticVariable 中,可以在需要的地方直接使用该静态变量的值。需要注意的是,静态变量是全局共享的,因此需要谨慎使用。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)