Page 1 of 2 12 LastLast
Results 1 to 20 of 23
Like Tree2Likes

Thread: GUI questions google won't answer

  1. #1
    ObedMarsh is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default GUI questions google won't answer

    So I'm building this banking application for our first semester project, it's basically an abstract superclass called "Accounts" which has two children, "CurrentAccount" and "SavingsAccount". I've written an "AccountsGUI" class which is gonna be the GUI for the whole thing.

    I have been SCOURING the internet for answers to how you connect all this and our book seems to lack examples on anything that helps in this situation, I'm trying to create the GUI as something that has no logical functinality. As in it needs to be "stupid", just reach to information in text fields with the click of a button etc. and the other main program should all the actual functionality by simply building a GUI object and using Objects of type CurrentAccount/SavingsAccount.

    Currently my GUI class looks something like this in the way its setup I'll try to write it in some pseudo like sense.

    GUICLASS {

    // Field Values
    Bunch of components (buttons, textfields etc.) which are going to have some sort of functionality.

    // Constructor
    creates and builds the main frame
    adds components to jpanels(also the field value ones)
    adds jpanels to frame

    }

    // Other classes, one for each actionPerformed method.
    CLASS FOR EACH COMPONENT WITH ACTIONLISTENER IMPLEMENTED ONLY CONTAINING "actionPerformed" METHOD {
    actionPerfo....
    }

    ETC {

    }

    ...

    My question is how do I make this happen? I can't seem to find any concrete information on how you do things this way anywhere and noone seems to be able to answer my question so that I understand the answer. The GUI is working but I can't seem to connect the dots on how I get my main program, accounts classes and guiclass to work together while keeping the GUI dumb and only in a position where it sends information back and forth.

    If I have a bunch of account objects, which represent an account each with it's own specific field values, which are built by the main program, from .dat files where I store the field values... How do I make my GUI buttons etc. pass that information to my main program (the main program is a separate file from all classes called "AccountsMain").

    I could post code but... there's a whole lot so far and posting everything seems redundant.

    I'm not sure I'm explaining myself clearly enough but I hope someone can help me find a way to make this work or tell me if I'm on the wrong track completely.
    Last edited by ObedMarsh; 11-27-2012 at 05:26 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: GUI questions google won't answer

    Your GUI doesn't read anything.
    It should have methods like loadAccount(Account account), which will populate the relevant fields.
    The main program will call that when it has an account to load.

    When the user enters a search and hits the button, the action will (via a SwingWorker) call anything that happens to be listening on the GUI (you'll want a GUIListener interface or maybe more than one, that's entirely up to you), say a searchAccounts(SearchCriteria criteria) method. This will be implemented by your main code (your Controller). That will then find the data then call setSearchResults() on the GUI.

    That's the concept.

    Controller knows about the GUI. The GUI knows about any GUIListeners (in your case probably the Controller will be the only thing implementing that).

    So the GUI accepts data (via methods) which it uses to populate its fields. It sends data (via actions) to its listeners.

    You can break it down more, but that should eb enough to give you your "dumb" GUI.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    ObedMarsh is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default Re: GUI questions google won't answer

    I'm curous about the actionPerformed method, can it be true I need to make a separate class for basically every different "action" I
    want in the GUI and then do this to every single one of those components:

    Java Code:
    <button>.addActionListener( new <classname>() );
    or should I somehow implements these "actionPerformed" methods otherwise? I've managed to make two buttons create new windows (by building new jframes etc.) inside their actionPerformed method, but it's a completely different story when I want to retrieve data from an object... I can't figure out in which class/mainprogram I should be creating the objects(accounts) and should I keep them in an array? Or should I have some completely new class which makes account objects from their .dat files where their field values are stored? This is my main file and the GUI works like this and is built from it's own constructor, but how to manipulate text fields and connect the account objects to the whole thing just escapes me completely for some reason.

    Here is my main file so you understand how I build my GUI from my main file.
    Java Code:
    import java.util.*;
    import java.awt.*;
    import java.awt.Event;
    import java.text.SimpleDateFormat;
    import java.io.*;
    
    
    public class AccountsMain {
    
    
    	public static void main(String [] args) throws FileNotFoundException {
    		
    		AccountsGUI loadGUI = new AccountsGUI();
    		
    	}	// End of main.
    	
    		
    }
    Last edited by ObedMarsh; 11-27-2012 at 10:40 PM.

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

    Default Re: GUI questions google won't answer

    Quote Originally Posted by ObedMarsh View Post
    ... I'm trying to create the GUI as something that has no logical functinality. As in it needs to be "stupid", just reach to information in text fields with the click of a button etc. and the other main program should all the actual functionality by simply building a GUI object and using Objects of type CurrentAccount/SavingsAccount.
    .....
    I'm not sure I'm explaining myself clearly enough but I hope someone can help me find a way to make this work or tell me if I'm on the wrong track completely.
    Your not off track at all, and in fact have some very good ideas going on.

    Quote Originally Posted by ObedMarsh View Post
    I'm curous about the actionPerformed method, can it be true I need to make a separate class for basically every different "action" I
    want in the GUI and then do this to every single one of those components:

    Java Code:
    <button>.addActionListener( new <classname>() );
    or should I somehow implements these "actionPerformed" methods otherwise?
    First a caveat: I am not a professional programmer nor have I taken a programming course except one semester of Intro to Pascal in 1979, and so most all that I've learned has been self-taught. So please take anything that I have to say with a large grain of salt and a healthy dose of skepticism.

    But I've found that several options have worked well for me. One option is to create anonymous inner classes for your button/menu Actions and to have these inner classes do little more than call the methods of a single Control class. Another is to create more complex stand-alone classes that extend AbstractAction for the button/menu listeners. But either way, I strongly agree that you're on the right track in trying to extract the logic out of your GUI.


    I've managed to make two buttons create new windows (by building new jframes etc.) inside their actionPerformed method,...
    Be careful with this as it can be tough on the user to see windows swapping on the screen, especially if it is done frequently. An occasional dialog such as a modal log-in dialog is a good idea, but major changes in GUI view is sometimes better accomplished by swapping JPanels via a CardLayout.

    ... but it's a completely different story when I want to retrieve data from an object... I can't figure out in which class/mainprogram I should be creating the objects(accounts) and should I keep them in an array? Or should I have some completely new class which makes account objects from their .dat files where their field values are stored?
    A lot of this will depend on your program requirements, and may even change over time. For a small academic demo program, a simple ArrayList might work well to hold Accounts, but for a more complex project, a database would work better. Regardless, if the program logic is well separated from the view code, the same GUI code would work well with either.

    Here is my main file so you understand how I build my GUI from my main file.
    Java Code:
    import java.util.*;
    import java.awt.*;
    import java.awt.Event;
    import java.text.SimpleDateFormat;
    import java.io.*;
    
    
    public class AccountsMain {
    
    
    	public static void main(String [] args) throws FileNotFoundException {
    		
    		AccountsGUI loadGUI = new AccountsGUI();
    		
    	}	// End of main.
    	
    		
    }
    That's a good start since you want the main method to be small. You'll also want to make sure though that you start the Swing GUI on the Swing event thread by starting it in a Runnable that is queued on the event queue via SwingUtilities.invokeLater(...).

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: GUI questions google won't answer

    For Swing listeners my preferred is the anonymous class, usually extending the Abstract version (so you don't have to implement the methods you're not interested in).
    The methods would then simply call a method locally to do the actual processing if there is more than one or two lines. I find trying to write the logic actually in the anonymous class is a bit messy.
    Something like:
    Java Code:
    deleteEntryButton.addActionListener(new AbstractAction() {
        public void actionPerformed() {
            deleteEntry();
        }
    });
    ...
    private void deleteEntry() {
       // Stick the GUI deletion logic in here
       // It will then call any listeners to do any logical deletion.
    }
    It just means that you can easily add some additional stuff in there without clogging up your layout code.
    For example you might decide to add a pop up that asks "Are you sure?" or something like that.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    ObedMarsh is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default Re: GUI questions google won't answer

    Thanks for the great replies!

    Fubarale: I like your idea with the inner classes, if I'm guessing correctly this should make reaching the field values (text fields etc.) in the GUI class file possible while still being able to create separate methods for various actions etc. It's something like that which I'm looking for. Also regarding the implementation of account objects into the GUI... I'm not sure what you mean by "controller" don't think we've gone through that. But should I make an accounts arraylist as a field value in the GUI so that I can manage the account objects while the program is running or should the GUI interact with the .dat files containing field value info for the account objects? I wanna keep the GUI apart from the accounts classes yes, but surely I need to build like a separate GUI constructor, which calls the normal constructor and then passes the accounts object into the GUI object as a parameter?

    To simplify that last part, something like this:
    Java Code:
    // normal constructor
    AccountsGUI loadGUI = new AccountsGUI();
    
    // AccountsGUI which can handle an array of accounts 
    AccountsGUI loadGUI = new AccountsGUI( ArrayList<Accounts> accountList );
    
    // AccountsGUI which can handle two separate arrays of accounts.
    AccountsGUI loadGUI = new AccountsGUI( ArrayList<CurrentAccount> currentAccList, ArrayList<SavingsAccount> savingsAccList );
    Or am I off base?

    My teachers advise was something like, "remember that a GUI class is still just a class". Which is kind of a tease but I'm assuming he wants me to use methods I normally now from average (non-GUI) classes to make it work?
    Last edited by ObedMarsh; 11-28-2012 at 02:35 PM.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: GUI questions google won't answer

    The GUI class will have a way of being supplied a collection of Accounts.
    Which goes back to what I said about the GUI accepts data.
    There's no need to give that data in the constructor.
    Fubarable likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    ObedMarsh is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default Re: GUI questions google won't answer

    I don't think it's part of our course so far since we haven't heard the mention of anonomous classes yet, but I read up on i abit. As far as I can tell it's basically a syntax that goes:

    Java Code:
    new ActionListener() {
        public void actionPerformed(ActionEvent event)
        {
            // do something.
        }
    }
    which you put inside the parenthesis on the declarations:
    Java Code:
    <button>.addActionListener( <insert code here> );
    How does this work exactly and will I be able to acces field values in the top GUI class from them?

    Also I'm not sure I'm understanding that part about
    The GUI class will have a way of being supplied a collection of Accounts.
    It's the "way" I'm kinda stuck on figuring out?

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: GUI questions google won't answer

    The anonymous class has access to all member variables, and any final variables in the method it is declared.
    It can also call methods in the parent, which is what I was showing above (post 5).

    The "way" is a method.
    Java Code:
    public class GUI {
    ... setup gui stuff ...
       public void showAccounts(List<Account> accounts) {
          ... code for  showing the given accounts.
       }
    }
    That would be called by your Controller when it has a list of accounts to show.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    ObedMarsh is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default Re: GUI questions google won't answer

    This has been a huge help so far, my components can now interact and I'm getting the hang of attaching these inner classes to my buttons and making actions work from various text fields/areas. So all that's left for me to learn is figuring out the system thats gonna build the arraylist from the files/backup files where theyre respective field values are stored I think abit of grinding and many cups of coffee should help me solve that, though I would appreciate any advice and wholeheartedly thank you for all your help so far! :)

    Also since I'm gonna be making methods in the AccountsGUI class itself, which loads accounts/arraylists of accounts, I can assume I need a field value in the AccountsGUI class to put it in right?
    Last edited by ObedMarsh; 11-29-2012 at 02:18 AM. Reason: More to add...

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: GUI questions google won't answer

    I wouldn't.
    Your AccountsGUI should not be reading the data in in the first place.
    That's the job of the business side of your app.

    The normal structure is something like:
    GUI (talks to each other) Controller (gets data from) Data source
    The stuff that gets passed in those links is your model.

    The data source is usually fronted by DAOs (Data Access Object) which interact with whatever the source of the data is, eg a database or file or web service. The point being that your controller doesn't give a monkeys where the data comes from. In your case it would be from the file.

    From your GUIs point of view it just wants to display what it's been given, and hold enough info to be able to ask for more. So a displayed list of accounts only needs to hold the info that's actually displayed (account number and name maybe?) and the id for the account (maybe the same as the account number, depends on your model). The id is so it can request all the details if that account is selected.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    ObedMarsh is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default Re: GUI questions google won't answer

    Ok so as far as I can understand so far I have the "Model" and the "View" in a "Model View Controller" setup, the model being my logical accounts classes and the View being the GUI. It's the controller part I'm not quite understanding. Is this a separate class that handles interaction in some way? Or is it an interface you implement in the GUI, as in a list of methods you implement in the GUI to get info from files? Or should I have the methods to get info from files inside my logical classes and then make methods to pass those accounts into the GUI, using the GUI's own methods? I feel like I'm on the virge of understanding this but I'm missing some last ingredient.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: GUI questions google won't answer

    That class handles all the "business logic", or at least fobs it off onto other layers (eg the aforementioned DAOs).
    It is separate and will implement some form of Listener, which is what the GUI is expecting to be registered.
    The reason there is that the GUI should not care what the controller actually does. It just needs to know it implements whatever methods are on the Listener.

    So if you click a button on the GUI that requires more data (eg display full account data) then the GUI will tell the Controller (which is a Listener on the GUI) that a request has been made for account id xxxxxxx. The Controller will then get that information and pass it to the GUI, probably via a showAccount() method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    ObedMarsh is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default Re: GUI questions google won't answer

    I feel like you keep explaining but that thick crusty wall in my head won't crumble, anyways I think I got it to a certain degree. So I need to implement an ActionListener in the controller class? And make static methods I can use inside the GUI in this sense
    Java Code:
    <controller>.method(<something>);
    Then make methods in the controller which reaches into my datafiles? I'm curious as to how I'm supposed to implement the evenlisteners in the controller class? Should I make a method for each function in the GUI, in the controller class? How do I implement the "actionperformed" part to the controller class, so that there is an actionPerformed method for every action? I can't seem to find anything that shows me how this is done.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: GUI questions google won't answer

    These aren't event listeners.
    The GUI will accept your own listener, say GUIListener, whose methods are defined by whatever it is the GUI is going to have to ask the controller for, so maybe getAccount(int id).
    This is not any form of Swing listener.

    The controller will implement this interface (they're always interfaces).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

  17. #17
    ObedMarsh is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default Re: GUI questions google won't answer

    I think I found a very good way, which separates the logic from the GUI. I made a controller class which has inner classes, that I assigned to components in the GUI through the controller class, as actionlisteners. Then I made corresponding methods to use in those classes (in the actionPerformed method inside them), which reaches through the GUI and gets data from text fields etc. (using methods I made inside the GUI). I also made static methods inside the logical classes to reach to the files and get the data which I store as "Accounts"(superclass) objects in a field value arraylist, inside the controller class.

    If this makes sense to you, does it sound like a solid solution? I mean the logic is completely separate from the GUI class this way and it seems to work perfectly so far.
    Last edited by ObedMarsh; 12-02-2012 at 09:30 PM.

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

    Default Re: GUI questions google won't answer

    I can't speak for all of your comments, but I can tell you that your use of static methods is not a good one, and that you most definitely should not be doing this, unless you're talking about stateless utility methods, but it doesn't sound like this to me.

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: GUI questions google won't answer

    It's a solution, but it still sounds to me like you are tying your GUI and controller together too tightly.
    Just having ActionListeners in the Controller, for example.
    But if you're comfortable with that then you may as well continue down that line.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: GUI questions google won't answer

    Quote Originally Posted by Tolls View Post
    These aren't event listeners.
    The GUI will accept your own listener, say GUIListener, whose methods are defined by whatever it is the GUI is going to have to ask the controller for, so maybe getAccount(int id).
    This is not any form of Swing listener.

    The controller will implement this interface (they're always interfaces).
    Tolls: can you elaborate on this a bit more. I'm trying to improve my Swing MVC coding and would like to incorporate your ideas but am not clear on what you've stated here. Again, thanks!

Page 1 of 2 12 LastLast

Similar Threads

  1. What is the Right Answer :
    By sachin218 in forum New To Java
    Replies: 2
    Last Post: 11-04-2012, 02:41 AM
  2. google map
    By karyoker in forum New To Java
    Replies: 2
    Last Post: 07-29-2012, 05:36 AM
  3. Compare true Answer /Player answer
    By Eilime in forum New To Java
    Replies: 1
    Last Post: 04-24-2011, 08:51 PM
  4. Could anyway answer me this?
    By Jojomofoman in forum New To Java
    Replies: 14
    Last Post: 12-16-2010, 11:14 PM
  5. Help me answer a few questions
    By namlunxxx in forum XML
    Replies: 3
    Last Post: 06-16-2010, 05:26 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
  •