对不起,如果有类似的问题,找不到任何东西。
基本上,在我的程序中,我在JTable中有每个行的质量,描述,价格等项目。我想打开一个新的JDialog,只要从表中选择该项目,就可以获得有关该项目的更多信息。但是,我不知道如何获取所选行,例如更改其颜色以知道它已被选中。以下我尝试了它并没有做任何事情。我猜这个事件的来源与模型无关。
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());
}
}
});
}
答案 0 :(得分:1)
您可以通过将列表选择侦听器添加到表的选择模型来获取所选行:
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);
}
}
答案 1 :(得分:0)
以下是我的一个旧项目的几行代码。 (我故意不提供完整的代码,因为很多代码都是不敬的)
将MouseListener添加到表格
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();
}