Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8201633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:52:58+00:00 2026-06-07T06:52:58+00:00

Can someone provide a concrete (actual Java code) example of MVP in action? This

  • 0

Can someone provide a concrete (actual Java code) example of MVP in action?

This would include the following 3 types of classes and how they call each other’s methods to achieve the pattern and process/respond to a client-side response:

  • Model – some kind of value object (VO)
  • View – represents or generates the UI
  • Presenters – business logic
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:53:00+00:00Added an answer on June 7, 2026 at 6:53 am

    MVP is my favorite design pattern to create a UI.

    The big difference between MVP and MVC is how to handle the view.

    • In MVC, the Controller manipulates the view, taking care of how to render in the view parts of the user actions and model. That means that the Controller and View have ‘tight coupling’.
    • In MVP, the Presenter takes care of the user tasks, the model is shared between the Presenter and View. So the view renders UI according to the model, sometimes the view can have actions to be called from the Presenter. The Presenter and View can have a defined interface contracts to make them ‘loose coupling’. For example you can create a View for Java Swing UI and another for JavaFX UI. Or if the connection to the data source changes, then you just need update the presenter.

    There are many styles to program the MVP.

    In a formal way, consists in create interfaces for each element of the design pattern.

    /*-- file: Application.java --*/
    import javax.swing.JOptionPane;
    
    /**
     *
     * @author danLeon
     */
    interface LoginModel {
    
        String getUser();
    
        void setUser(String user);
    }
    
    class MyLoginModel implements LoginModel {
    
        String user;
    
        @Override
        public String getUser() {
            return user;
        }
    
        @Override
        public void setUser(String user) {
            this.user = user;
        }
    }
    
    interface LoginView {
    
        LoginPresenter getPresenter();
    
        void setPresenter(LoginPresenter loginPresenter);
    
        void updateModelFromView();
    
        void updateViewFromModel();
    
        void open();
    
        void close();
    
        void userRejected();
    }
    
    class MyLoginView extends javax.swing.JFrame implements LoginView {
    
        private LoginPresenter loginPresenter;
    
        /**
         * Creates new form MyLoginView
         */
        public MyLoginView() {
            initComponents();
        }
    
    
        @SuppressWarnings("unchecked")
        private void initComponents() {
            java.awt.GridBagConstraints gridBagConstraints;
    
            jLabel1 = new javax.swing.JLabel();
            jTextField1 = new javax.swing.JTextField();
            jLabel2 = new javax.swing.JLabel();
            jButton1 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setBounds(new java.awt.Rectangle(0, 0, 0, 0));
            java.awt.GridBagLayout layout = new java.awt.GridBagLayout();
            layout.columnWidths = new int[] {0, 7, 0};
            layout.rowHeights = new int[] {0, 7, 0, 7, 0};
            getContentPane().setLayout(layout);
    
            jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
            jLabel1.setText("Welcome");
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 0;
            gridBagConstraints.gridwidth = 3;
            getContentPane().add(jLabel1, gridBagConstraints);
    
            jTextField1.setColumns(13);
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 2;
            gridBagConstraints.gridy = 2;
            gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
            getContentPane().add(jTextField1, gridBagConstraints);
    
            jLabel2.setText("User");
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 2;
            getContentPane().add(jLabel2, gridBagConstraints);
    
            jButton1.setText("Login");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 2;
            gridBagConstraints.gridy = 4;
            getContentPane().add(jButton1, gridBagConstraints);
    
            pack();
        }
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            getPresenter().login();
        }
    
        private javax.swing.JButton jButton1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JTextField jTextField1;
    
    
        @Override
        public void updateModelFromView() {
            getPresenter().getModel().setUser(jTextField1.getText());
        }
    
        @Override
        public void updateViewFromModel() {
            jTextField1.setText(getPresenter().getModel().getUser());
        }
    
        @Override
        public void open() {
            setVisible(true);
            jTextField1.selectAll();
            jTextField1.requestFocus();
        }
    
        @Override
        public void close() {
            dispose();
        }
    
        @Override
        public void userRejected() {
            jLabel1.setText("Try again!");
            jTextField1.selectAll();
            jTextField1.requestFocus();
        }
    
        @Override
        public LoginPresenter getPresenter() {
            return loginPresenter;
        }
    
        @Override
        public void setPresenter(LoginPresenter loginPresenter) {
            this.loginPresenter = loginPresenter;
        }
    }
    
    interface LoginPresenter {
    
        LoginModel getModel();
    
        void setModel(LoginModel loginModel);
    
        LoginView getView();
    
        void setView(LoginView loginView);
    
        void setOnLogin(Runnable onLogin);
    
        void run();
    
        void login();
    }
    
    class MyLoginPresenter implements LoginPresenter {
    
        LoginModel loginModel;
        LoginView loginView;
        private Runnable onLogin;
    
        @Override
        public LoginModel getModel() {
            return loginModel;
        }
    
        @Override
        public void setModel(LoginModel loginModel) {
            this.loginModel = loginModel;
        }
    
        @Override
        public LoginView getView() {
            return loginView;
        }
    
        @Override
        public void setView(LoginView loginView) {
            this.loginView = loginView;
        }
    
        @Override
        public void setOnLogin(Runnable onLogin) {
            this.onLogin = onLogin;
        }
    
        @Override
        public void run() {
            loginModel.setUser("previousUser");
            loginView.setPresenter(this);
            loginView.updateViewFromModel();
            loginView.open();
        }
    
        @Override
        public void login() {
            loginView.updateModelFromView();
            if (loginModel.getUser().equalsIgnoreCase("root")) {
                loginView.close();
                loginView.setPresenter(null);// for memory sanity. 
                onLogin.run();
            } else {
                loginView.userRejected();
            }
        }
    }
    
    public class Application {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    LoginModel loginModel = new MyLoginModel();
                    LoginPresenter loginPresenter = new MyLoginPresenter();
                    loginPresenter.setModel(loginModel);
                    LoginView loginView = new MyLoginView();
                    loginPresenter.setView(loginView);
                    loginPresenter.setOnLogin(new Runnable() {
    
                        @Override
                        public void run() {
                            JOptionPane.showMessageDialog(null, "Welcome user!");
                        }
                    });
                    loginPresenter.run();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following is excrept from this article on MVVM. Can someone provide example of how
Can someone provide a code example for the given scenario?
Can someone provide for-dummies example of how to use `MonadRandom'? Currently I have code
can someone provide me more insight on why this code works the way it
Can someone provide an example of android code to send simple data to PHP
Can someone provide an example of how to load a .svg file and display
Can someone provide a quick top level explanation of how Valgrind works? An example:
Can someone provide basic sample / link for MVP Framework. Need to design a
Can someone provide a working example in which stored procedure returns a recordset and
Can someone provide a good example of multiple UpdatePanels being updated by a singe

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.