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 9171215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:07:57+00:00 2026-06-17T16:07:57+00:00

I am very confused, many examples I have seen only deal with one button

  • 0

I am very confused, many examples I have seen only deal with one button listener being passed. I want to be able to pass ALL Components of a View Class (JFrame) to the Controller Class which will have easy access to the listeners of those components. How Can I do this easily. This is what I’ve done so far with JMenuItem, but in the JFrame i have more buttons, dont want to repeat in the Constructor every time.

 public class EventController implements ActionListener {
    private EventModel model;
    private EventView view;



    public EventController(){
        model = new EventModel(); 

    }


    @Override
   public void actionPerformed(ActionEvent e) {
       if (e.getActionCommand().equals("add")) {
       JOptionPane.showMessageDialog(null, "add button clicked");
   }   else if (e.getActionCommand().equals("edit")) {
       JOptionPane.showMessageDialog(null, "edit button clicked");
   }   else if (e.getActionCommand().equals("delete")) {
       JOptionPane.showMessageDialog(null, "delete button clicked");
  }


    }   

//class View

  public class EventView extends javax.swing.JFrame {
Connection conn = JavaConnect.ConnectDB();
PreparedStatement pst = null; 
ResultSet rs = null; 


    public EventView() {
        initComponents();
        updateEventTable();
        addEvent.addActionListener(new EventController());
        addEvent.setActionCommand("add");
        editEvent.addActionListener(new EventController());
        editEvent.setActionCommand("edit");
        deleteEvent.addActionListener(new EventController());
        deleteEvent.setActionCommand("delete");
    }
    public void updateEventTable() {
        try {
            String sql = "SELECT date as 'Date',eventName as 'Name', time as 'Start Time' FROM Event";
             pst = conn.prepareStatement(sql); 
             rs = pst.executeQuery();
             tableEvent.setModel(DbUtils.resultSetToTableModel(rs));
             tableEvent.getColumnModel().getColumn(0).setPreferredWidth(80);
             tableEvent.getColumnModel().getColumn(1).setPreferredWidth(170);
             tableEvent.getColumnModel().getColumn(2).setPreferredWidth(110);  
    }
        catch (Exception e ) {
                     JOptionPane.showMessageDialog(null, e);
        } finally {
            try {
                rs.close(); pst.close();conn.close();;
            } catch(SQLException e){}
        }


    }
  • 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-17T16:07:58+00:00Added an answer on June 17, 2026 at 4:07 pm

    Add a method like this:

    private void setListener(AbstractButton button, ActionListener listener, String command) {
      button.addActionListener(listener);
      button.setActionCommand(command);
    }
    

    Build a single EventController instance.

    EventController controller = new EventController();
    

    Then, call it for each button:

    setListener(addEvent, controller, "add");
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very confused and am hoping someone might have had this problem and be
I am very confused. I have this lambda expression: tvPatientPrecriptionsEntities.Sort((p1, p2) => p1.MedicationStartDate .Value
hello i have just start using Raphael but i'm very confused in the following
I have read the docs and everything but I'm very confused. I never needed
I have seen many sites that are responsive both on desktop browsers and mobile
I have seen many samples to hard code the number of texture unit in
I am very confused with the Event Handling in Blackberry. I facing many problems
I am very confused in using finish() for activity and using back button of
Have a web-page with very complex structure and javascript logic with many ajax calls,
Being fairly new to the .Net world, I am getting very confused about where

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.