gpt4 book ai didi

java - 我正在尝试使用 MouseMotionListener 将数据从 JTable 打印到命令行

转载 作者:行者123 更新时间:2023-12-02 09:26:17 25 4
gpt4 key购买 nike

我对java相当陌生,尤其是java的这一面。我目前正在尝试创建一个 JTable,它将接收 CSV 数据文件,然后该表将显示在 GUI 上,您可以选择数据的特定部分,从中创建可视化效果。目前,我正在使用 MouseListener 进行 mouseClicked - 这会将单元格正确打印到命令行。但是,当对 mouseDragged 使用 MouseMotionListener 时,它只会打印出多次单击的单元格(来自 mouseClicked)。我知道这与 mouseClicked 有关(或者至少认为是),但我无法理解它。

代码:

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Color;

public class TableView extends ChartGUI{

/**
*
*/
public JScrollPane GetPanel() {
return scrollPane;
}
public void SetScrollPane(JScrollPane pane) {
scrollPane = pane;
}
public TableView(String title, Object[][] data) {
if(data != null) {
Object[] columnNames = data[0];
Object[][] tableData = new Object[data.length][];
for(int i = 0; i < (data.length - 1); i++){
tableData[i] = data[i+1];
}

table = new JTable(tableData, columnNames);
table.setCellSelectionEnabled(true);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(PANE_WIDTH, PANE_HEIGHT));
SetScrollPane(scrollPane);
} else {
System.out.println("Null Data");
}

/** MouseListener to allow users to select specific cells, printing the data to the command line */
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
col = table.getSelectedColumn();
row = table.getSelectedRow();
selData = GetData(table, row, col);

System.out.println("Selected data: MouseClicked :: " + (String)selData);
}
});
table.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent evt) {
col = table.getSelectedColumn();
row = table.getSelectedRow();
selData = GetData(table, row, col);

System.out.println("Selected data: MouseReleased :: " + (String)selData);
}
});
table.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent evt) {
col = table.getSelectedColumn();
row = table.getSelectedRow();
selData = GetData(table, row, col);

System.out.println("Selected data: MouseDragged :: " + (String)selData);

}
});

}
/** A method to return the values at an index selected by the user, as a String */
public String GetData(JTable table, int row_index, int col_index){
return (String)table.getModel().getValueAt(row_index, col_index);
}

public static void main(String[] args){
System.out.println("Why are you doing this?, maybe we should have some JUnit Tests");
}

private static final long serialVersionUID = 4901866775892531001L;
private JScrollPane scrollPane;
private JTable table;
private String selData;
private int col;
private int row;
private int PANE_WIDTH = 900;
private int PANE_HEIGHT = 600;
}

感谢您的帮助,非常感谢!

最佳答案

表格单元格不是通过鼠标移动来选择的,而是通过单击来选择的。您的 mouseDragged 方法将在每次触发时打印出所选表格单元格的信息,但所选表格单元格仅在您单击时才会更改。您可能想从 mouseEvent 获取当前鼠标位置,并使用该信息选择一个新的表格单元格,然后打印出该信息。

关于java - 我正在尝试使用 MouseMotionListener 将数据从 JTable 打印到命令行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15932136/

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