This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Suggestions for displaying files from database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Suggestions for displaying files from database" Watch "Suggestions for displaying files from database" New topic
Author

Suggestions for displaying files from database

Arnes Arnautovich
Greenhorn

Joined: Aug 27, 2011
Posts: 9

I need your suggestions on how to display list of files from a database. Getting sql result is easy but in what manner do I show them?
Using JTable perhaps? Or some other way? They need to be selectable and then downloaded. Tranfering files from database to my
desktop I managed although I always have to enter the location where the file will be stored which isn't very user-friendly, but all in all I can do it.

Please leave your suggestions, thank you.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

What all information do you wish to display to the user? What options does he have? Can the user download multiple files at the same time? That would decide what component to use.
e.g.
1) File name, type, date, download button
2) JList with file names and a common download button

Arnes wrote:...although I always have to enter the location where the file will be stored which isn't very user-friendly...

You can ask the user the first time. On the UI provide a checkbox which says something like "Download all files to this location {download_location}". Check if this box is checked, else ask the user.

PS. In case your user needs to manually type the download location, check out JFileChooser#showSaveDialog()


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Arnes Arnautovich
Greenhorn

Joined: Aug 27, 2011
Posts: 9

Maneesh Godbole wrote:What all information do you wish to display to the user? What options does he have? Can the user download multiple files at the same time? That would decide what component to use.
e.g.
1) File name, type, date, download button
2) JList with file names and a common download button

Arnes wrote:...although I always have to enter the location where the file will be stored which isn't very user-friendly...

You can ask the user the first time. On the UI provide a checkbox which says something like "Download all files to this location {download_location}". Check if this box is checked, else ask the user.

PS. In case your user needs to manually type the download location, check out JFileChooser#showSaveDialog()


Thanks! Will try it. And I checked JList, it looks good.
Arnes Arnautovich
Greenhorn

Joined: Aug 27, 2011
Posts: 9

Just one question. How would I reference an id of a file inside a database? Do I take id along with his name and other info from database (except binary data) and put name and other needed info for the user to see in a JList and hide id of a file so that when user chooses the file the program knows which file inside the database he is choosing? The problem is, how do I hide the id and show name, etc. In PHP that would be as easy as just taking all info from database, printing only needed like names, etc. and inside links for files put get variable that points to the right file inside database.
How do I do that in Swing environment? Because if I have an array of String objects in which each String object has name + size + md5hash etc, I can't put the id inside too because when I place this array of String objects inside JList the id's will be showed too.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

In Java it would be as easy as creating a class whose objects contained whatever information about the file you needed, and whose toString() method returned whatever you wanted to appear in the JList. Jamming everything into a String looks easy at first but doesn't end up being easy.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

I am with Paul here. Constructing an object with the data encapsulated inside is the proper way to go.

Do I take id along with his name and other info from database (except binary data) and put name and other needed info for the user to see in a JList..

If I understand this correctly, you wish to display multiple attributes for a single file. For this I would suggest a JTable instead of a JList.

In pseudo code:
Retrieve the data from the DB.
Wrap the data in an object
Wrap all objects inside a collection like say ArrayList
Create a table model (say DefaultTableModel) from this ArrayList
Associate a JTable with this model.

Once you have the table mode, you can easily control the data which is displayed. Check out the model methods like getRowCount(),getColumnCount(),getValueAt()
Arnes Arnautovich
Greenhorn

Joined: Aug 27, 2011
Posts: 9

Maneesh Godbole wrote:I am with Paul here. Constructing an object with the data encapsulated inside is the proper way to go.

Do I take id along with his name and other info from database (except binary data) and put name and other needed info for the user to see in a JList..

If I understand this correctly, you wish to display multiple attributes for a single file. For this I would suggest a JTable instead of a JList.

In pseudo code:
Retrieve the data from the DB.
Wrap the data in an object
Wrap all objects inside a collection like say ArrayList
Create a table model (say DefaultTableModel) from this ArrayList
Associate a JTable with this model.

Once you have the table mode, you can easily control the data which is displayed. Check out the model methods like getRowCount(),getColumnCount(),getValueAt()


Thanks, that's what I need. I'm already checking the JTable and by the documentation on How To Use JTable it looks promising.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Paul Clapham wrote:In Java it would be as easy as creating a class whose objects contained whatever information about the file you needed, and whose toString() method returned whatever you wanted to appear in the JList.

I agree with the class, but I'd not require toString() to be written just for use in Swing components. Instead, use a custom ListCellRenderer (TableCellRenderer, TreeCellRenderer, etc).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Arnes Arnautovich
Greenhorn

Joined: Aug 27, 2011
Posts: 9

To report, I used JTable to display data from sql query. I also managed to implement listeners so now when I click twice on a directory in application I "enter" that directory and the sub-directories are shown. I've made a simple Linux-like . and .. directories (well not real, just table rows) that bring user to root and last directory, respectively.
I even added a test menu item for deleting the selected row from database. The only downside is I can't seem to remove the row immediately from the "view" as I delete him but instead, when I do I have to change directory and return to see directory gone (since the table is populated from query from database which holds true data). I don't know should I start another thread for this or not, I think I had like 3-4 threads this few days now, I fear being seen as a spammer.
What do you guys think? How to remove a row from a "view"?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Like you have already found out, the table is unaware of any data. It relies on the underlying model to display the data. Remove the data from the model and it wil vanish from the table too. Check the model for methods which will let you remove data. Hint. Make sure those methods ensure a table refresh.

I would approach it this way:
Load data from DB, wrap in Model, display.
When user wants to delete, ask server to delete it from DB, Once the server confirms the delete did happen, delete from your model. This way, you would need to load the data from the server only once.

I fear being seen as a spammer.

Nope. Spammers usually talk about enhancing your biological organs or bank balances
Arnes Arnautovich
Greenhorn

Joined: Aug 27, 2011
Posts: 9

Maneesh Godbole wrote:Check the model for methods which will let you remove data.

See I have a "custom" table model that extends AbstractTableModel and I haven't implemented removeRow() method. I don't know how.
DefaultTableModel has a removeRow() method but since I don't have DefaultTableModel I can't use it. Unless I implement that method.
Maneesh Godbole wrote:
I would approach it this way:
Load data from DB, wrap in Model, display.
When user wants to delete, ask server to delete it from DB, Once the server confirms the delete did happen, delete from your model. This way, you would need to load the data from the server only once.

Yes that pretty much how my program works. I create a Model and inside it I call for database "directories". Then I show it inside JTable.
Then when the user deletes the directory the server does the job with no problem. I did a back-end and it works. The problem is removeRow() method which I don't have in my custom Model and don't know how to implement one.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Let your custom model extend the default model
 
wood burning stoves
 
subject: Suggestions for displaying files from database
 
Similar Threads
read large CSV file and update the database
Storing files in RDBMS
FTP
constructing xml from values in database
How To Fetch a File ( .doc, .txt, .pdf, ..., etc.) From Client's Machine