• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Having a JTable Selection.

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic