我如果用快捷键删除一行数据,则在jtable视图里面,显示的是下一条数据被删除了,但在model里面,确实是删除我了我要的数据,但就是显示有问题,如果在jbutton的点击事件里写这段删除代码,一切正常,真是晕了。
class Testing
{
private DefaultTableModel model;
private JTable table; public void buildGUI()
{
JPanel p = new JPanel();
JTextField tf1 = new JTextField("has focus, does F1 work OK?");
JTextField tf2 = new JTextField("see if it works here, as well");
table = new JTable();
model = new DefaultTableModel(new String[][]{
{"1a","1b"}
,{"2a","2b"}
,{"3a","3b"}
},new String[]{"col1","col2"}); table.setModel(model);
p.add(tf1); p.add(tf2);
p.add(table);
JFrame f = new JFrame();
f.getContentPane().add(p);
f.setSize(400,300);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btn=new JButton("delete");
btn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
model.removeRow(table.getSelectedRow());
}
});
p.add(btn);
KeyStroke f1 = KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0);
Action action_F1 = new AbstractAction(){
public void actionPerformed(ActionEvent e){
model.removeRow(table.getSelectedRow());
}
};
f.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(f1,"F1");
f.getRootPane().getActionMap().put("F1", action_F1);
f.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Testing().buildGUI();
}
});
}
}
class Testing
{
private DefaultTableModel model;
private JTable table; public void buildGUI()
{
JPanel p = new JPanel();
JTextField tf1 = new JTextField("has focus, does F1 work OK?");
JTextField tf2 = new JTextField("see if it works here, as well");
table = new JTable();
model = new DefaultTableModel(new String[][]{
{"1a","1b"}
,{"2a","2b"}
,{"3a","3b"}
},new String[]{"col1","col2"}); table.setModel(model);
p.add(tf1); p.add(tf2);
p.add(table);
JFrame f = new JFrame();
f.getContentPane().add(p);
f.setSize(400,300);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btn=new JButton("delete");
btn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
model.removeRow(table.getSelectedRow());
}
});
p.add(btn);
KeyStroke f1 = KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0);
Action action_F1 = new AbstractAction(){
public void actionPerformed(ActionEvent e){
model.removeRow(table.getSelectedRow());
}
};
f.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(f1,"F1");
f.getRootPane().getActionMap().put("F1", action_F1);
f.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Testing().buildGUI();
}
});
}
}
楼主【fxbird】截止到2008-07-20 20:59:31的历史汇总数据(不包括此帖):
发帖的总数量:131 发帖的总分数:4098 每贴平均分数:31
回帖的总数量:1052 得分贴总数量:296 回帖的得分率:28%
结贴的总数量:118 结贴的总分数:3798
无满意结贴数:10 无满意结贴分:380
未结的帖子数:13 未结的总分数:300
结贴的百分比:90.08 % 结分的百分比:92.68 %
无满意结贴率:8.47 % 无满意结分率:10.01 %
值得尊敬
当选中其中一行,点击快捷键F1时,由于JTable可编辑,F1被视为输入,也就是相应输入框获得输入焦点,所以显示错误
点击button时自然不存在这问题。其实也不是严格删除了下一行,而是选择行时鼠标点击的那一列(亦即可以获得输入焦点的列)会被保留在视图中至于解决办法期待牛人!!当然一个笨办法就是将JTable设为不可编辑,但这似乎有点……
if (table.isEditing())
table.getCellEditor().stopCellEditing();
model.removeRow(table.getSelectedRow());这样就行了,为什么认为有输入焦点了,就不会在视图中删除,即使删不掉,也不应该删除下一行啊。什么玩意。