这是一个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());
}
}
这是Model Class:
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
重写方法是错误的。会是什么呢?
答案 0 :(得分:2)
我不确定我的回答会对您有所帮助,但应使用ListSelectionListener
检测选择更改。它更好,因为它不仅为鼠标点击提供反应,而且还为关键事件提供反应。
要将监听器添加到表中,只需使用表的选择模型:
myTable.getSelectionModel().addListSelectionListener(myListener)
这个改变可能会帮助您解决问题