Results 1 to 2 of 2
  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
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Re: Seperating the View fromt the Logic

    To the moderators:
    Sorry about this double post. I didn't realize I had double posted my computer must've lagged and sent 2 requests. It happened when I was writing the message and then it logged me out. When I logged in it showed only one message on my screen. I only noticed now. Sorry once again.

Similar Threads

  1. Seperating Code into differnet methods
    By Log1c in forum New To Java
    Replies: 0
    Last Post: 04-13-2012, 12:39 AM
  2. String seperating Help.
    By dylanka in forum New To Java
    Replies: 4
    Last Post: 11-25-2011, 01:32 AM
  3. Need help in logic
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 07:44 PM
  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
  •