Java: Retrieving data from Database and load in JTable - java

I know how to retrieve data (just text in a table) from a database in Java and how to show it in the console. But I want to load it in a JTable.
Is there a good (modern) way (tutorial), without using Vectors?

Check out this tutorial: How to Use Tables

Seems your question is similar with these two questions:
How to fill data in a JTable with database?
Displaying data from database in JTable

Check out GlazedLists it comes with ready made TableModels that are based on modern collection interfaces.
http://www.glazedlists.com/
If you don't want to use an extra library. You can easily implement your own javax.swing.table.TableModel. I like to implement TableModel and java.util.List so I'm working with just a simple List, and hooking up any List easily.

Related

How do you have a text field do dynamic suggestions in the UI from a DB?

I want to have a Java program dynamically suggest list items as a user types in a text field? I want to the suggestions to come from a table in a DB. I might port to Javascript.
I'm not very familiar with SQL, just enough to run simple queries that I need to do. I can normalize simple tables.
The DB will be small (<1000 items), I could have all the data in local storage, and then write an event loop per keystroke to search for the strings, but I was wondering if there was a more efficient way to do this generally.
I am open to using some package or other so I don't have to reinvent the wheel, but I would rather write it efficiently myself.

JTable data persistence advice

I am seeking advice in persisting my JTable data in an elegant manner. So far my research has indicated I can iterate through the many columns and rows and extract the data for saving (seems convoluted) or that I can save the table and related data as an Object in an Object file.
I would love to hear some advice from those more versed in this area as I am quite new to JTables and their workings. Are there many other solutions available that may be a better choice?
It depends on what you want to do with the persisted data. If you only want to persist it so that you can display it again later, look at serializing (Java Serializable or Java Externalizable) it to a data-stream that you put somewhere. Later you can read it back (deserialize) and display it again.
If you want to put it in a database where the information is useable for other purposes, then you probably want to implement some object which models your data to keep it clear and simple. Then you can present this in a Swing Jtable by adapting your model to a table model. This still means you need to write the adaptation/transformation logic but it shouldn't be onerous and you get the most usable result. The TableModel is simply a way of looking at your data that a JTable is able to understand. Look at Adaptor patterns to get one idea about the mapping.
Hope that helps.

2 Column JTable for filenames. Autoupdate when more files are added to a list of files?

I am making a file converter. I will have JButton's to Add and Remove files from the list. When adding a file, it'll bring up a JFileChooser(or something) and return a list of files. Will I need to repopulate the entire table each time I add/remove files? If I have to resolve to that, what is the best approach?
When I didn't have two columns, I used JListModel + JList< File>. Is there a similar approach for using JTables?
No you don't have to repopulate the entire table. Just use DefaultTableModel.addRow(). JTable automatically creates an instance of DefaultTableModel.
Check this link: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
I think a good way is writing your own TableModel. If you inherit from AbstractTableModel, I think that it's a convenient way to use the observer design pattern. If you want to update the table, call fireTableXxx() methods.

Architecture of Android application with simple database

I want to build a fairly simple Android application.
The basic data model of my app consist on several basic elements with relationships between them.
I want the data to be persistant so I'm thinking about using an sqlite DB.
I looked at the Android Developer website, and the closest thing that I could find that is relevant to my question is the "NotePad Tutorial" which make use of an sqlite DB to store notes.
I think by now I got the handle on the basics, but I still have some questions though:
In the tutorial they have only one table in the DB. If my application requires a more complicated scheme - should I still use the same method? that is - putting everything inside a subclass of SQLiteOpenHelper? Or is there a more "structured" way to go?
Should I bother creating classes for all the data elements stored in my DB? I mean this is what I learned that I should do, but in the documentation there is no hint about that.
If I Should create classes - How should I use them correctly? I mean, since the result of the query is a Cursor object, and not a collection of rows, Should/Can I parse that into objects?
Thanks!
Define all the tables in the same subclass, this makes easy to see all the table at one place and possibly write SQL for upgade etc
Yes, that would be easier for manipulation in the java side and makes code clean
Read from the cursor and initialize an arraylist of objects one by one.

Displaying records with GWT MVP

I am following a 'Issue tracking' gwt project with screenshots here:
http://code.google.com/p/gwt-mvp-sample/wiki/screenshots
This template code can show exactly one Issue object with the IssueDisplayWidget
I need to allow the app to record many Issues (even just using in-memory List<> class for testing). And allow the IssueDisplayWidget to show every Issue added.
How could I achieve this?
You can use ListDataProvider to represent data and update table after each adding with EventBus or directly.

Categories

Resources