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

  • Home
  • SEARCH
  • 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 8998499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:02:24+00:00 2026-06-16T00:02:24+00:00

I am building a simple app and I am implementing it in a simple

  • 0

I am building a simple app and I am implementing it in a simple MVC pattern where the controller adds event handlers to the view. Here’s a sample controller code attaching a handler to the UI.

Basically, the code adds an event handler when the UI’s save button is clicked. The UI contains the name and id number entry. What I wanted to happen is to pass the name and id number into the actionPerformed function.

ui.onAddStudent(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.print("test");
    }
});

And the receiving function in the UI (in another file) is the following.

public void onAddStudent(ActionListener handler){
    //something missing here
    addStudent.addActionListener(handler);
}

I am not really into Java because it’s not my forte. I actually do JavaScript. Now, a similar handler In JavaScript, one can use the call() or apply() method to call the handler and pass in additional parameters. If the above code was in JS, it would be like

//in the controller
ui.onAddStudent(function(event,id,name){
    //I can use id and name
});

//in the UI
ui.onAddStudent = function(handler){
    //store into a cache
    //add handler to the button
}

//when student is added (button clicked)
handler.call(this,event,id,name);

How do I do the same thing in Java?

  • 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-16T00:02:25+00:00Added an answer on June 16, 2026 at 12:02 am

    You have two choices:

    • let it as it is, and have the controller get the ID and name from the GUI (and that is the easiest and simplest solution, IMHO)
    • use your own Event and Listener types, containing this information. For example:

      public class StudentAddedEvent {
          private long ID;
          private String name;
          ...
      }
      
      public interface StudentAddedListener {
          void studentAdded(StudentAddedEvent event);
      }
      

    The UI would register an ActionListener on the button, and this action listener would do:

    @Override
    public void actionPerformed(ActionEvent e) {
        long id = getIdInGui();
        String name = getNameInGui();
        StudentAddedEvent event = new StudentAddedEvent(id, name);
        for (StudentAddedListener listener : studentAddedListeners) {
            listener.studentAdded(event);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with ASP.NET MVC 2 and building a simple business app. Here are
I am building a PhoneGap/Cordova app and have a simple code that uses http://ricostacruz.com/jquery.transit/
Here's the concept for this relatively simple Flash app I'm building: Check an XML
I'm building a simple data entry app in WPF form using the MVVM pattern.
Building a simple app here; the methods in question: static coin class public static
I've been spending the weekend learning Backbone.js and while building a simple app an
I'm soon going to be starting some iPhone Development (3.0) building a simple app
Is not efficient :). I'm trying to learn django by building a simple app.
I'm building a fairly simple Android app (sdk revision 14: ICS) which allows users
I have an app i building which is a simple naviagtion app. I do

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.