0

我想发布一个关于我特别想做的事情的问题,但不知道如何解决这个问题,因为我是新手JTables。这就是我的自动取款机: 这就是我的atm

但我想这样安排表格:

在此处输入图像描述

因此,在特定日期下,显示与该日期相对应的所有事件,而不是像上面那样单独显示。

以下是有问题的相关分类,我不确定在哪里以及如何进行交替以让我实现上述目标。

public class DbUtils {
    public static TableModel resultSetToTableModel(ResultSet rs) {

        try {
            ResultSetMetaData metaData = rs.getMetaData();
            int numberOfColumns = metaData.getColumnCount();
            Vector columnNames = new Vector();

            for (int column=0; column < numberOfColumns; column++) {
                columnNames.addElement(metaData.getColumnLabel(column + 1));              
            }
            Vector rows = new Vector(); 
            while(rs.next()) {
            Vector newRow = new Vector(); 
            for (int i =1; i <= numberOfColumns; i++) {
            newRow.addElement(rs.getObject(i));
            }
            rows.addElement(newRow);
            } 
            return new DefaultTableModel(rows,columnNames);

           }  catch (Exception e) {
              e.printStackTrace();      
             return null; 
        }



    }
}



public class EventModel {

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 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));
        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));
       }
    } 
    catch(SQLException ex){
    ex.printStackTrace();
    } finally {
            try {
                rs.close();
                pst.close();
            } catch (Exception e) {
            }
        }  
    return e;
} //end getEvent



}



public class EventController {
private KeyAdapter keyAdapter = new KeyAdapter() {
        @Override
        public void keyReleased(java.awt.event.KeyEvent e) {
            if((e.getKeyCode()==KeyEvent.VK_UP) || (e.getKeyCode()==KeyEvent.VK_DOWN)) {
             changeEvent();           
        }
    }
  };  

  public void changeEvent() {
      int rowSelected = view.tableEvent.getSelectedRow();
      tableClick = view.tableEvent.getModel().getValueAt(view.tableEvent.convertRowIndexToModel(rowSelected), 0).toString();
      events = model.getEvent(tableClick); //tell model to change its state based on user input on views 
      view.changeDisplay(events);

  }

}



public class EventView {
public void changeDisplay(Events e) {
       evTitle.setText(""+e.getEventName());
       evWhen.setText("When: "+ e.getEventDate());
       evWhere.setText("Where: "+ e.getEventVenue());
       evDescription.setText(""+ e.getEventDetail());
       evOpportunity.setText(""+ e.getEventOpportunity());
       evMoreDet.setText(""+ e.getEventMoreDetails());
       endTime.setText("End Time: "+e.getEndTime());
   }

}

e是 Events 类的对象,它只是一个bean.

4

1 回答 1

1

你也许可以简单地setAutoCreateRowSorter(true)。附录:由于Dateimplements Comaprable,请确保您的TableModel返回类型为日期列,如此Date.class所示。另请参阅如何使用表格:排序和过滤

一种更精细的方法是使用树形表,例如Outline此处所示。顶层是日期,叶子是事件。

于 2013-01-24T10:48:58.577 回答