• 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

Suggestions for displaying files from database

 
Greenhorn
Posts: 9
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()
 
Arnes Arnautovich
Greenhorn
Posts: 9
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Arnes Arnautovich
Greenhorn
Posts: 9
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let your custom model extend the default model
 
Men call me Jim. Women look past me to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic