2

从类模型:

    public TableModel getTableData() {
     TableModel model=null;
    try {
        String sql = "SELECT eventID as 'Event ID', date as 'Date',eventName as 'Name', time as 'Start Time' FROM Event";
        pst = conn.prepareStatement(sql); 
        rs = pst.executeQuery();
        model = (DbUtils.resultSetToTableModel(rs));       
        }
    catch(SQLException e){
        e.printStackTrace();
    }
   finally {
        try {rs.close(); pst.close();  }
        catch(SQLException e){}     }                                 
       return model; 
}

public void getEvent(String tableClick) {
    Events e = new Events();
    try {
        pst = conn.prepareStatement("SELECT * FROM Event WHERE eventID='"+tableClick+"' ");
        rs = pst.executeQuery();
        while(rs.next()){      
        e.setEventName(rs.getString(2));
        e.setEventDate(rs.getDate(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));
       }
    } 
    catch(SQLException ex){
    ex.printStackTrace();
    } finally {
        try {
        rs.close();pst.close();conn.close();} 
        catch (Exception ex){}
      }
} //end getEvent

从类控制器:

@Override
    public void valueChanged(ListSelectionEvent event) {
        int rowSelected = view.tableEvent.getSelectedRow();
        String tableClick = view.tableEvent.getModel().getValueAt(view.tableEvent.convertRowIndexToModel(rowSelected), 0).toString();
        model.getEvent(tableClick); //tell model to change its state based on user input on views - model is instance of class model
    } 

当我运行我的程序时,堆栈跟踪pst = conn.prepareStatement("SELECT * FROM Event WHERE eventID='"+tableClick+"' ");在模型类的行model.getEvent(tableClick);和控制器类的行显示错误。在 NetbeanIDE 中以调试模式运行带有断点的程序后,我只能看到e.setEventName(...)正在填充数据库中的值,而Eventbean 类的所有其他值都是空的。e.setEventDate(...)将日期设置为#2222。如果您需要更多信息,我将编辑我的帖子。但这真的让我很烦。

是否是 ListSelectionListener 引起的问题,也许mouseListener更合适?

进一步补充,如果我getEvent()改变

public void getEvent(String tableClick) {
Events e = new Events();
try {
    pst = conn.prepareStatement("SELECT * FROM Event WHERE eventID='"+tableClick+"' ");
    rs = pst.executeQuery();
    while(rs.next()){      
    JOptionPane.showMessageDialog(null, "Yess");

   }
} 

它仅Yes在第一次单击行时显示。不是第二次点击另一行。

4

2 回答 2

1

使用 MouseListener 来检测点击的表格行。

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

也是 方法 getEvent()rs.close();pst.close();的块的结尾。try

于 2013-01-20T15:01:33.393 回答
1

使用 a ListSelectionListener(如此处所示)检测选择的变化。使用 aJButton和 a CellEditor(如此处所示)调用特定行的操作。

于 2013-01-20T15:22:53.090 回答