• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

JTable inside a JFrame from a different class

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! I am new here and also somewhat new to java.

I am having trouble picturing how to get a table I made that references a sqlite database into a JTable, then put that JTable into a JFrame that exists in a different class.

I want the table from SearchBooks to be passed to the UserInterface class. I am thinking that the showTableData method in SearchBooks could be loaded into the JFrame (UIFrame) that exists in UserInterface class. - In fact thats what I am trying to do. It doesn't seem as simple as just calling the showTableData class, or I may be missing some crucial element.



Here is my user Interface class (That contain the UIFrame frame I need to display the table information to).





Any help would be greatly appreciated! Thanks!



 
Rancher
Posts: 144
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I counted at least 3 different JFrame references - that doesn't look right.
Also: Not really a neginner question - would had fit better in the gui forum (Why is it that anyone always post in Beginner?).
In fact: It's quite easy: Just add the JTable where you needed it, load the data a-sync not in EDT and then just set the model within the EDT.
 
Adam Grey
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response. Sorry, I am a beginner which is why I gravitated to this forum. I will clean up the JFrame references after I figure this out. To me, it does not seem as simple as you make it. I am trying to keep all my business logic in the SearchBooks function which is where the connection to the SQLite database is defined. I guess I need some help with what methods to call from SearchBooks in order to populate the JTable that is in the UserInterface class.

What do you mean by EDT?

I'm not totally understanding you, and it may be my fault.

Lets say I have this code in UserInterface:



It's not populating the list because I put all this code:




inside SearchBooks. It is still getting called while the JTable in UserInterface remains blank. So if I delete that code from SearchBooks, how do I send the data from SearchBooks to the JTable in UserInterface?

So my real question is what should I do to define and use those same constraints but within the UserInterface defined JFrame? There are also buttons in that JFrame as well.

 
Kristina Hansen
Rancher
Posts: 144
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EDT = Event Dispatch Thread
Not just a Java term but basically how any modern graphical environment works: Any time you as a human interact with any input device of system (even when touching the touchscreen of your smartphone) you generate what's called an (input)
event. It is most important above any else when you're dealing with GUIs you fully understand the concept of input events and a dispatch thread.
It comes down to this: You have your Java code running and then you have the GUI. Both are handled in their own threads - or simple: They run (correctly: they're executed) in parallel on the same time - but don't really interact with eac
h other. So whenever you either want to process an event or want to update the GUI you have it to de-couple it.
What means de-coupling in terms of GUIs: I bet you encountered a "frozen" application not responding to the mouse or any key presses. This is caused by someone not fully understood and hence not correctly used de-coupling.
When the OS sends your GUI an input event it's the EDT to take care of it and delegate it to the registered listeners. But, it's also responsible for drawing the GUI.
When you stick to Java and this forum you'll see there're often beginners come up with questions like these: "I want to have a timer. And I have a button. But when I click it nothing happens for 10 seconds and then it only shows 10 instead of counting.". The issue: They locking up the EDT so it can't draw anymore.
Same goes for your loading of your data. Although it may be fast, and maybe even fast enough to accept the short "freeze", it's not how you should do it. In fact, the correct way is to take the event, start a new thread to de-couple it from the EDT (so it can do its things) and when you ready and all is done and you have the data ready to update the GUI you have to correctly re-sync it back into the EDT. That's basically what you're doing in your main(): You let the EDT do all the GUI stuff.
From just looking over your code you're doing it wrong currently. You have the actionPerformed() - this is called from within the EDT - and call showTableData() inside it - so you do what you should avoid at all cost: Doing non-GUI related stuff inside the EDT.
To do it correctly: Start a new thread, let all happen inside it, and at the end just update the GUI with another call to invokeLater() so it gets re-synced back into the EDT.

Side-note: Your SQL stuff also has some flaws:
1) Don't use Class.forName() - that's obsolete (don't know why even still recent tutorials and books still use it - JDBC4 made it obsolet - and pretty much any driver today is a type 4 one).
2) You're using your prepareStatement wrong - you prepare a statement to not build a SQL string yourself.
3) Move the TableDataModel stuff from above the SQL stuff to below it.
 
Marshal
Posts: 25194
64
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adam Grey wrote:I want the table from SearchBooks to be passed to the UserInterface class.



It would be much easier if your user interface was all in one class. Why isn't it? You created a UserInterface class but then you have bits of the user interface in other classes? I would suggest rethinking that design.
 
Marshal
Posts: 68066
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adam Grey wrote:. . . I will clean up the JFrame references after I figure this out. . . . .

Get rid of the unnecessary frames now. You won't figure the code out until after you have cleaned up those references.

I am trying to keep all my business logic in the SearchBooks function . . .

SearchBooks is a bad name for a class. BookSearcher would be better. I presume that class is completely distinct from your GUI code. Don't mix business logic and GUI display code in the same class.
Don't make a display class implement action listener. Since λs have been around for (I think exactly) six years, use a λ for each action listener object. Java™ Tutorials about λs.Don't write addActionListener(this);, not even if you found it in a textbook.

What do you mean by EDT?

Event Dispatch Threa‍d. Link.

. . . Lets say I have this code in UserInterface: . . .

Don't give child components bounds. Use a layout manager object. Don't add the table after you have set the frame visible. Add every component first, then set visible. I can never remember how to populate a table, but you will find the details in the Java™ Tutorials.
You mentioned a list somewhere. I can't see any Lists. I suggest you write code to query your database and create methods to return the results as Lists or Object[][]s.Remember those types are implicitly mutable, so you need to take defensive copies whereverr you use those lists or arrays.
 
Paul Clapham
Marshal
Posts: 25194
64
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adam Grey wrote:I guess I need some help with what methods to call from SearchBooks in order to populate the JTable that is in the UserInterface class.



In a typical object-oriented design, you would call a method of UserInterface which is designed to make that happen.

So you would need for SearchBooks to have a reference to a UserInterface object, the one you created to build and display the UI. This method would be called something like "populateTable"; it would have a parameter to pass the required data, probably a TableModel of some kind. And it would take that parameter and put the data into the JTable which it contains.
 
Adam Grey
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wanted to say thanks for the help, that feature is working now.
 
Paul Clapham
Marshal
Posts: 25194
64
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good to hear that!
 
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!