下面列出了javax.swing.table.TableModel#getColumnClass ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Creates a panel that contains a table based on the specified table model.
*
* @param model the table model to use when constructing the table.
*
* @return The panel.
*/
public static JPanel createTablePanel(final TableModel model) {
final JPanel panel = new JPanel(new BorderLayout());
final JTable table = new JTable(model);
for (int columnIndex = 0; columnIndex < model.getColumnCount(); columnIndex++) {
final TableColumn column = table.getColumnModel().getColumn(columnIndex);
final Class c = model.getColumnClass(columnIndex);
if (c.equals(Number.class)) {
column.setCellRenderer(new NumberCellRenderer());
}
}
panel.add(new JScrollPane(table));
return panel;
}
/**
* Creates a panel that contains a table based on the specified table model.
*
* @param model the table model to use when constructing the table.
*
* @return The panel.
*/
public static JPanel createTablePanel(TableModel model) {
JPanel panel = new JPanel(new BorderLayout());
JTable table = new JTable(model);
for (int columnIndex = 0; columnIndex < model.getColumnCount();
columnIndex++) {
TableColumn column = table.getColumnModel().getColumn(columnIndex);
Class c = model.getColumnClass(columnIndex);
if (c.equals(Number.class)) {
column.setCellRenderer(new NumberCellRenderer());
}
}
panel.add(new JScrollPane(table));
return panel;
}
/**
* Creates a panel that contains a table based on the specified table model.
*
* @param model the table model to use when constructing the table.
*
* @return The panel.
*/
public static JPanel createTablePanel(TableModel model) {
JPanel panel = new JPanel(new BorderLayout());
JTable table = new JTable(model);
for (int columnIndex = 0; columnIndex < model.getColumnCount();
columnIndex++) {
TableColumn column = table.getColumnModel().getColumn(columnIndex);
Class c = model.getColumnClass(columnIndex);
if (c.equals(Number.class)) {
column.setCellRenderer(new NumberCellRenderer());
}
}
panel.add(new JScrollPane(table));
return panel;
}
protected JComponent createDefaultTable(final TableModel data)
{
final JTable table = new JTable(data);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
for (int columnIndex = 0; columnIndex < data.getColumnCount(); columnIndex++)
{
final TableColumn column = table.getColumnModel().getColumn(columnIndex);
column.setMinWidth(50);
final Class c = data.getColumnClass(columnIndex);
if (c.equals(Number.class))
{
column.setCellRenderer(new NumberCellRenderer());
}
}
return new JScrollPane
(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
}
protected JComponent createDefaultTable(final TableModel data)
{
final JTable table = new JTable(data);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
for (int columnIndex = 0; columnIndex < data
.getColumnCount(); columnIndex++)
{
final TableColumn column = table.getColumnModel().getColumn(columnIndex);
column.setMinWidth(50);
final Class c = data.getColumnClass(columnIndex);
if (c.equals(Number.class))
{
column.setCellRenderer(new NumberCellRenderer());
}
}
return new JScrollPane
(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
}
public static void printTableModel( final TableModel mod, final PrintStream out ) {
out.println( "Tablemodel contains " + mod.getRowCount() + " rows." ); //$NON-NLS-1$ //$NON-NLS-2$
for ( int i = 0; i < mod.getColumnCount(); i++ ) {
out.println( "Column: " + i + " Name=" + mod.getColumnName( i ) + "; DataType=" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ mod.getColumnClass( i ) );
}
out.println( "Checking the data inside" ); //$NON-NLS-1$
for ( int rows = 0; rows < mod.getRowCount(); rows++ ) {
for ( int i = 0; i < mod.getColumnCount(); i++ ) {
final Object value = mod.getValueAt( rows, i );
final Class<?> c = mod.getColumnClass( i );
if ( value == null ) {
out.println( "ValueAt (" + rows + ", " + i + ") is null" ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
} else {
if ( c.isAssignableFrom( value.getClass() ) == false ) {
out.println( "ValueAt (" + rows + ", " + i + ") is not assignable from " + c ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
} else if ( c.equals( Object.class ) ) {
out.println( "ValueAt (" + rows + ", " + i + ") is in a generic column and is of "
//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ "type " + value.getClass() ); //$NON-NLS-1$
} else {
out.println( "ValueAt (" + rows + ", " + i + ") is in a typed column and is of "
//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ "type " + value.getClass() ); //$NON-NLS-1$
}
}
}
}
}
protected void initDefaultMetaData( final TableModel model ) {
for ( int i = 0; i < model.getColumnCount(); i++ ) {
final String columnName = model.getColumnName( i );
final Class columnType = model.getColumnClass( i );
final DefaultDataAttributes attributes = new DefaultDataAttributes();
attributes.setMetaAttribute( MetaAttributeNames.Core.NAMESPACE, MetaAttributeNames.Core.NAME,
DefaultConceptQueryMapper.INSTANCE, columnName );
attributes.setMetaAttribute( MetaAttributeNames.Core.NAMESPACE, MetaAttributeNames.Core.TYPE,
DefaultConceptQueryMapper.INSTANCE, columnType );
columnAttributes.add( attributes );
}
tableAttributes = EmptyDataAttributes.INSTANCE;
}
public static boolean isSafeToCache( final TableModel model ) {
final int columnCount = model.getColumnCount();
for ( int i = 0; i < columnCount; i += 1 ) {
Class columnClass = model.getColumnClass( i );
while ( columnClass.isArray() ) {
columnClass = columnClass.getComponentType();
}
if ( String.class.equals( columnClass ) ) {
continue;
}
if ( Number.class.isAssignableFrom( columnClass ) ) {
continue;
}
if ( Date.class.isAssignableFrom( columnClass ) ) {
continue;
}
if ( Boolean.class.equals( columnClass ) ) {
continue;
}
if ( columnClass.isPrimitive() ) {
continue;
}
if ( Paint.class.isAssignableFrom( columnClass ) ) {
continue;
}
if ( Shape.class.isAssignableFrom( columnClass ) ) {
continue;
}
if ( Stroke.class.isAssignableFrom( columnClass ) ) {
continue;
}
if ( columnClass.isEnum() ) {
continue;
}
return false;
}
return true;
}
public OfflineTableModel( final TableModel model, final DataAttributeContext dataAttributeContext ) {
columnCount = model.getColumnCount();
columnTypes = new Class[columnCount];
columnNames = new String[columnCount];
columnAttributes = new DefaultDataAttributes[columnCount];
values = new Object[columnCount];
tableAttributes = new DefaultDataAttributes();
for ( int i = 0; i < columnCount; i++ ) {
columnTypes[i] = model.getColumnClass( i );
columnNames[i] = model.getColumnName( i );
columnAttributes[i] = new DefaultDataAttributes();
}
if ( model instanceof MetaTableModel ) {
final MetaTableModel metaTableModel = (MetaTableModel) model;
tableAttributes.merge( metaTableModel.getTableAttributes(), dataAttributeContext );
for ( int i = 0; i < columnCount; i++ ) {
columnAttributes[i].merge( metaTableModel.getColumnAttributes( i ), dataAttributeContext );
}
}
if ( model.getRowCount() > 0 ) {
for ( int i = 0; i < columnCount; i++ ) {
values[i] = model.getValueAt( 0, i );
}
}
}