java - TableRowSorter 过滤时 JTable 模型更新不可见

标签 java swing jtable defaulttablemodel tablerowsorter

我有一个颜色编辑 JDialog,用户可以在其中添加/删除/编辑颜色。 颜色显示在从数据库填充的 Jtable 中。 UI 包含一个 txtField,它允许通过 TableRowSorter 进行过滤。 Jtable 使用 defaultTableModel,例如在用户选择删除一行后,我执行 model.removeRow(tableau.getSelectedRow()) 来刷新模型。

我的问题如下:当用户不使用排序器并单击删除时,一切都会正常,并且 JTable 会立即更新以表示此操作,即从 JTable 中删除相应的行,但是当他使用输入的过滤器执行相同的操作(TableRowSorter 执行过滤),Jtable 中不会出现视觉反馈,即删除的行仍然可见。

1)无过滤:我选择带有 FooColor7 的行

enter image description here

2) 然后我点击删除按钮:包含 FooColor7 的行被删除,JTable 会显示这一点。

enter image description here

现在是“错误”行为:

3) 过滤:我选择带有 FooColor3 的行

enter image description here

4)然后我点击了删除按钮:即使从数据库中正确删除了该行,UI 也没有变化...

enter image description here

这里是一种 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

有什么想法吗?

最佳答案

model.removeRow(table.getSelectedRow()); 表.repaint();

  1. 无需重新绘制。当数据更改时,TableModel 会通知表格,表格将重新绘制自己

  2. 由于您正在对数据进行排序,因此您可能会从 TableModel 中删除错误的行。首先需要将 View 索引转换为模型索引:

例如:

int modelIndex = table.convertRowIndexToModel( table.getSelectedIndex() );
model.removeRow( modelIndex );

关于java - TableRowSorter 过滤时 JTable 模型更新不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24601857/

相关文章:

java - 无法发布 Twitter 状态更新,错误为 "no trusted certificate found"

java - Collection.stream().forEach() 和 Collection.forEach() 有什么区别?

java - JComponent - 设置透明背景

java - java中鼠标单击操作事件执行方式错误

java - 测试另一个测试期间生成的 Java 代码

java - 在将我的相对布局更改为 Linearlayout 时,它在 TTS 的 textview 上抛出空指针异常

java - 有没有人用过GUTS(GUICE UTILITIES TOOLS SWING)?

java - 如何只选择 jtable 中一行中的单元格?

java - Hibernate JTable 同步

java - JTable 中列中的颜色单元格