[英]reading the value of jtable selected row
I am working with java RMI and swing and I have read the values from database but i am unable to read the value of selected row in this code. 我正在使用Java RMI和swing,已经从数据库中读取了值,但是无法在此代码中读取所选行的值。 What i want to JTable to show all databases and it is showing all the available databases in server but i am unable to read the selected row value in this
我想让JTable显示所有数据库,并显示服务器中所有可用的数据库,但是我无法在其中读取所选的行值
package schoolclient; 打包schoolclient;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import schoolserver.SchoolInterface;
public class DatabaseList {
JFrame jFrame = null;
JPanel jPanel = null;
JList jList = null;
JTable jTable = null;
String data = null;
schoolserver.SchoolInterface schoolInt = null;
public DatabaseList(SchoolInterface sii) {
schoolInt = sii;
jFrame = new JFrame();
jTable = new JTable(){
public boolean isCellEditable(int row, int column) {
return false;
}
};
jTable.setModel(createTable());
jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jTable.addMouseListener(new MouseAdapter() {
public void MouseClicked(MouseEvent e) {
if(SwingUtilities.isLeftMouseButton(e) && (e.getClickCount() == 2)) {
new createListSelection();
}
}
});
JScrollPane scrollPane = new JScrollPane(jTable);
jFrame.add(scrollPane);
jFrame.setSize(200, 200);
jFrame.setVisible(true);
}
private DefaultTableModel createTable() {
DefaultTableModel dtm = new DefaultTableModel();
dtm.addColumn("Databases", createArray());
return dtm;
}
private class createListSelection implements ListSelectionListener {
@Override
public void valueChanged(ListSelectionEvent e) {
if(!e.getValueIsAdjusting()) {
ListSelectionModel lsm = jTable.getSelectionModel();
data = (String) jTable.getValueAt(jTable.getSelectedRow(), 0);
System.out.println(data);
}
}
}
Object[] createArray() {
ArrayList<Object> al = null;
Object[] x = null;
try {
al = schoolInt.availableDatabases();
x = new Object[al.size()];
int i = 0;
for(Object o : schoolInt.availableDatabases())
x[i++] = o;
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, "no connection to database or "
+ "remote server availabe", "Server Information", JOptionPane.OK_OPTION);
}
return x;
}
}
You look to be over-complicating things. 您看起来太复杂了。 Don't re-add listeners within a listener, but rather simply add one listener, a MouseListener to the JTable, and add it once.
不要在侦听器中重新添加侦听器,而只需在JTable中添加一个侦听器,一个MouseListener,然后将其添加一次即可。 Within it check for double clicks (presses) and respond.
在其中检查双击(按)并做出响应。 Something like:
就像是:
jTable.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
data = (String) jTable.getValueAt(jTable.getSelectedRow(), 0);
System.out.println(data);
}
}
});
Other problems, your method within your MouseAdapter will never be called, ever: 其他问题,MouseAdapter中的方法将永远不会被调用:
jTable.addMouseListener(new MouseAdapter() {
public void MouseClicked(MouseEvent e) {
// ....
}
});
Your capitalization is wrong, and since MouseAdapter/MouseListener does not have a MouseClicked method (it is mouseClicked
) this method is never called. 您的大小写错误,并且因为MouseAdapter / MouseListener没有MouseClicked方法(它是
mouseClicked
),所以永远不会调用此方法。 Always place an @Override
annotation above any method that you think might be an override, and let the compiler warn you if it is not so. 始终将
@Override
注释放置在您认为可能会覆盖的任何方法之上,如果不是这样,则让编译器警告您。 Had you done this, you'd get a prompt warning from the compiler. 完成此操作后,编译器会提示您警告。
Regarding your comment 关于你的评论
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.