Getting value from table by mouse click

Hi,

I am totally new to Java, and a novice in programing all together....

For a big final project at school I have to develop an application to deal with data - store, retrieve, edit, print and so on..

I managed to connect to mySQL database and get a resultset, and display the result in a table format.

The next step is: When the user select any line with a mouse click, the row corresponding to the primary key (column 1) should be displayed in a textArea field.

My question: how do I get the value where the mouse was clicked?

My code (so far):

// Getting the resultSet:and put it in the table (that was created by NetBeans 5.5)

public Vector<Vector><Object>> getData() {

Vector<Vector><Object>> vtrEmployee = new Vector<Vector><Object>>();

try {

String query = "SELECT * FROM tblemployees ";

rs = stmt.executeQuery(query);

while (rs.next()) {

rowCode = rs.getString("empCode");

rowTitle = rs.getString("empTitle");

rowFirstName = rs.getString("empFName");

rowLastName = rs.getString("empLName");

rowNick = rs.getString("empNick");

rowDOB = rs.getString("empDOB");

rowStartWork = rs.getString("empStartWork");

rowAddress = rs.getString("empAddress");

rowSalary = rs.getInt("empSalary");

rowPass = rs.getString("empPass");

rowAccessLevel = rs.getInt("empAccessLevel");

rowRemark = rs.getString("empRemark");

Vector<Object> rowVector = new Vector<Object>();

rowVector.add(rowCode);

rowVector.add(rowFirstName);

rowVector.add(rowNick);

vtrEmployee.add(rowVector);

}

} catch(SQLException ex) {

System.err.println("SQLException: " + ex.getMessage());

}

return vtrEmployee;

}// End getData()

//Selecting the wanted Line

public String selectLine(String selectedEmp) {

try {

String query = "SELECT * FROM tblemployees WHERE empCode = '" + selectedEmp + "';";

System.out.println("rowCode = " + rowCode);

rs = stmt.executeQuery(query);

while (rs.next()) {

String rowCode = rs.getString("empCode");

String rowTitle = rs.getString("empTitle");

String rowFirstName = rs.getString("empFName");

String rowLastName = rs.getString("empLName");

String rowNick = rs.getString("empNick");

String rowDOB = rs.getString("empDOB");

String rowStartWork = rs.getString("empStartWork");

String rowAddress = rs.getString("empAddress");

int rowSalary = rs.getInt("empSalary");

String rowPass = rs.getString("empPass");

int rowAccessLevel = rs.getInt("empAccessLevel");

String rowRemark = rs.getString("empRemark");

selectedEmp = "Code: " + rowCode +

"\nName: " + rowTitle + " " + rowFirstName + " " + rowLastName + " (" + rowNick + ") " +

"\nDOB: " + rowDOB + "\nWorking Since: " + rowStartWork + "\nAddress: " + rowAddress +

"\nSalary: " + rowSalary + "\nPassword: " + rowPass + "\nAccess Level: " + rowAccessLevel +

"\nRemarks: " + rowRemark ;

}

} catch(SQLException ex) {

System.err.println("SQLException: " + ex.getMessage());

}

return selectedEmp;

}//End selectLine()

And this is the mouse click even that is supposed to return the empCode variable to the selectLine()

private void rtrvTableMouseClicked(java.awt.event.MouseEvent evt) {

txtAreaResults.setText(Employee.selectLine(<How do I get the value for here?>));

}

Any simple solution for this?

Many thanks in advance..

[3669 byte] By [Gil@BKKa] at [2007-11-27 9:57:44]
# 1

Don't forget to use the "Code Formatting Tags",

see http://forum.java.sun.com/help.jspa?sec=formatting,

so the posted code retains its original formatting.

You should be using a ListSelectionListener so you a notified when you click on a row. Read the Swing tutorial on "How to Write a List Selection Listener" for an example to get you started:

http://java.sun.com/docs/books/tutorial/uiswing/events/listselectionlistener.html

camickra at 2007-7-13 0:28:06 > top of Java-index,Desktop,Core GUI APIs...
# 2

Ok - That was a huge step forward for me - Thank you!! but I am still not out of the woods....

I adapted the code to my application as bellow:

private void rtrvTableMouseClicked(java.awt.event.MouseEvent evt) {

class SharedListSelectionHandler implements ListSelectionListener {

public void valueChanged(ListSelectionEvent e) {

ListSelectionModel lsm = (ListSelectionModel)e.getSource();

firstIndex = e.getFirstIndex();

lastIndex = e.getLastIndex();

boolean isAdjusting = e.getValueIsAdjusting();

}

}

txtAreaResults.setText(Employee.selectLine("" + rtrvTable.getValueAt(lastIndex ,0)));

String temp = "" + rtrvTable.getValueAt(lastIndex, 0);

System.out.println("ViewEmp - 1st Index = " + firstIndex);

System.out.println("ViewEmp - last Index = " + lastIndex);

System.out.println("ViewEmp - temp = " + temp);

}

But where ever I click - I get both indexes at '0' ...

What did I miss?

Gil@BKKa at 2007-7-13 0:28:06 > top of Java-index,Desktop,Core GUI APIs...
# 3

> What did I miss?

Well, you have a working example from the tutorial to test. So you can compare your code with the tutorial code to see whats different.

Did you add a System.out.println(...) in your code to see if its ever being executed?

Where in the tutorial do they use a MouseListener?

camickra at 2007-7-13 0:28:06 > top of Java-index,Desktop,Core GUI APIs...
# 4
getSelectedRow() <<< This is what I was looking for.....Works like a charm :)Thanks.
Gil@BKKa at 2007-7-13 0:28:06 > top of Java-index,Desktop,Core GUI APIs...