📄 remindtable.java
字号:
package com.liming.remind.ui;
import java.util.List;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import com.liming.remind.dao.RemindDAO;
import com.liming.remind.dto.Remind;
/**
* Function : Remind Table to show Reminds
*
* @author Liming
* @time Dec 18, 2008 8:16:42 PM
* @version 1.0
*/
public class RemindTable extends JTable {
/**
* Field : serialVersionUID
*/
private static final long serialVersionUID = 6491299848652715676L;
// Reminds List
private List<Remind> remindsList;
// The table header
private String tableColumns[] = new String[]{ "ID", "RemindTime",
"RemindContent" };
// Table model
private MyModel model;
/**
* @param remindDAO
* @throws MyNullPointerException
*/
public RemindTable(RemindDAO remindDAO) {
// Get Reminds List
remindsList = remindDAO.getRemindsList();
// Get the instance of MyModel
model = new MyModel();
// And Set To Table
setModel(model);
// Set table header
TableColumn column = getColumn(tableColumns[0]);
column.setPreferredWidth(20);
// Set selection mode to SINGLE_SELECTION
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// Set row height
setRowHeight(25);
}
/**
* Function : Get MyTable's Model
*/
public MyModel getModel() {
return model;
}
/**
* Function : Get selected Remind
*
* @return
*/
public Remind getSelectedRemind() {
int row = this.getSelectedRow();
if (row < 0 || row >= remindsList.size()) {
return null;
}
return remindsList.get(row);
}
/**
* Function : Public class to operate Table Model
*
* @author Liming
* @time Dec 18, 2008 8:36:51 PM
* @version 1.0
*/
public class MyModel extends AbstractTableModel {
/**
* Field : serialVersionUID
*/
private static final long serialVersionUID = 122780433843109586L;
public int getColumnCount() {
return tableColumns.length;
}
public int getRowCount() {
int remindsListSize = remindsList.size();
if (remindsListSize < 3) {
return 3;
}
return remindsList.size();
}
public Object getValueAt(int rowIndex, int columnIndex) {
Remind remind = null;
try {
remind = remindsList.get(rowIndex);
} catch (IndexOutOfBoundsException e) {
// There is needed to catch the exception
// e.printStackTrace();
}
if (remind == null) {
return null;
}
switch (columnIndex) {
case 0:
return remind.getId();
case 1:
return remind.getTime();
case 2:
return remind.getTips();
}
return null;
}
public String getColumnName(int columnIndex) {
return tableColumns[columnIndex];
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -