假设我正在使用CustomDataModel和CustomTableListener创建自定义JTable。假设他们光荣地工作。
现在假设在实现中每个JTable都保证其第一行填充了类型安全数据,并且该数据永远不会被删除,只会被修改 - 并且永远不会设置为null。 (使用JComboBoxes作为他们的编辑器,空的String和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是强制转换的类型。
我想的越多,我就越相信我的想法是不可能的。这不是必要的,但它会避免强制转换 - 尽管如果我当前的实现得到“修复”并通过手动转换完成类型检索,我认为这会强制执行强制转换。
现在;在SSCCE中我使用system.out.println来打印 - 它接受一个Object引用并调用它的toString()方法,但是我想要采用的方法或动作我不一定要绑定到Object参数
重点是直接获取存储数据的类型;如果将它存储为Object引用而不将其重新恢复为原始类型,我认为这是不可能的......除非使用泛型?我不知道 - 任何帮助表示赞赏!
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);
}
}
}
答案 0 :(得分:1)
首先,getColumnClass(int)
应返回具有泛型类型信息的Class,否则此函数的静态返回类型将实际为Class<Object>
,因此其cast(Object)
函数将具有返回类型Object。但是,这不会解决您的问题。问题的根源是getValueAt(int,int)
的{{1}}不包含除Object之外的静态类型信息(即其返回类型为Object)。
当您考虑它时,AbstractTableModel
不能返回除Object之外的任何静态类型,因为它适用于表中的每个列,并且每个列可以具有不同的类型。实际上,您可以在运行时添加列,这些列在编译时甚至都没有考虑过。你怎么能静态地确定这些列的类型?
您很可能不得不求助于使用getValueAt(int,int)
运算符和运行时强制转换或重新实现JTable框架的大部分内容来为每列添加静态类型信息(并且很可能无法添加列在运行时)。