java 设置 JTable 中列的数据类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7429122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set The Data Type Of Column in a JTable
提问by Kaushik Balasubramanain
I created a JTable with a table model . Now based on an input which i have, i want to make one column into a particular data Type. How do i do this?
我创建了一个带有表模型的 JTable。现在基于我拥有的输入,我想将一列变成特定的数据类型。我该怎么做呢?
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.AbstractTableModel;
public class MyTableModelTwo extends AbstractTableModel {
private static final long serialVersionUID = 1L;
private Object[][] data;
private String[] columnNames;
public MyTableModelTwo(Object[][] data) {
this.data = data;
}
@Override
public int getRowCount() {
return data.length;
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}
@Override
public Class<?> getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
@Override
public boolean isCellEditable(int rowIndes, int columnIndex) {
return true;
}
@Override
public String getColumnName(int index) {
return columnNames[index];
}
@Override
public void setValueAt(Object value, int rowIndex, int columnIndex) {
data[rowIndex][columnIndex] = value;
fireTableCellUpdated(rowIndex, columnIndex);
}
public MyTableModelTwo(String[] columnNames, Object[][] data) {
this.columnNames = columnNames;
this.data = data;
}
}
class MyTableTwo extends JPanel implements TableModelListener {
private static final long serialVersionUID = 1L;
private JTable table;
private Object[][] data;
private JTextField t;
public MyTableTwo(int noElements, String[] columnNames) {
data = new Object[noElements][columnNames.length];
t = new JTextField();
MyTableModelTwo m = new MyTableModelTwo(columnNames, data);
table = new JTable(m);
table.getModel().addTableModelListener(this);
setLayout(new GridLayout(1, 0));
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
@Override
public void tableChanged(TableModelEvent e) {
}
public JTextField returnT6() {
return t;
}
}
回答by mKorbel
overide public Class getColumnClass(int column) {...
覆盖public Class getColumnClass(int column) {...
please my question, why do you needed there AbstractModel, for why reasons, really what do you want to restict/mofify/change/override, ( you can prety ignore this == be sure that not really not good way to start to playing with anything for JTable based on AbstractTableModel
), however ... consider using DefalutTableModel rather than AbstractTableModel
请我的问题,你为什么需要AbstractModel,出于什么原因,你真的想要限制/修改/更改/覆盖,(you can prety ignore this == be sure that not really not good way to start to playing with anything for JTable based on AbstractTableModel
),但是......考虑使用DefalutTableModel而不是AbstractTableModel
回答by raven
I stand corrected, but I don't believe columns (or cells, rather) in JTable have any notion of datatypes. You're best to check for the expected integer and throw an exception otherwise. Eg:
我的立场是正确的,但我不相信 JTable 中的列(或单元格)有任何数据类型的概念。您最好检查预期的整数,否则抛出异常。例如:
try{
Integer.parseInt(myTableCellValue);
}catch(ParseException e){
//Not a valid integer
}