Swing: catching exceptions from TableModel

2019-01-29 07:54发布

问题:

I have a TableModel that may throw an exception on its setValueAt method if user enters an invalid value:

public class MyTableModel extends AbstractTableModel {
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
        String valueStr = (String) value;
        // some basic failure state
        if(valueStr.length()>5) {
            throw new ValidationException("Value should have up to 5 characters");
        }
        this.currentValue = valueStr;
    }
}

Question is: how can another class catch this exception? It may show a popup message, or update a status bar, or paint the cell red. Whatever I chose to do, I don't think the TableModel should be doing that.

回答1:

Presumably you are using a JTable to edit the cell so you can use a custom editor:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;

public class TableFiveCharacterEditor extends DefaultCellEditor
{
    private long lastTime = System.currentTimeMillis();

    public TableFiveCharacterEditor()
    {
        super( new JTextField() );
    }

    public boolean stopCellEditing()
    {
        JTable table = (JTable)getComponent().getParent();

        try
        {
            String editingValue = (String)getCellEditorValue();

            if(editingValue.length() != 5)
            {
                JTextField textField = (JTextField)getComponent();
                textField.setBorder(new LineBorder(Color.red));
                textField.selectAll();
                textField.requestFocusInWindow();

                JOptionPane.showMessageDialog(
                    table,
                    "Please enter string with 5 letters.",
                    "Alert!",JOptionPane.ERROR_MESSAGE);
                return false;
            }
        }
        catch(ClassCastException exception)
        {
            return false;
        }

        return super.stopCellEditing();
    }

    public Component getTableCellEditorComponent(
        JTable table, Object value, boolean isSelected, int row, int column)
    {
        Component c = super.getTableCellEditorComponent(
            table, value, isSelected, row, column);
        ((JComponent)c).setBorder(new LineBorder(Color.black));

        return c;
    }

    private static void createAndShowUI()
    {
        JTable table = new JTable(5, 5);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);

        //  Use a custom editor

        TableCellEditor fce = new TableFiveCharacterEditor();
        table.setDefaultEditor(Object.class, fce);

        JFrame frame = new JFrame("Table Five Character Editor");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( scrollPane );
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}


回答2:

First alternative is to use a Thread.setDefaultUncaughtExceptionHandler:

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    public void uncaughtException(Thread t, Throwable e) {
        if(e instanceof ValidationException) {
            JOptionPane.showConfirmDialog(someframe, e.getMessage());
        }
    }
});

but I am not sure if there's a more proper way to handle this situation, like some sort of exception listener.