java - JTable 的自定义 DataModel 返回类型安全值

标签 java swing jtable abstracttablemodel typed

假设我正在使用 CustomDataModel 和 CustomTableListener 创建自定义 JTable。假设他们工作出色。

现在假设在实现中,保证每个 JTable 的第一行都填充有类型安全的数据,并且该数据永远不会被删除,只会被修改 - 并且永远不会设置为 null。(使用 JComboBoxes 作为编辑器,空字符串和呈现为空字符串的 0 是唯一的“空”选择)

现在;我想编写一个使用 getColumnClass 返回类型数据的方法。

根据我所读到的内容,我必须结合使用以下方法:

class CustomDataModel extends AbstractTableModel {
...
//implement data struc and other methods (full testable code further down below in the SSCCE)
...

/**
 * Guaranteed to return a class based on this table's construction
 * @param c
 * @return 
 */
@Override
public Class getColumnClass(int c){
    return getValueAt(0,c).getClass();
}

...

public <T> T returnType(int row, int column) {
    //the following will not compile - and I'm stuck, don't know how to
    //use getColumnClass to return type-specific data
    return getColumnClass(column).cast(getValueAt(row,column));
}
}

NetBeans 告诉我强制转换调用返回 Object,但我确信cast(Object obj) 返回 T,其中 T 是强制转换中 this 的类型。

我越想越觉得我想要的东西是不可能的。这并不是真正必要的,但它会避免强制转换 - 尽管我认为如果我当前的实现“固定”并通过手动强制转换完成类型检索,这将强制执行强制转换。

现在;在 SSCCE 中,我使用 system.out.println 进行打印 - 它接受一个对象引用并调用其 toString() 方法,但我想要采取的方法或操作不一定希望绑定(bind)到对象参数。

重点是直接获取存储的数据的类型;如果它存储为对象引用而不将其转换回其原始类型,我想这实际上是不可能的......除非使用泛型?我不知道 - 任何帮助表示赞赏!

SSCCE

//package utility;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

/**
 * @author Sean
 */
public class UnitTesting extends JPanel{

    public UnitTesting() {
        super(new GridLayout(1,0));

        JTable table = createAJTable();
        table.getTableHeader().setReorderingAllowed(false);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Normally here JComboBoxes are set as the Editors - SSCCE will use dummy test data

        //Add the scroll pane to this panel.
        add(scrollPane);

        //Test the data Types - I want to retrieve type-specific data to avoid casting
        CustomModel dataModel = (CustomModel)table.getModel();
        Object val;
        for(int row = 0; row < dataModel.getRowCount(); row++){
            //Row possibility
            doSomeThings(row, dataModel);
            for(int col = 0; col < dataModel.getColumnCount(); col++){
                //Here's the old way of going about this. (also could invoke getColumnClass())
                val = dataModel.getValueAt(row, col);
                System.out.println(val + " : " + val.getClass());
                //Overloaded possibility - needs typed data
//                doSomeAction(dataModel.typedValueAt(row, col));
            }
        }
    }

    private JTable createAJTable() {
        return new JTable(new CustomModel(new String[]{"Col1", "Col2", "Col3", "Col4", "Col5"}));
    }

    private void doSomeAction(String str){
        //Do things with strings
    }

    private void doSomeAction(int number){
        //Do things with integers
    }

    private void doSomeThings(int row, CustomModel dataModel) {
        String col1Data, col2Data, col5Data;
        int col3Num, col4Num;
        //Retrieve data and store it in typed local variable (need casting or typed retrieval)
        //Do things with the Strings and Integers together
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("TableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        UnitTesting newContentPane = new UnitTesting();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        /*
         * Set Look and feel here; taken out for SSCCE
         */
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }

    class CustomModel extends AbstractTableModel {
        //Dummy data for the SSCCE
        private List<String> columnHeadings;
        private List<List<Object>> data;

        CustomModel(String[] cols){
            data = new ArrayList<>();
            columnHeadings = Arrays.asList(cols);

            ArrayList<Object> testRow = new ArrayList<>();
            testRow.add("String");
            testRow.add("Str");
            testRow.add(0);
            testRow.add(5);
            testRow.add("Strong");

            data.add(testRow);
        }

        //This is the method I want to create
//        public <T> T typedValueAt(int row, int column) {
//            return getColumnClass(column).cast(getValueAt(row,column));
//        }

        @Override
        public int getRowCount() {
            return data.size();
        }

        @Override
        public int getColumnCount() {
            return columnHeadings.size();
        }

        @Override
        public String getColumnName(int columnIndex) {
            return columnHeadings.get(columnIndex);
        }

        @Override
        public Class<?> getColumnClass(int c) {
            return getValueAt(0,c).getClass();
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            //For the SSCCE we'll just test with one row of uneditable data
            return false;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return data.get(rowIndex).get(columnIndex);
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            //Normally check for row existence and populate necessary rows - but for SSCCE just this will suffice
            data.get(rowIndex).set(columnIndex, aValue);
        }
    }
}

最佳答案

首先,getColumnClass(int)应该返回一个带有泛型类型信息的类,否则该函数的静态返回类型实际上将是 Class<Object>因此它是 cast(Object)函数将具有返回类型 Object。但是,这并不能解决你的问题。问题的根源在于getValueAt(int,int)AbstractTableModel除 Object 之外不携带任何静态类型信息(即其返回类型为 Object)。

仔细想想,getValueAt(int,int)无法返回除 Object 之外的任何静态类型,因为它适用于表中的每一列,并且每一列都可以具有不同的类型。事实上,您可以在运行时添加在编译时甚至没有想到的列。如何静态确定这些列的类型?

您很可能不得不求助于使用 instanceof运算符和运行时强制转换或重新实现 JTable 框架的大部分,以便为每列添加静态类型信息(并且很可能失去在运行时添加列的能力)。

关于java - JTable 的自定义 DataModel 返回类型安全值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20101766/

相关文章:

java,游戏板创建不需要的新行

java Lanterna无法从终端读取输入

java - 为链接列表创建 getLast 方法

java - 刷新 JTable 条目

java - 在进行任何绘图之前调用 setVisible 属性

java - 使用 Java Swing 在 JTable 上获取字符串值而不是复选框?

java - 何时使用以及如何编写循环和半代码

java - 当调用新的 JPanel 时,单击 JButton 时随机生成数字

java - 带轴 JTable

java - 如何使用正则表达式验证 JTable 的第一列?