public class EventView extends javax.swing.JFrame {

Connection conn = JavaConnect.ConnectDB();
PreparedStatement pst = null; 
ResultSet rs = null; 

private EventModel model;

     /** Constructor */
    public EventView(EventModel model) {
        initComponents();
        this.model = model;
        updateEventTable();       
    }


   public void addEventListener(ActionListener al) {
        addEventButton.addActionListener(al);
    }

  /* public void clearListener(ActionListener cl) {
        clearEventButton.addActionListener(cl);
    }*/
   public void addDialogListener(ActionListener ae) {
        addEvent.addActionListener(ae);

    }
   public void editDialogListener(ActionListener ee) {
        editEvent.addActionListener(ee);
    }


   public String getEventName() {
        return txtName.getText();
    }
   public  Date eventDate() {
        return txtDate.getDate();
    }
   public String startTime() {
        return txtStart.getText();
    }
   public String endTime() {
        return txtEnd.getText();
    }
   public String locationWhere() {
        return txtWhere.getText();
    }
   public String getDetails() {
        return txtDetails.getText();
    }
   public String getOpportunities() {
        return txtOppor.getText();
    }
   public  String getMore() {
        return txtMore.getText();
    }

   public void showError(Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
   public void showSuccess(String e) {
        JOptionPane.showMessageDialog(null, e);
    }


    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){}
        }


    }


     public static void main(String args[]) {

     public void run() {
               EventModel model  = new EventModel();
               new EventView(model).setVisible(true);
            }
        });


     }





public class EventController {

    //... The Controller needs to interact with both the Model and View.
    private EventModel model;
    private EventView view;


    /** Constructor */
    public EventController(EventModel model, EventView view){

        this.model = model;
        this.view = view;
        //... Add listeners to the view.
        view.addEventListener(new addEventListener());       
        view.addDialogListener(new addDialogListener());
        view.editDialogListener(new editDialogListener());
    }

    private class addEventListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String name = "";
            String date;
            String start="";
            String end="";
            String venue="";
            String details="";
            String opportunities="";
            String moreOppor="";

            try {
                name = view.getEventName();
                date = view.eventDate().toString();
                start = view.startTime();
                end = view.endTime();
                venue = view.locationWhere();
                details = view.getDetails();
                opportunities = view.getOpportunities();
                moreOppor = view.getMore();
                model.addEvent(name,date,start,venue,details,opportunities,moreOppor,end);    
                view.showSuccess("Event Added!");

            } catch (Exception ex) {
                view.showError(ex);
            }
        }
    }


  private class addDialogListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
           System.out.println("1");

        }
    }
   private class editDialogListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
             System.out.println("2");
        }
    }






} // end class

I am unable to execute the events of the components being clicked. why is this? what have i done wrong

That code is very difficult to understand, especially because you have ignored Java naming conventions and made your classes look like variables. And essential parts are missing.
From what you have posted:
There's a main method that isn't valid Java, but appears to try to define a run() method that never gets called, but if it did it would create a view and a model, but there's no code that would create a controller.

Edited 3 Years Ago by JamesCherrill

I was following this James ... Click Here. I assumed I didnt need a Main Method and by simply calling view.setvisible() on another button on another gui, would run everything smoothly. From the link, If i just create a main method as they did, would it work?

The main method is what executes when the program starts. Without that nothing happens. Also the link shows a main that instantiates the controller as well as the model and view. That's a good place to start.

I have another GUI called Mainmenu from which a button is pressed to navigate to the view class above. so what do I type in main menu class to execute the view class along with all the model and controller. Rather than have a main method class seperately since I'm using netbeans.

In general, you would have a controller that is instatiated at startup, in the main method, and that would be responsible for creating models and views, and making windows visible/invisible as required. In a larger GUI you could have multiple controllers, each handling different subsets of the model(s) and views, but for a learning exercise a single controller would usually be enough.

This article has been dead for over six months. Start a new discussion instead.