将JMenuItem传递给控制器​​类 [英] Passing JMenuItem to Controller Class

查看:141
本文介绍了将JMenuItem传递给控制器​​类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC框架的View类中有3组 JMenuItem 。我想在控制类 EventController 中引用它们。有人可以告诉我这是否实现?下面是事件控制器。 EventView由 JMenuItem addEvent,editEvent,deleteEvent组成,我如何在Controller类中为它们设置侦听器。有人可以使用我的Controller类的示例代码吗?

  public class EventController实现ActionLister {
private EventModel model;
私有EventView视图;
private ActionListener actionListener;


public EventController(EventModel model,EventView view){
this.model = model;
this.view = view;
}

第二个问题,我只能更新一个 JTable c code code code code code code code code code code code code $ public void updateEventTable(){
try {
String sql =SELECT date as'Date',eventName as'Name',time as'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(异常e){
JOptionPane.showMessageDialog(null,e);
} finally {
try {
rs.close(); pst.close(); conn.close();;
} catch(SQLException e){}
}
}

这是在View Class中有代码的坏习惯,应该理想地在Model中,但是如何做到这一点可以告诉我。这些概念对我而言是新的,并且想要学习。因为 JTable 我觉得很困难

解决方案

让你的模型出口 动作 可以根据需要添加到菜单和按钮。因为数据库访问本质上是异步的,所以每个这样的操作都使用工作线程< a>在EDT上更新表模式时在后台查询数据库。另请参阅 关于Swing组件和模型之间的关系。



附录:你能给我一个代码示例吗?




  • FileMenu 是使用 Action 来封装功能的一个非常基本的例子。


  • href =http://stackoverflow.com/a/11944233/230513>这里在 JToolBar中更广泛地使用 Action / code>。


  • JHotDraw ,引用这里,是一个非常复杂的例子,更改基于上下文的可用 Action 实例,如此处所述。


  • 示例提供了Swing中MVC的一般检查。



I Have 3 sets of JMenuItem on a View class of the MVC framework. I want to refer to them in the controll class EventController. Can someone show me this is achieved? Below is the event Controller. Class EventView consists of JMenuItem addEvent, editEvent, deleteEvent, how do I do the listeners for them in the Controller class. Can someone demonstrate using a sample code on top of my Controller class?

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


public EventController(EventModel model, EventView view){
    this.model = model;
    this.view = view;
}

Second question, I can only update a JTable from the View class itself, so

public void updateEventTable() {
    try {
        String sql = "SELECT date as 'Date',eventName as 'Name', time as '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){}
    }
}

This is bad practice having codes in View Class, Should ideally be in Model, but how to do this can you show me. The concepts is new to me and want to learn. Because of JTable I find it very difficult

解决方案

Let your model export instances of Action that can be added to menus and buttons as needed. Because database access is inherently asynchronous, let each such action use a worker thread to query the database in the background while updating the table mode on the EDT. See also A Swing Architecture Overview concerning the relationship between Swing components and models.

Addendum: Can you show me a code sample?

  • FileMenu is a very basic example of using Action to encapsulate functionality.

  • The example cited here uses Action more extensively in a JToolBar.

  • JHotDraw, cited here, is a very complex example that changes the available Action instances based on context, as discussed here.

  • This example offers a general examination of MVC in Swing.

这篇关于将JMenuItem传递给控制器​​类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆