gpt4 book ai didi

java - JTable 显示某一日期下的事件列表

转载 作者:行者123 更新时间:2023-12-01 14:53:53 24 4
gpt4 key购买 nike

我想发布一个关于我特别想做的事情的问题,但不知道如何解决这个问题,因为我是 JTables 的新手。这是我的atm: This is what I have atm

但我想这样安排表格:

enter image description here

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

这里是有问题的相关类别,我不确定在哪里以及如何进行更改才能实现上述目标。

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

最佳答案

您也许可以简单地setAutoCreateRowSorter(true)。附录:由于 Date 实现了 Comaprable,因此请确保您的 TableModel 返回日期列的 Date.class 类型,如下所示显示here 。另请参阅How to Use Tables: Sorting and Filtering .

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

关于java - JTable 显示某一日期下的事件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14499356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com