Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By kjkrum
  • 1 Post By Tolls

Thread: Swing - What components to use? Logging software.

  1. #1
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Swing - What components to use? Logging software.

    Hello!

    I'm still a newbie at coding, but I feel like I can create the simple programs that I myself feel like I am missing or finding a need of. Right now, I've been working on a program to keep track of my wine cellar and the wines in it. I've coded a program that works fine for my needs, though it's missing a GUI. My plan is to share the program with a few of my friends, and because of that I thought it might be good to learn some GUI programming.

    The program itself is very simple. One creates a new wine object, which then is stored in an ArrayList for those objects. The list is saved in a file and read when the program is reopened, so the entries are kept in between sessions. What I would like to accomplish with the GUI is a window that displays the list of wines and some of the attributes the wine objects has.

    Something like the following Photoshop-sketchup:

    Swing - What components to use? Logging software.-ld4wj.jpg

    • Buttons
      The buttons will be JButtons, very simple and I know how to use these in the proper way.
    • Text
      The text below the buttons will be JLabels that display various information.


    My big question is how to display the attributes of the wines that are stored in the ArrayList. The list may contain hundreds of different wines, and I want to have something looking as the above sketch that is also scrollable.

    Objects of the wine class has the following attributes:

    Java Code:
    	String country;
    	String name;
    	String extName;
    	int vintage;
    	double price;
    	String character;
    	String grape;
    	int amount;
    	int storage;
    Not all of these have to be displayed in the GUI, though I would like to have most of them in a column each. I'm also thinking about the possibility to sort the list in different orders, e.g. price/vintage decscending/ascending order.

    I've done some reading into what type of Swing component could be the best to display the information I want to display, and I figure I could perhaps use a JTable. Looking at Oracles tutorial site, I found this suitable picture:

    Swing - What components to use? Logging software.-basictable.png

    This might be a bit silly, but I'm just looking for a type of confirmation that I'm looking in the correct way. Is it possible to use a JTable component to achieve what I want to achieve, or should I perhaps use something else that I haven't thought of?

    Any thoughts of insights and tips are welcome!

    Thank you,
    Z!

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: Swing - What components to use? Logging software.

    Looks fine to me. But I would be wary of using text files as databases. You could end up with read write access issues.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  4. #4
    romero4742 is offline Member
    Join Date
    Mar 2012
    Posts
    27
    Rep Power
    0

    Default Re: Swing - What components to use? Logging software.

    Quote Originally Posted by al_Marshy_1981 View Post
    Looks fine to me. But I would be wary of using text files as databases. You could end up with read write access issues.
    What is the best way to store data?

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Swing - What components to use? Logging software.

    For something as simple as this, you could make your wine bottle class Serializable and serialize the whole ArrayList to a file.
    Tolls likes this.
    Get in the habit of using standard Java naming conventions!

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Swing - What components to use? Logging software.

    Quote Originally Posted by al_Marshy_1981 View Post
    Looks fine to me. But I would be wary of using text files as databases. You could end up with read write access issues.
    Only if it's multi user, which is unlikely on a desktop app like this.
    Start the program.
    Read in the file.
    Do stuff.
    Write back to the file on exit or save.
    DarrylBurke likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Swing - What components to use? Logging software.

    Quote Originally Posted by Tolls View Post
    Only if it's multi user, which is unlikely on a desktop app like this.
    Start the program.
    Read in the file.
    Do stuff.
    Write back to the file on exit or save.
    This is basically what I want it to do, therefor I thought there wouldn't be any problems just to write a file, read file, write back to file.

    ---

    Thanks for all the great answers, I'm currently looking into using JTables, and I'm slowly learning to use it in the way I want to!

    kjkrum - I will look into your suggestion aswell! I haven't heard about Serializable before, so it is def. something I should read about!

    Thanks all,
    Z!

  8. #8
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Swing - What components to use? Logging software.

    I've run into a few questions regarding the display of different data types in the JTable.

    As an experiment to understand how the default table model works, and how I can populate a table with information, I've used a for-loop that put String-objects into my JTable. I found some good JTable guides and got it working.

    I have the following code to create a JTable:

    Java Code:
            JTable table = new JTable();
            DefaultTableModel model = new DefaultTableModel();
            table.setModel(model);
            model.setColumnIdentifiers(new String[] {"Name", "Country", "Vintage", "Price"});
    The columns are to be populated with data from the SBWine-objects that are stored in an ArrayList<SBWine>.

    To do this I can use a for-loop, but so far I've only managed to get it working on String objects.

    Java Code:
            for (SBWine p : wineList)
            {
                model.addRow(new String[] {p.name, p.country});
            }
    I do not know how to display the values of Vintage and Price. They are of the datatypes Integer and Double. Do I manually have to run a toString(); before adding it to my JTable, or can I set the model to accept a certain datatype in one of the columns?

    If possible, I would want to keep the data types that they are in, since I cannot really find a good reason to display a number as a String, and I might want to be able to sort the data later on, and therefor simplify the future coding.

    Thanks for all the help and support so far,
    Z!

Similar Threads

  1. Swing components are not well-displayed
    By Josep_16 in forum AWT / Swing
    Replies: 1
    Last Post: 08-21-2011, 03:37 AM
  2. binding swing components
    By furqankhan in forum Advanced Java
    Replies: 1
    Last Post: 06-25-2010, 01:08 PM
  3. Problem In Swing Components
    By SANDY_INDIA in forum AWT / Swing
    Replies: 1
    Last Post: 07-19-2008, 11:23 PM
  4. Tab order on swing components
    By ashvin@projectdemo.biz in forum AWT / Swing
    Replies: 1
    Last Post: 05-31-2008, 11:06 AM
  5. Where is it best to declare swing components?
    By MacNstuff in forum AWT / Swing
    Replies: 1
    Last Post: 02-06-2008, 01:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •