Results 1 to 3 of 3
  1. #1
    AWJustAGuy is offline Member
    Join Date
    Apr 2012
    Rep Power

    Default Using MVC architecture in a Swing GUI program?

    Not sure if this is the right forum... but here goes!
    I'm trying to write a simple account editing program. This program will read data from a file, create an array of accounts from the information, use a GUI to display the contents, and allow editing of balance, saving all changes back into the file, using Swing for the GUI. I'm basing it on MVC architecture... which I'm terrible at. Currently I've got one file, the Model portion, which contains the Account Class and all it's necessary methods and variables. Then I have my App class (the View portion of MVC) which opens the applet (or whatever it's called) and creates the JComboBox and text fields necessary to view and edit the info.

    I've currently got a very basic structure up now, in which I input the account data manually in the program in the App class, such as follows:
    Java Code:
    // Creates an array of accounts.
    Account[] accounts = new Account[] {new Account("Tim Allen", 500000, 123456),
                                    new Account("Sam Winchester", 351342, 666666)};
    // Creates a new combo box filled with the accounts created above.
    final JComboBox comboBox = new JComboBox(accounts);
    Now I'm moving over to the file input. I have all the code for inputting and separating the file; I just don't know where to stick the code. Should I put it in the App class like it already is? That seems like a lot of game time for what's supposed to be the simple View portion of the program, but it would definitely be the simplest solution. Do I put it in the Model class? That seems to make more sense, but how do I transfer the information to the JComboBox for viewing and editing? I haven't even touched the Controller class yet... I have no idea what I'm even supposed to do with that.

    If anyone can give me a little direction to go in, I would greatly appreciate it.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power

    Default Re: Using MVC architecture in a Swing GUI program?

    File input and output should definitely not go in the view portion of your code since it has nothing to do with displaying data or interacting with the user. I'm not a professional coder, but I would lean towards placing this in its own class in the control or model package. I would lean towards control, but regardless it is most definitely not "view".

    On second thought, perhaps the file handling should be in the model since control is mainly for communicating between model and view. Again, I'm no pro at this, so let's see what they have to say as well.
    Last edited by Fubarable; 04-24-2012 at 05:13 AM.

  3. #3
    AWJustAGuy is offline Member
    Join Date
    Apr 2012
    Rep Power

    Default Re: Using MVC architecture in a Swing GUI program?

    Thanks for the reply!
    I think I'm getting this worked through. I am putting the file input and writing into the controller class, having the user interact with that class specifically in the command line prompt to select a file, etc. That way, I can use the controller class to run the GUI View, passing it the necessary information from the file input.

    I'll see what I can do with this, and in the meantime, if anyone has any ideas or input, I'm all ears!

    Thanks, and happy coding!

Similar Threads

  1. Java swing program
    By Jake23 in forum AWT / Swing
    Replies: 1
    Last Post: 02-07-2012, 02:22 PM
  2. Problem Using KeyListener in swing program.
    By gauravfzd in forum AWT / Swing
    Replies: 7
    Last Post: 08-24-2011, 10:13 AM
  3. problems with swing example program
    By mr_sandman in forum New To Java
    Replies: 4
    Last Post: 08-19-2011, 12:23 AM
  4. Simple swing program, help please!
    By N00Bie in forum New To Java
    Replies: 5
    Last Post: 02-17-2011, 05:16 PM
  5. netbeans+swing = painting program?
    By yuriythebest in forum New To Java
    Replies: 3
    Last Post: 12-14-2008, 08:55 PM

Posting Permissions

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