我有一个颜色编辑JDialog,用户可以在其中添加/删除/编辑颜色。 颜色显示在Jtable中,该数据库由DB填充。 UI包含一个txtField,它允许通过TableRowSorter进行过滤。 Jtable使用defaultTableModel,在用户选择删除行之后,例如我做了一个model.removeRow(tableau.getSelectedRow())来刷新模型。
我的问题如下:当用户没有使用分拣机并点击删除时,一切正常,JTable立即更新以表示此操作,即相应的行从JTable中删除,但是当他输入过滤器做同样的事情(TableRowSorter进行过滤)时,Jtable中的视觉反馈不会发生,即删除的行仍然可见。
1)没有过滤:我用FooColor7选择行
2)然后我点击了删除按钮:删除了FooColor7的行,JTable显示了该行。
现在"越野车"行为:
3)过滤:我选择FooColor3行
4)然后我点击删除按钮:即使从数据库中正确删除了行,UI也没有变化......
这里有一种SSCCE(有点因为没有依赖的工作而且工作时间太长了):
public class DialogEditColors extends JDialog implements ActionListener, KeyListener
{
final TableRowSorter<TableModel> sorter;
private JTable table = null;
private DefaultTableModel model = null;
private JPanel panelBoutons = null;
private JTextField txtFieldSearch;
private JLabel lblTitle;
private JButton btnAdd , btnDelete, btnEdit;
private JButton btnCancel, btnSave;
private JScrollPane scroller;
private ColorDao colorDao= new ColorDao(Share.connection);
public DialogEditColors()
{
super();
setSize(439, 313);
setTitle(" Edition couleurs");
getContentPane().setLayout(null);
setModal(true);
setResizable(false);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
btnSave = new JButton("Enregistrer");
btnSave.setBounds(317, 247, 89, 26);
btnSave.addActionListener(this);
btnCancel = new JButton("Annuler");
btnCancel.setBounds(214, 247, 89, 26);
btnCancel.addActionListener(this);
btnAdd = new JButton("");
btnAdd.setBounds(new Rectangle(10, 8, 33, 26));
btnAdd.addActionListener(this);
btnDelete = new JButton("");
btnDelete.setBounds(new Rectangle(53, 8, 33, 26));
btnDelete.addActionListener(this);
btnEdit = new JButton("");
btnEdit.setBounds(new Rectangle(96, 8, 33, 26));
btnEdit.addActionListener(this);
panelBoutons = new JPanel();
panelBoutons.setBorder(new LineBorder(Color.GRAY));
panelBoutons.setBounds(27, 11, 378, 43);
panelBoutons.setLayout(null);
txtFieldSearch = new JTextField("", 10);
txtFieldSearch.setBounds(193, 9, 175, 24);
panelBoutons.add(txtFieldSearch);
txtFieldSearch.addKeyListener(this);
Object[][] data = new Object[colorDao.findAll().size()][2];
String[] title = { "Couleur", "Description" };
int i = 0;
for (Couleur coul : colorDao.findAll())
{
data[i][0] = coul.getNom();
data[i][1] = coul.getDescription();
i++;
}
model = new DefaultTableModel(data, title);
table = new JTable(model)
{//THIS ALLOWS TO ALTERNATE THE BACKGROUND COLOR OF ROWS
public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
{
Component returnComp = super.prepareRenderer(renderer, row, column);
Color alternateColor = new Color(242, 242, 242);
Color whiteColor = Color.WHITE;
if ( !returnComp.getBackground().equals(getSelectionBackground()) )
{
Color bg = (row % 2 == 0 ? alternateColor : whiteColor);
returnComp.setBackground(bg);
bg = null;
}
return returnComp;
};
};
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setCellSelectionEnabled(true);
table.setColumnSelectionAllowed(true);
sorter = new TableRowSorter<TableModel>(model);
table.setRowSorter(sorter);
scroller = new JScrollPane(table);
scroller.setBounds(27, 65, 378, 171);
addComponents();
setVisible(true);
}//END OF CONSTRUCTOR
private void addComponents()
{
getContentPane().add(scroller);
getContentPane().add(btnSave);
getContentPane().add(btnCancel);
panelBoutons.add(btnAdd);
panelBoutons.add(btnDelete);
panelBoutons.add(btnEdit);
getContentPane().add(panelBoutons);
}////END OF METHOD
public void actionPerformed(ActionEvent e)
{
if ( e.getSource() == btnDelete )
{
if ( table.getSelectedRowCount() != 0 )
{//THERE IS A CHOSEN ROW IN JTABLE
String selectedColor = (table.getValueAt(table.getSelectedRow(), table.getSelectedColumn())).toString();
//TEST THE READONLY ATTRIBUTE OF THE COLOR
if ( colorDao.findByNom(selectedColor).get(0).getReadOnly() == true )
{//THE COLOR'S READONLY ATTRIBUTE IS TRUE
JOptionPane.showMessageDialog(this, "This item is readOnly: impossible to delete it!");
}
else
{//THE COLOR'S READONLY ATTRIBUTE IS FALSE
int response = JOptionPane.showConfirmDialog(this, "Are you sure you
want to delete this color?", "Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if ( response == 0 ) //CONFIRMED
{//FIRST DELETE THE COLOR FROM DB
colorDao.delete(colorDao.findByNom(selectedColor).get(0));
//THEN REFRESH JTABLE TO TAKE DELETION INTO ACCOUNT
model.removeRow(table.getSelectedRow());
table.repaint();
}
}
}
else
{//NO CHOSEN COLOR
JOptionPane.showMessageDialog(this, "No chosen color!");
}
}
else if ( e.getSource() == btnCancel )
{
this.dispose();
}
}//END OF METHOD
public void keyReleased(KeyEvent e)
{
if ( e.getSource() == txtFieldSearch )
{
String text = txtFieldSearch.getText();
if ( text.length() == 0 )
{
sorter.setRowFilter(null);
}
else
{
sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
}
((AbstractTableModel) table.getModel()).fireTableDataChanged();
}
}//END OF METHOD
}////END OF CLASS
有什么想法吗?
答案 0 :(得分:2)
model.removeRow(table.getSelectedRow()); table.repaint();
不需要重画。 TableModel将在数据更改时通知表,表将自行重新绘制
由于您正在对数据进行排序,因此可能会从TableModel中删除错误的行。首先需要将视图索引转换为模型索引:
例如:
int modelIndex = table.convertRowIndexToModel( table.getSelectedIndex() );
model.removeRow( modelIndex );