我正在用netbeans制作一个gui。我右键单击JTable
以添加用于鼠标单击的事件侦听器。我将我的代码添加到新方法中。对于我的生活,当我点击JTable
时我没有错误。
关于我做错了什么或如何解决它的任何想法?如果您需要查看更多我的代码,请告诉我。除了与DB的连接之外的所有内容都用完了驱动程序类。
我将我的程序移动到日食,以便让我更容易拍摄。
private void tableDisplayMouseClicked(java.awt.event.MouseEvent evt) {
try {
int row = tableDisplay.getSelectedRow();
String tableClick = (tableDisplay.getModel().getValueAt(row, 1).toString());
String sql = " select * from contact where id = ' " + tableClick + " ' ";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
if(rs.next()) {
System.out.println("hey dude this method is being called.");
String add2 = rs.getString("Business_Name");
fieldBN.setText(add2);
String add3 = rs.getString("First_Name");
fieldFN.setText(add3);
String add4 = rs.getString("Last_Name");
fieldLN.setText(add4);
String add5 = rs.getString("Phone");
fieldP.setText(add5);
String add6 = rs.getString("Email");
fieldE.setText(add6);
String add7 = rs.getString("Address_Line_1");
fieldA.setText(add7);
String add8 = rs.getString("Address_Line_2");
aLine2.setText(add8);
String add9 = rs.getString("Website");
fieldW.setText(add9);
}
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
这是onclick方法
这是我的平均听众
public Driver() {
gui();
conn = DbConnect.ConnectDb();
UpdateTable();
tableDisplay.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tableDisplayMouseClicked(evt);
}
});
}
以下是整个驱动程序类:
import java.awt.BorderLayout;
public class Driver {
private JFrame f;
private JPanel p;
private JTextField fieldBN;
private JTextField fieldFN;
private JTextField fieldLN;
private JTextField fieldP;
private JTextField fieldE;
private JTextField fieldA;
private JTextField aLine2;
private JTextField fieldW;
private JLabel labelBN;
private JLabel labelFN;
private JLabel labelLN;
private JLabel labelP;
private JLabel labelE;
private JLabel labelA;
private JLabel labelW;
private JComboBox relationship;
private JButton buttonS;
private JTable tableDisplay;
String[] relationshipValues = { "Business", "Friend", "Family", "Professional" };
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
// Constructor:
public Driver() {
gui();
conn = DbConnect.ConnectDb();
UpdateTable();
tableDisplay.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tableDisplayMouseClicked(evt);
}
});
}
public void gui() {
f = new JFrame("Contact Book");
GridBagConstraints c = new GridBagConstraints();
p = new JPanel(new GridBagLayout());
f.getContentPane().add(p, BorderLayout.NORTH);
c.gridx = 0;
c.gridy = 0;
labelBN = new JLabel ("Business Name:");
p.add(labelBN, c);
c.gridx = 1;
c.gridy = 0;
fieldBN = new JTextField(10);
p.add(fieldBN, c);
c.gridx = 0;
c.gridy = 1;
labelFN= new JLabel ("First Name:");
p.add(labelFN, c);
c.gridx = 1;
c.gridy = 1;
fieldFN = new JTextField (10);
p.add(fieldFN, c);
c.gridx = 0;
c.gridy = 2;
labelLN= new JLabel ("Last Name:");
p.add(labelLN, c);
c.gridx = 1;
c.gridy = 2;
fieldLN = new JTextField (10);
p.add(fieldLN, c);
c.gridx = 0;
c.gridy = 3;
labelP = new JLabel ("Phone Number:");
p.add(labelP, c);
c.gridx = 1;
c.gridy = 3;
fieldP = new JTextField (10);
p.add(fieldP, c);
c.gridx = 0;
c.gridy = 4;
labelE = new JLabel ("Email:");
p.add(labelE, c);
c.gridx = 1;
c.gridy = 4;
fieldE = new JTextField (10);
p.add(fieldE, c);
c.gridx = 0;
c.gridy = 5;
labelA = new JLabel ("Address:");
p.add(labelA, c);
c.gridx = 1;
c.gridy = 5;
fieldA = new JTextField (10);
p.add(fieldA, c);
c.gridx = 1;
c.gridy = 6;
aLine2 = new JTextField (10);
p.add(aLine2, c);
c.gridx = 0;
c.gridy = 7;
labelW = new JLabel ("Website:");
p.add(labelW, c);
c.gridx = 1;
c.gridy = 7;
fieldW = new JTextField (10);
p.add(fieldW, c);
c.gridx = 0;
c.gridy = 8;
labelW = new JLabel ("Relationship:");
p.add(labelW, c);
c.gridx = 1;
c.gridy = 8;
relationship = new JComboBox(relationshipValues);
p.add(relationship, c);
c.gridx = 1;
c.gridy = 9;
buttonS = new JButton("Save:");
p.add(buttonS, c);
c.gridx = 0;
c.gridy = 10;
tableDisplay = new JTable();
p.add(tableDisplay, c);
// pack the frame for better cross platform support
f.pack();
// Make it visible
f.setVisible(true);
f.setSize(1400,900); // default size is 0,0
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} // End of Gui Method
private void UpdateTable() {
try {
String sql = "SELECT * FROM contact";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
tableDisplay.setModel(DbUtils.resultSetToTableModel(rs));
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
private void tableDisplayMouseClicked(java.awt.event.MouseEvent evt) {
try {
int row = tableDisplay.getSelectedRow();
String tableClick = (tableDisplay.getModel().getValueAt(row, 1).toString());
String sql = " select * from contact where id = ' " + tableClick + " ' ";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
if(rs.next()) {
System.out.println("hey dude this method is being called.");
String add2 = rs.getString("Business_Name");
fieldBN.setText(add2);
String add3 = rs.getString("First_Name");
fieldFN.setText(add3);
String add4 = rs.getString("Last_Name");
fieldLN.setText(add4);
String add5 = rs.getString("Phone");
fieldP.setText(add5);
String add6 = rs.getString("Email");
fieldE.setText(add6);
String add7 = rs.getString("Address_Line_1");
fieldA.setText(add7);
String add8 = rs.getString("Address_Line_2");
aLine2.setText(add8);
String add9 = rs.getString("Website");
fieldW.setText(add9);
}
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Driver();
}
});
} // End main Method
} // End class Driver
答案 0 :(得分:1)
很难知道为什么您在注册MouseListener
时遇到问题,但没有足够的上下文来诊断问题。
首先应该在代码中放置put语句,以检查方法是否已被调用以及查询是否返回值。
如果所有其他方法都失败了,您应该尝试手动注册鼠标侦听器。有关详细信息,请参阅How to Write a Mouse Listener。
您的查询还包括匹配中的空格......
String sql = " select * from contact where id = ' " + tableClick + " ' ";
^------------------^-----
这会影响数据库将结果与查询匹配的能力。尝试删除这些
String sql = " select * from contact where id = '" + tableClick + "'";