JTable Input Verifier

前端 未结 3 555
一整个雨季
一整个雨季 2021-02-09 18:18

I am trying to create a simple Input Verifier for a JTable. I ended up with overriding the method: editingStopped(). The problem is that the event does not include informations

3条回答
  •  旧时难觅i
    2021-02-09 18:34

    hmm, there might be a simpler solution to this. Please try this, it worked for me. The key is to remember last selected item, and then perform validation on the current item. If input is wrong, you can roll back to the last selected item, and notify user about that. Roll back is performed using EventQueue.invokeLater(...), therefore avoiding recursive call to the listeners.

    private final DefaultTableModel dtm = new DefaultTableModel();
    private final JTable table = new JTable(dtm);
    private final Object[] lastItem;
    private final AtomicInteger lastIndex = new AtomicInteger(-1);
    private final ItemValidator validator = new ItemValidator();
    
    
    public YourConstructor() {
    
        lastItem = new Object[table.getColumnCount()];
    
    
        //store last value of selected table item in an array.
        table.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent evt){
                lastIndex.set(table.getSelectedRow());
                int row = lastIndex.get();
                for(int i=0;i1 && i

提交回复
热议问题