swing表格 - swing table java




Swing:從TableModel捕獲異常 (2)

我有一個TableModel,如果用戶輸入一個無效的值可能會拋出setValueAt方法的異常:

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;
    }
}

問題是: 另一個類怎麼能捕獲這個異常呢? 它可能會顯示一個彈出消息,或者更新狀態欄,或者將單元格塗成紅色。 無論我選擇做什麼,我都不認為TableModel應該這樣做。


想必您使用JTable編輯單元格,以便您可以使用自定義編輯器:

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();
            }
        });
    }
}

第一種方法是使用Thread.setDefaultUncaughtExceptionHandler

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

但我不確定是否有更合適的方法來處理這種情況,比如某種異常監聽器。







tablemodel