Results 1 to 4 of 4
  1. #1
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Seperating the View fromt the Logic

    Hello everyone I have a little question that's been nagging me for a bit. I'm practicing using the components of the Swing library and came across this particular problem. I wanted to know if there is any way that I can separate an actionListener/action object from the swing component that uses it while providing a way to still access it for updating. I know this kinda sounds ridiculous but my goal is to be able to make up a generic interface that can be applied to an arbitrary amount of processing types. For example here is my code ignoring the imports:
    Java Code:
    public class BasicTextIOFrame extends JFrame {
        
        private String input;
    
        public BasicTextIOFrame(String title, String prompt) {
            setTitle(title);
            JPanel centerPanel = new JPanel();
            
            JLabel promptLabel = new JLabel(prompt, SwingConstants.RIGHT);
            final JTextField inputBox = new JTextField(new JTextFieldLimit(20), 
                                                      "", 12);
            centerPanel.add(promptLabel);
            centerPanel.add(inputBox);
            
            add(centerPanel, BorderLayout.CENTER);
            
            JPanel southPanel = new JPanel();
            
            JButton submit = new JButton("Submit");
            submit.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent event) {
                        input = inputBox.getText().trim();
                        inputBox.setText(ReverseStringGUI.calculate(input));
                    }
                });
            
            southPanel.add(submit);
            add(southPanel, BorderLayout.SOUTH);
            pack();
        }   
    }
    Here I want the actionListener to be able to process a string (such as calculating an inputed string and reversing it) or processing another problem such as one that implements an integer (processing an int and squaring it) but as you can see my behavior is dependent on the view components (.getText() and setText()).

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: Seperating the View fromt the Logic

    You could just have the actionListener make a call back to a general purpose method to handle components. This would best be done by defining an interface. That way, your actionListener does not need to know anything about the component it is handling. The interface implementation would have all of that info.

    Also, a couple of items. It is best not to extend JFrame. Simply swap the roles of JFrame and JPanel and add the panel to the frame. And JTextField will also process actions.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Re: Seperating the View fromt the Logic

    Quote Originally Posted by jim829 View Post
    You could just have the actionListener make a call back to a general purpose method to handle components. This would best be done by defining an interface. That way, your actionListener does not need to know anything about the component it is handling. The interface implementation would have all of that info.

    Also, a couple of items. It is best not to extend JFrame. Simply swap the roles of JFrame and JPanel and add the panel to the frame. And JTextField will also process actions.

    Regards,
    Jim
    So I'm guessing in this case the component (JTextField) would have to be made externally from the class and passed into an ActionListener class then the same instance would be passed to the BasicTextIO class.

    Also thanks for the tip. From the book I'm reading Core Java I had thought that extending JFrame and most Swing components was common practice. I will extend from JPanel. Now I'm just a bit confused on when to extend as well, will research this as saying "is a" in this case seems a bit ambigious (ex. BigFrame extending and overwriting size attributes).
    Last edited by typedef; 02-09-2014 at 11:25 PM.

  4. #4
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Re: Seperating the View fromt the Logic

    Just as an update in case anyone later looks at this for reference of what was done.
    My design after feedback was as follows:
    -- I made a Problem interface that extended ActionListener. This interface contains this method signature:
    Java Code:
    public String calculate()
    -- I made a RevStringAction class that contains the component that will receive give the input and will receive the output. It implemented ActionListener and Problem.
    -- I decided that what I would do is make multiple ActionListeners for different problems each containing its own calculate method.
    -- I also made it so that instead of having a class extend JFrame or JPanel a class would just be considered as a GUI with multiple instance variable components that would be assembled in the constructor. For this I used a method signature:
    Java Code:
    public BasicTextIOGUI(String title, String prompt, JTextField IOField, Problem aProblem)
    -- From here its just a matter of initializng a JTextField component, then the ActionListener, then the actual BasicTextIOGUI.

    Anyways thanks for the help jim829
    Last edited by typedef; 02-10-2014 at 03:08 AM.

Similar Threads

  1. Seperating the View fromt the Logic
    By typedef in forum AWT / Swing
    Replies: 1
    Last Post: 02-10-2014, 01:04 AM
  2. Seperating Code into differnet methods
    By Log1c in forum New To Java
    Replies: 0
    Last Post: 04-13-2012, 12:39 AM
  3. String seperating Help.
    By dylanka in forum New To Java
    Replies: 4
    Last Post: 11-25-2011, 01:32 AM
  4. How enter this mode-gui/design-view/code-view
    By lse123 in forum NetBeans
    Replies: 0
    Last Post: 02-28-2010, 10:09 PM
  5. Updating a view using actions in a seperate view
    By xcallmejudasx in forum Eclipse
    Replies: 0
    Last Post: 10-24-2008, 10:24 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
  •