Swing:从TableModel捕获异常 [英] Swing: catching exceptions from TableModel

查看:115
本文介绍了Swing:从TableModel捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户输入无效值,我有一个TableModel可能会在其 setValueAt 方法中抛出异常:

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

问题是:这个异常?它可能会显示一个弹出消息,或更新状态栏,或者将该单元格绘制为红色。无论我选择做什么,我不认为 TableModel 应该这样做。

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.

推荐答案

大概你正在使用JTable编辑单元格,以便您可以使用自定义编辑器:

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

这篇关于Swing:从TableModel捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆