我为jCombobox
单元格设置DefaultCellEditor
为JTable
。
当我在单元格中键入一个值(jCombobox
)时,问题出现了,每当我点击其他地方时,该值就会丢失。有人知道为什么以及如何解决这个问题?
table.getColumnModel().getColumn(1).setCellEditor(new SpringJobTablePopupCellEditor());
public class SpringJobTablePopupCellEditor extends AbstractCellEditor implements TableCellEditor {
JTextField jtf;
DefaultCellEditor other;
DefaultCellEditor checkbox;
private DefaultCellEditor lastSelected;
JComboBox cbox = null;
public SpringJobTablePopupCellEditor() {
jtf = new JTextField();
jtf.setDocument(new JTextFieldLimit(1000));
other = new DefaultCellEditor(jtf);
checkbox = new DefaultCellEditor(generateBox("10"));
}
@Override
public Object getCellEditorValue() {
return lastSelected.getCellEditorValue();
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
final JTable t = table;
cbox.getEditor().getEditorComponent().addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void focusLost(FocusEvent e) {
if(t.isEditing()){
t.getCellEditor().stopCellEditing();
}
}
});
String val = table.getModel().getValueAt(row, column - 1).toString();
if("ak".equals(val)){
lastSelected = checkbox;
return checkbox.getTableCellEditorComponent(table, value, isSelected, row, column);
}
lastSelected = other;
return other.getTableCellEditorComponent(table, value, isSelected, row, column);
}
private JComboBox generateBox(String type) {
cbox = new JComboBox();
cbox.setEditable(true);
for (Map.Entry<String, String> entry : SpringJob.akMap.entrySet()) {
cbox.addItem(entry.getValue());
}
return cbox;
}
}
答案 0 :(得分:0)
试
JTable table = new yourTable();
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
当你从一个组合框转到另一个组合框时,你需要强制你的桌子停止编辑 我通过将focusListener添加到组合框并添加了focuslost
来实现类似的功能public void focusLost(FocusEvent e) {
if (table.isEditing())
table.getCellEditor().stopCellEditing();
}
}
答案 1 :(得分:0)
组合框通常存储了多个项目列表。当您单击jtable中的JCombo框并键入值时,它将不会直接添加到组合框列表项中。你必须先在组合框中添加值。组合框包含对象数组。
尝试使用此代码在Jtable组合框中动态添加值
static JComboBox combo = new JComboBox();
static JTable table = new JTable();
public static void main(String[] args)
{
JFrame frame = new JFrame();
JPanel topPanel = new JPanel();
JPanel middlepanel = new JPanel();
combo.addItem("First");
combo.addItem("Second");
combo.addItem("Third");
JButton button = new JButton("Add Item");
topPanel.add(button);
frame.setLayout(new BorderLayout());
frame.setSize(500, 500);
frame.add(topPanel, BorderLayout.NORTH);
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addColumn("A", new Object[] { "item1" });
middlepanel.setLayout(new BorderLayout());
middlepanel.add(table,BorderLayout.CENTER);
String str[] = new String[combo.getItemCount()];
for(int i=0;i<combo.getItemCount();i++){
str[i] = combo.getItemAt(i).toString();
}
table.getColumnModel().getColumn(0).setCellEditor(new MyComboBoxEditor(combo));
table.getColumnModel().getColumn(0).setCellRenderer(new MyComboBoxRenderer(str));
table.setRowHeight(25);
frame.add(middlepanel,BorderLayout.CENTER);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
switch(cmd)
{
case "Add Item":
combo.addItem("Fourth");
combo.addItem("Fifth");
String str[] = new String[combo.getItemCount()];
for(int i=0;i<combo.getItemCount();i++){
str[i] = combo.getItemAt(i).toString();
}
table.getColumnModel().getColumn(0).setCellEditor(new MyComboBoxEditor(combo));
table.getColumnModel().getColumn(0).setCellRenderer(new MyComboBoxRenderer(str));
break;
}
}
});
}
@Override
public void actionPerformed(ActionEvent e)
{
}