gpt4 book ai didi

java - 仅使用 mousePressed、mouseReleased 和 mouseDragged 选择自定义 JTable 中的多行

转载 作者:行者123 更新时间:2023-12-02 02:39:03 27 4
gpt4 key购买 nike

我在自定义 JTable 类中选择和取消选择行时遇到问题。

我的自定义类扩展了 JTable 并实现了 MouseListener 和 MouseMotionListener。当我按下一行时,我希望该行被选中;如果按下未选定的行,我希望取消选择它;用于选择和取消选择行的代码当前位于重写的changeSelection 方法中。

import javax.swing.*;
import javax.swing.table.TableModel;
import java.awt.event.*;

public class SelectFRCTable extends JTable implements MouseListener, MouseMotionListener {

private boolean isPressed;
private boolean isReleased;
private boolean isDragged;

public SelectFRCTable(TableModel model) {
//Register for mouse events on the table.
this.setModel(model);
addMouseListener(this);
addMouseMotionListener(this);
}

public void mousePressed(MouseEvent e) {
System.out.println("Mouse Pressed: " + e.getClickCount());
isPressed = true;
isReleased = false;
}

public void mouseReleased(MouseEvent e) {
System.out.println("Mouse Released: " + e.getClickCount());
isPressed = false;
isDragged = false;
isReleased = true;
}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mouseClicked(MouseEvent e) {
System.out.println("Mouse Clicked: " + e.getClickCount());
}

public void mouseDragged(MouseEvent e) {
System.out.println("Mouse Dragged: " + e.getClickCount());
isDragged = true;
}

public void mouseMoved(MouseEvent e) {}

@Override
public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) {
ListSelectionModel selectionModel = getSelectionModel();
boolean selected = selectionModel.isSelectedIndex(rowIndex);

if (selected) {
selectionModel.removeSelectionInterval(rowIndex, rowIndex);
getValueAt(rowIndex, columnIndex);
} else if (!selected) {
selectionModel.addSelectionInterval(rowIndex, rowIndex);
}
}
}
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.util.Vector;

public class TableExample extends JFrame{

public TableExample() {
JFrame frame = new JFrame("Custom JTable Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(925,700);
frame.setLayout(new GridBagLayout());

// Create a table with all the events
Vector<String> columns = new Vector<String>();
Vector<Vector<String>> rows = new Vector<Vector<String>>();
columns.add("Column 1");

// Get information about each event and store it in a vector to eventually be loaded into a
// table model.
for (int i=0; i<30; i++) {
String info = "Hello World";
Vector<String> data = new Vector<String>();
data.add(info);
rows.add(data);
}

// Create table model and configure it so that none of the cells are editable.
TableModel model = new DefaultTableModel(rows, columns) {
public Class getColumnClass(int column) {
Class returnValue;
if ((column >= 0) && (column < getColumnCount())) {
returnValue = getValueAt(0, column).getClass();
} else {
returnValue = Object.class;
}
return returnValue;
}
public boolean isCellEditable(int row, int column) {
return false; //This causes all cells to be not editable
}
};

// Create custom table
SelectFRCTable table = new SelectFRCTable(model);

// Add the table to a scrollable pane
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane, BorderLayout.CENTER);

pack();
setVisible(true);
}
}

当前代码在一定程度上有效。如果用户按下该行,则选择该行;如果用户再次按下该行,则取消选择选定的行。然而,如果用户按下一行并选择它,然后在选择它后继续按住鼠标左键并移动光标,则该行会被取消选择,然后再次选择,然后再次取消选择,依此类推,直到用户释放鼠标左键。

这是当前条件用于执行changeSelection 方法中的if/else-if 语句的结果。我尝试了各种其他条件,这些条件也使用全局 boolean 变量 isPressed、isReleased 和 isDragged 的​​组合,但到目前为止我还没有找到有效的解决方案。

有人有什么想法吗?

非常感谢任何帮助,提前致谢。

最佳答案

我找到了解决最初问题的方法,该解决方案也适用于启用拖动的情况:

import javax.swing.*;
import javax.swing.table.TableModel;
import java.awt.event.*;

public class SelectFRCTable extends JTable implements MouseListener, MouseMotionListener {

private boolean selectionDetermined;
private boolean isSelecting;
private boolean isDeselecting;

public SelectFRCTable(TableModel model) {
//Register for mouse events on the table.
this.setModel(model);
addMouseListener(this);
addMouseMotionListener(this);
}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {
selectionDetermined = false;
isSelecting = false;
isDeselecting = false;
}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mouseClicked(MouseEvent e) {}

public void mouseDragged(MouseEvent e) {}

public void mouseMoved(MouseEvent e) {}

@Override
public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) {
ListSelectionModel selectionModel = getSelectionModel();
boolean selected = selectionModel.isSelectedIndex(rowIndex);

if (selected && !selectionDetermined) {
selectionDetermined = true;
isSelecting = false;
isDeselecting = true;
} else if (!selected && !selectionDetermined) {
selectionDetermined = true;
isSelecting = true;
isDeselecting = false;
}

if (isSelecting && !selected) {
selectionModel.addSelectionInterval(rowIndex, rowIndex);
} else if (isDeselecting && selected) {
selectionModel.removeSelectionInterval(rowIndex, rowIndex);
}
}
}

释放鼠标时会设置标志,并且这些标志在 ChangeSelection() 方法中用作 if\else-if 语句的条件。

关于java - 仅使用 mousePressed、mouseReleased 和 mouseDragged 选择自定义 JTable 中的多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57190995/

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