我想在这里做三件事:
首先,我试图使组合框在您启动该程序时显示“ not feed”,在启动该程序时它什么也没有显示,仅当我单击组合框时,它显示选项“ feed”和“ not饲料”。
其次,我正在尝试对组合框进行验证,当我单击JButton
时,它将继续验证组合框是否全部为“提要”(如果您要继续的话),否则将弹出一个弹出窗口,提示“再次检查” ”
最后,我想使前4个col上的单元格不可编辑,而最后一列可编辑。
public class DosageTableHelper {
private static JTable toDoTable;
private static JScrollPane jpane;
private static int counter=1;
public static DefaultTableModel getElderlyFromQueryDos(String timing,String position) throws SQLException {
SQLObject so = new SQLObject();
ResultSet rs = null;
if(timing.equalsIgnoreCase("Morning")){
PreparedStatement stmt = so.getPreparedStatementWithKey("SELECT morningdosage FROM et_elderly WHERE name = ?");
stmt.setString(1,position);
stmt.executeQuery();
System.out.println(stmt);
rs = stmt.getResultSet();
}
else if(timing.equalsIgnoreCase("Afternoon")){
PreparedStatement stmt = so.getPreparedStatementWithKey("SELECT afternoondosage FROM et_elderly WHERE name = ?");
stmt.setString(1,position);
stmt.executeQuery();
System.out.println(stmt);
rs = stmt.getResultSet();
}
else if(timing.equalsIgnoreCase("Noon")){
PreparedStatement stmt = so.getPreparedStatementWithKey("SELECT noondosage FROM et_elderly WHERE name = ?");
stmt.setString(1,position);
stmt.executeQuery();
System.out.println(stmt);
rs = stmt.getResultSet();
}
return (DefaultTableModel) buildTableModel(rs);
}
@SuppressWarnings("unchecked")
public static DefaultTableModel buildTableModel(ResultSet rs) throws SQLException {
ArrayList<DosageObject> DosageList=null;
System.out.println(rs);
try {
while(rs.next()){
ByteArrayInputStream in = new ByteArrayInputStream(rs.getBytes(1));
ObjectInputStream is = new ObjectInputStream(in);
Object retrieveDosBlob =(Object) is.readObject();
if(retrieveDosBlob instanceof ArrayList<?>){
DosageList=((ArrayList<DosageObject>) retrieveDosBlob);
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// storing array list in an array list for future uses
Vector<String> columnNames = new Vector<String>();
columnNames.add("Description");
columnNames.add("Prescription");
columnNames.add("Medication Type");
columnNames.add("Dosage");
columnNames.add("Checked");
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
for(int k=0;k<DosageList.size();k++){
Vector<Object> vector = new Vector<Object>();
vector.add(DosageList.get(k).getMedDescrip());
vector.add(DosageList.get(k).getMedPrescrip());
vector.add(DosageList.get(k).getMedType());
vector.add(DosageList.get(k).getMedDosage());
data.add(vector);
}
DefaultTableModel dtm = new DefaultTableModel(data, columnNames) {
private static final long serialVersionUID = 4234183862785566645L;
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return !( rowIndex == 1 && columnIndex == 1 );
}
};
return dtm;
}
// Debug-able main method
public static void main(String[] args) throws SQLException {
ArrayList<String> nameList= new ArrayList<String>();
nameList.add("Lee Ching Chong");
nameList.add("Lim Kuay Siak");
nameList.add("Lee Ching Chong");
toDoTable =new JTable(getElderlyFromQueryDos("morning",nameList.get(0)));
String[] values = new String[] { "Not Feed", "Feed" };
TableColumn col = toDoTable.getColumnModel().getColumn(4);
col.setCellEditor(new MyComboBoxEditor(values));
col.setCellRenderer(new MyComboBoxRenderer(values));
jpane = new JScrollPane(toDoTable);
JPanel panel = new JPanel();
JFrame frame = new JFrame();
frame.setBounds(0, 0, 700, 543);
panel.add(jpane);
frame.getContentPane().add(new JScrollPane(panel));
JButton btnNext = new JButton("Next");
panel.add(btnNext);
frame.setVisible(true);
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
toDoTable.setModel(DosageTableHelper.getElderlyFromQueryDos("morning",nameList.get(counter)));
String[] values = new String[] { "Not Feed", "Feed" };
TableColumn col = toDoTable.getColumnModel().getColumn(4);
col.setCellEditor(new MyComboBoxEditor(values));
col.setCellRenderer(new MyComboBoxRenderer(values));
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
counter++;
}
});
}
}
@SuppressWarnings("rawtypes")
class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
private static final long serialVersionUID = 1319299961084034009L;
@SuppressWarnings("unchecked")
public MyComboBoxRenderer(String[] items) {
super(items);
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
setSelectedItem(value);
return this;
}
}
class MyComboBoxEditor extends DefaultCellEditor {
private static final long serialVersionUID = -1702063500403826596L;
@SuppressWarnings({ "rawtypes", "unchecked" })
public MyComboBoxEditor(String[] items) {
super(new JComboBox(items));
}
}
样本输出:
最佳答案
阅读Oracle教程-How to use tables - part about to use JComboBox as TableCellEditor,
说明JTable
具有两个独立的功能,分别用于编辑存储在TableCellEditor
中的值和XxxTableModel
用于绘制存储在TableCellRenderer
中的值的功能,
编辑完您(事件,通过API实现的通知程序)后,只需使用XxxTableModel
将选定的值从TableCellEditor
(JComboBox
)存储到XxxTableModel
,以存储XxxTableModel.setValueAt
或"feed"
,而不是"not feed"
作为JComboBox
,Object
只是从模型绘制到视图
默认情况下,所有内容都可以通过使用TableCellRenderer
自动运行,然后需要覆盖减少数量的方法(在您的情况下,DefaultTableModel
,getColumnClass
可能也是isCellEditeble
)
有两种方法(第一种方法需要覆盖setValueAt
)XxxTableModel.setValueAt
将被禁用(以编程方式),直到所有值都从空白更改为JButton
或"feed"
,这需要在模型内部循环,注意执行代码行"not feed"
之后,将来自编辑器的值存储到模型
来自super.setValueAt()
的操作在模型内部循环(原始,最简单但最有效的验证,简单直接的代码,JButton
内没有错误)
要求覆盖XxxTableModel.setValueAt
并设置,更改XxxTableModel.setValueAt
以更正值,这将需要为isCellEditable(row, column)
创建单独的数组,仅用于模型事件,请勿从外部更改此数组,只需使用方法可以通知
如果可能的话,使用isCellEditable
(以避免覆盖XxxTableModel.setValueAt
,方法,侦听器,尤其是模型通知程序-DefaultTableModel
所需的代码中的任何错误解释)
模型内部的代码排序很重要,首先是将值存储到模型中,然后调用任何更改,验证,其他代码,仅调用基于模型中存储的值的代码
我确定每三点都在这里,包括SSCCE / MCVE表格中的工作代码示例