Java 打开JDialog的JTable侦听器
抱歉,如果有类似的问题,无法找到任何内容 基本上,在我的程序中,我有一个JTable表中的项目,每行都有它们的质量、描述、价格等。无论何时从表中选择该项,我都希望打开一个新的JDialog,其中包含有关该项的更多信息。但是,我不知道如何获取所选行,例如更改其颜色以知道它已被选中。下面我试过了,但什么也没用。我猜事件的来源与模型无关Java 打开JDialog的JTable侦听器,java,swing,jtable,jdialog,Java,Swing,Jtable,Jdialog,抱歉,如果有类似的问题,无法找到任何内容 基本上,在我的程序中,我有一个JTable表中的项目,每行都有它们的质量、描述、价格等。无论何时从表中选择该项,我都希望打开一个新的JDialog,其中包含有关该项的更多信息。但是,我不知道如何获取所选行,例如更改其颜色以知道它已被选中。下面我试过了,但什么也没用。我猜事件的来源与模型无关 public void addListener(){ table.getSelectionModel().addListSelectionListener(
public void addListener(){
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent e) {
if (e.getSource() == table.getSelectionModel()) {
ItemDialog id = new ItemDialog(table.getSelectedRow());
}
}
});
}
通过将列表选择侦听器添加到表的选择模型中,可以获取所选行:
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
/**
* Adapted version of a standard Java demo project:
* https://docs.oracle.com/javase/tutorial/displayCode.html?
* code=https://docs.oracle.com/javase/tutorial/uiswing/examples
* /components/SimpleTableDemoProject/src/components
* /SimpleTableDemo.java
*/
public class SimpleTableDemo extends JPanel {
public SimpleTableDemo() {
super(new GridLayout(1, 0));
String[] columnNames = {"First Name", "Last Name", "Sport",
"# of Years", "Vegetarian"};
Object[][] data = {
{"Kathy", "Smith", "Snowboarding", 5, false},
{"John", "Doe", "Rowing", 3, true},
{"Sue", "Black", "Knitting", 2, false},
{"Jane", "White", "Speed reading", 20, true},
{"Joe", "Brown", "Pool", 10, false}
};
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
table.getSelectionModel().addListSelectionListener(
selectionEvent -> {
if (!selectionEvent.getValueIsAdjusting()
&& selectionEvent.getSource().equals(table.getSelectionModel()))
System.out.println("Row index: " + table.getSelectedRow());
}
);
add(new JScrollPane(table));
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
SimpleTableDemo newContentPane = new SimpleTableDemo();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(SimpleTableDemo::createAndShowGUI);
}
}
下面是我以前的一个项目中的几行代码。(我故意不提供完整的代码,因为很多代码都是不敬的) 将鼠标侦听器添加到表中
table.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) { // change it to whatever key or click you want
if (table.getRowCount() != 0) {
printSelectedRowData();
}
}
}
});
这里是printSelectedRowData()方法
public void printSelectedRowData() {
new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
try {
Object[] rowData = new Object[table.getColumnCount()];
for (int i = 0; i < table.getColumnCount(); i++) {
rowData[i] = table
.getValueAt(table.getSelectedRow(), i);
System.out.println(table.getColumnName(i) + ": "
+ rowData[i] + "\n");
}
} catch (Exception e) {
System.err.println("Error ");
}
return null;
}
}.execute();
}
public void printedselectedrowdata(){
新SwingWorker(){
@凌驾
受保护的Void doInBackground()引发异常{
试一试{
Object[]rowData=新对象[table.getColumnCount()];
对于(int i=0;i
要更快地获得更好的帮助,请发布一个(最少完整的可验证示例)或(简短、独立、正确的示例)。为表格硬编码一些数据。JTable#getSelectedRow
将告诉您所选的行(如果没有,则为-1),但您可能需要的是表示行的数据,这取决于您使用的TableModel
的类型。而且,如果我是您的用户,我不会高兴您每次更改选择时都弹出一个对话框。考虑使用<代码> JButton < /代码>(更多信息)或<代码> jopopMutual……ListStudio侦听器是一个更好的主意,因为MouseListener不考虑键盘选择(可能是一件好事,可能是件坏事),但是你永远不应该访问UI元素Fro。在事件调度线程之外,您可能会陷入线程竞争状态,这可能会产生错误的结果确保您一切正常,正如我提到的,这是一个旧项目。这只是做同样事情的另一种方式,可能会不太安全。请注意,ListSelectionListener会在取消选择值和选择值时通知您是的,您是对的:如果您只想处理最终事件,请使用getValueIsAdjusting
方法(仅当它返回false
)。请参阅更新的答案。