wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Having a JTable Selection. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Having a JTable Selection." Watch "Having a JTable Selection." New topic
Author

Having a JTable Selection.

david foley
Ranch Hand

Joined: Dec 01, 2012
Posts: 58
OK,
I am just Wondering can any one point me in the Right Direction, I have two Tables inside my Database one is Patient and the Other is History.. Inside my Swing JFrame I have two JTables Under Each Other. When i Connected to the Database, The Patient Table populates. But how do i go about, when i Select a Name inside the Patient JTable, Get the History Table to populate with that person History

I take it I will need to Create another MYSQL Command, and Connect to that Table.

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2242
    
    7

Well first you need to have some kind of action when clicking on the patient record. Then in the action, query the history database and update/display the history jtable.

There are several ways to do the clicking action but I suggest you look into having a table model for the patient table, and maybe even for the history table.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
david foley
Ranch Hand

Joined: Dec 01, 2012
Posts: 58
For Some Reason, this code is working in a different Program i am reading from a book,

But when i try to add it to this Program i'm working on it does not seem to be working.








K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2242
    
    7

Hello

Your code looks right but it doesn't seem to work. When you select the patient record, does the history query get executed? Is the history query even correct? Do you see "changes" to the JTable?

You may want to output the return value from getValueAt() to check what you have.


After thinking about it, the table model approach may be a bit over the top (complicating things). So instead just set the JTable's selectionModel which extends DefaultListSelectionModel. In the overridden valueChanged() method, do the query stuff.

Sorry for the mis-direction.
david foley
Ranch Hand

Joined: Dec 01, 2012
Posts: 58
Hi,

When i Run the Code, Nothing is Coming up on the JTable.. So i have yet to move onto the History JTable..

I'm kinda out of ideas as to what may be causing this problem i have messed around with the code and no joy.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2242
    
    7

Hello David,

Did you able to get something going? I did a something testing and using a selection model will get you started.

For my testing I have
Patient class = patientId, firstName, lastName
PatientHistory class = historyId, patientId, txnDate, description

patientId for PatientHistory is clearly a foreign key in DB
I hard-coded the data into maps





Of course instead of all those maps, you mostly want to pass in the returned ResultSet to the table models rather than doing the query inside the table model like what you have done. This will also reduced the number of connections. Also in my code the "historyMap" passed in the PatientSelector class is the original full set of data. And inside the selector, I used a localMap to refresh the history table.
david foley
Ranch Hand

Joined: Dec 01, 2012
Posts: 58
Thanks for your Reply.

I did get the Two Tables working.
with a ResultSetModel, I am trying to figure out how i get the ActionListener() Working with the Tables.

I have my Query for History
SELECT * FROM History WHERE patientid = ?

So i am Hoping that when the user clicks on a Record inside JTable that it should pick up what the patientid is and display the Query1 into the History JTable
david foley
Ranch Hand

Joined: Dec 01, 2012
Posts: 58

Ok i have The Selection Working if i hard Code the Query



But how can i get it to work with this ?



if i hard code it doesn't make a difference which Row I select it going to give me the Same History i would like to get it working that once i select the Row it gives me the History table for that person.


I have tried


No of them are working giving me a YOU have an error in your SQL Syntax check the Manual that corresponds to your MySQL Server Version for the right Syntax to use near '?') at line 1
and the console gives me
Exception in thread "main" java.lang.NullPointerException
at DisplayQueryResults.<init>(DisplayQueryResults.java:329)
at DisplayQueryResults.main(DisplayQueryResults.java:363)

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2242
    
    7

The ? in the query is part of the string in your case. The query should end at "... where patientid =".

In your code then you do

to make your query complete.

To get the patientId from the JTable, you first need to get the selected row, look up the patientId in the patient result set then query the history table.
 
wood burning stoves
 
subject: Having a JTable Selection.
 
Similar Threads
JTable
JTABLE
JTable
Jtable
jtable