0

这是 MVC 的控制器类:

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


    /** Constructor */
   public EventController(EventModel myModel, EventView myView){
       model = myModel;
       view = myView;     
    }

   public void setUpListeners() {
       this.view.addEventButton.addActionListener(this); 
       this.view.addEventMenuItem.addActionListener(this);
       this.view.editEventMenuItem.addActionListener(this);
       this.view.tableEvent.addMouseListener(this);

   }

   @Override
   public void actionPerformed(ActionEvent e){
       Object button = e.getSource();
       if(button==this.view.addEventButton) {
           setEventDetails();          
       }
       else if (button==this.view.addEventMenuItem) {
           this.view.addDialog.setVisible(true);
       }
       else if(button==this.view.editEventMenuItem) {
           this.view.editDialog.setVisible(true);
       }

   }
   /*to change the display of label text of the VIEW according to the selected row of the table tableEvent*/

    @Override
    public void mouseClicked(java.awt.event.MouseEvent event) {
      int rowSelected = view.tableEvent.getSelectedRow();
      String tableClick = view.tableEvent.getModel().getValueAt(view.tableEvent.convertRowIndexToModel(rowSelected), 0).toString();
      Events e = model.getEvent(tableClick); //tell model to change its state based on user input on views 
      view.changeDisplay(e);
    }

这是 View Class 及其 changeDisplay() 方法来适当地更改标签文本:

public class EventView extends javax.swing.JFrame {
private EventModel model;
public void changeDisplay(Events e) {
       evTitle.setText(""+e.getEventName());
       evWhen.setText("When: "+ e.getEventDate());
       evWhere.setText("Where: "+ e.getEventVenue());
       evDescription.setText("Description: "+ e.getEventDetail());
       evOpportunity.setText("Opporunity: "+ e.getEventOpportunity());
       evMoreDet.setText("More Details: "+ e.getEventMoreDetails());
   }


}

这是模型类:

 public class EventModel {
        Connection conn = JavaConnect.ConnectDB();
        PreparedStatement pst = null;
        ResultSet rs = null; 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Events e = new Events(); 

public void addEvent(String name, Date date,String start, String venue, String details,
String opportunity, String more, String end) throws SQLException {
    try {
    String qry = "INSERT INTO EVENT(eventName,date,time,venue,details,opportunity,moreDetails,endTime) VALUES (?,?,?,?,?,?,?,?)";
    pst = conn.prepareStatement(qry);
    pst.setString(1, name);
    pst.setString(2, sdf.format(date));
    pst.setString(3, start);
    pst.setString(4, venue);
    pst.setString(5, details);
    pst.setString(6, opportunity);
    pst.setString(7, more);
    pst.setString(8, end);
    pst.executeUpdate(); 
    }
    finally{
       try{ pst.close(); } 
       catch (SQLException se) {}
      }
}



public Events getEvent(String tableClick) {


    try {
        pst = conn.prepareStatement("SELECT * FROM Event WHERE eventID='"+tableClick+"' ");
        rs = pst.executeQuery();
        while(rs.next()){      
        e.setEventName(rs.getString(2));
        System.out.println(rs.getString(2));
        e.setEventDate(rs.getString(3));
        e.setEventTime(rs.getString(4));
        e.setEventVenue(rs.getString(5));
        e.setEventDetail(rs.getString(6));
        e.setEventOpportunity(rs.getString(7));
        e.setEventMoreDetails(rs.getString(8));
        e.setEndTime(rs.getString(9));
        rs.close();
        pst.close();
       }
    } 
    catch(SQLException ex){
    ex.printStackTrace();
    } 
    return e;
} //end getEvent



}

单击表格行正确更改标签文本

我的程序有一个小错误,我似乎无法修复很久。基本上,每当我单击表格 ( tableEvent) 的行时,它应该在表格旁边的标签中显示适当的文本,如屏幕截图所示,事件“软件开发职业事件”。这在第一次执行时工作正常,但是mouseClicked在我通过addEvent模型的方法添加事件后,控制器中的事件方法不会改变文本的显示。我不确定它是什么,是我在实例化类对象时做错了什么Events,我的addEvent方法是错误的,还是控制器中实际mouseClicked覆盖的方法是错误的。会是什么呢?

4

1 回答 1

2

我不确定我的回答是否对您有帮助,但应该使用ListSelectionListener. 它更好,因为它不仅为鼠标点击提供反应,还为关键事件提供反应。要将侦听器添加到表中,只需使用表的选择模型:

myTable.getSelectionModel().addListSelectionListener(myListener)

可能此更改可以帮助您解决问题

于 2013-01-22T22:22:58.260 回答