下面列出了javax.swing.table.DefaultTableModel#getColumnCount ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public TableColumn addColumn(Object columnIdentifier, int width,
TableCellRenderer renderer,
TableCellEditor editor, List columnData) {
checkDefaultTableModel();
// Set up the model side first
DefaultTableModel m = (DefaultTableModel)getModel();
m.addColumn(columnIdentifier, columnData.toArray());
// The column will have been added to the end, so the index of the
// column in the model is the last element.
TableColumn newColumn = new TableColumn(
m.getColumnCount()-1, width, renderer, editor);
super.addColumn(newColumn);
return newColumn;
}
public TableColumn addColumn(Object columnIdentifier, int width,
TableCellRenderer renderer,
TableCellEditor editor, List columnData) {
checkDefaultTableModel();
// Set up the model side first
DefaultTableModel m = (DefaultTableModel)getModel();
m.addColumn(columnIdentifier, columnData.toArray());
// The column will have been added to the end, so the index of the
// column in the model is the last element.
TableColumn newColumn = new TableColumn(
m.getColumnCount()-1, width, renderer, editor);
super.addColumn(newColumn);
return newColumn;
}
public TableColumn addColumn(Object columnIdentifier, int width,
TableCellRenderer renderer,
TableCellEditor editor, List columnData) {
checkDefaultTableModel();
// Set up the model side first
DefaultTableModel m = (DefaultTableModel)getModel();
m.addColumn(columnIdentifier, columnData.toArray());
// The column will have been added to the end, so the index of the
// column in the model is the last element.
TableColumn newColumn = new TableColumn(
m.getColumnCount()-1, width, renderer, editor);
super.addColumn(newColumn);
return newColumn;
}
public TableColumn addColumn(Object columnIdentifier, int width,
TableCellRenderer renderer,
TableCellEditor editor, List columnData) {
checkDefaultTableModel();
// Set up the model side first
DefaultTableModel m = (DefaultTableModel)getModel();
m.addColumn(columnIdentifier, columnData.toArray());
// The column will have been added to the end, so the index of the
// column in the model is the last element.
TableColumn newColumn = new TableColumn(
m.getColumnCount()-1, width, renderer, editor);
super.addColumn(newColumn);
return newColumn;
}
public TableColumn addColumn(Object columnIdentifier, int width,
TableCellRenderer renderer,
TableCellEditor editor, List columnData) {
checkDefaultTableModel();
// Set up the model side first
DefaultTableModel m = (DefaultTableModel)getModel();
m.addColumn(columnIdentifier, columnData.toArray());
// The column will have been added to the end, so the index of the
// column in the model is the last element.
TableColumn newColumn = new TableColumn(
m.getColumnCount()-1, width, renderer, editor);
super.addColumn(newColumn);
return newColumn;
}
public TableColumn addColumn(Object columnIdentifier, int width,
TableCellRenderer renderer,
TableCellEditor editor, List columnData) {
checkDefaultTableModel();
// Set up the model side first
DefaultTableModel m = (DefaultTableModel)getModel();
m.addColumn(columnIdentifier, columnData.toArray());
// The column will have been added to the end, so the index of the
// column in the model is the last element.
TableColumn newColumn = new TableColumn(
m.getColumnCount()-1, width, renderer, editor);
super.addColumn(newColumn);
return newColumn;
}
public TableColumn addColumn(Object columnIdentifier, int width,
TableCellRenderer renderer,
TableCellEditor editor, List columnData) {
checkDefaultTableModel();
// Set up the model side first
DefaultTableModel m = (DefaultTableModel)getModel();
m.addColumn(columnIdentifier, columnData.toArray());
// The column will have been added to the end, so the index of the
// column in the model is the last element.
TableColumn newColumn = new TableColumn(
m.getColumnCount()-1, width, renderer, editor);
super.addColumn(newColumn);
return newColumn;
}
public TableColumn addColumn(Object columnIdentifier, int width,
TableCellRenderer renderer,
TableCellEditor editor, List columnData) {
checkDefaultTableModel();
// Set up the model side first
DefaultTableModel m = (DefaultTableModel)getModel();
m.addColumn(columnIdentifier, columnData.toArray());
// The column will have been added to the end, so the index of the
// column in the model is the last element.
TableColumn newColumn = new TableColumn(
m.getColumnCount()-1, width, renderer, editor);
super.addColumn(newColumn);
return newColumn;
}
public TableColumn addColumn(Object columnIdentifier, int width,
TableCellRenderer renderer,
TableCellEditor editor, List columnData) {
checkDefaultTableModel();
// Set up the model side first
DefaultTableModel m = (DefaultTableModel)getModel();
m.addColumn(columnIdentifier, columnData.toArray());
// The column will have been added to the end, so the index of the
// column in the model is the last element.
TableColumn newColumn = new TableColumn(
m.getColumnCount()-1, width, renderer, editor);
super.addColumn(newColumn);
return newColumn;
}
public TableColumn addColumn(Object columnIdentifier, int width,
TableCellRenderer renderer,
TableCellEditor editor, List columnData) {
checkDefaultTableModel();
// Set up the model side first
DefaultTableModel m = (DefaultTableModel)getModel();
m.addColumn(columnIdentifier, columnData.toArray());
// The column will have been added to the end, so the index of the
// column in the model is the last element.
TableColumn newColumn = new TableColumn(
m.getColumnCount()-1, width, renderer, editor);
super.addColumn(newColumn);
return newColumn;
}
public TableColumn addColumn(Object columnIdentifier, int width,
TableCellRenderer renderer,
TableCellEditor editor, List columnData) {
checkDefaultTableModel();
// Set up the model side first
DefaultTableModel m = (DefaultTableModel)getModel();
m.addColumn(columnIdentifier, columnData.toArray());
// The column will have been added to the end, so the index of the
// column in the model is the last element.
TableColumn newColumn = new TableColumn(
m.getColumnCount()-1, width, renderer, editor);
super.addColumn(newColumn);
return newColumn;
}
public TableColumn addColumn(Object columnIdentifier, int width,
TableCellRenderer renderer,
TableCellEditor editor, List columnData) {
checkDefaultTableModel();
// Set up the model side first
DefaultTableModel m = (DefaultTableModel)getModel();
m.addColumn(columnIdentifier, columnData.toArray());
// The column will have been added to the end, so the index of the
// column in the model is the last element.
TableColumn newColumn = new TableColumn(
m.getColumnCount()-1, width, renderer, editor);
super.addColumn(newColumn);
return newColumn;
}
public TableColumn addColumn(Object columnIdentifier, int width,
TableCellRenderer renderer,
TableCellEditor editor, List columnData) {
checkDefaultTableModel();
// Set up the model side first
DefaultTableModel m = (DefaultTableModel)getModel();
m.addColumn(columnIdentifier, columnData.toArray());
// The column will have been added to the end, so the index of the
// column in the model is the last element.
TableColumn newColumn = new TableColumn(
m.getColumnCount()-1, width, renderer, editor);
super.addColumn(newColumn);
return newColumn;
}
private static Object[] buildRowData(DefaultTableModel delegate, Object row) {
int cols = delegate.getColumnCount();
Object[] rowData = new Object[cols];
int i = 0;
for (Iterator<?> it = DefaultGroovyMethods.iterator(row); it.hasNext() && i < cols;) {
rowData[i++] = it.next();
}
return rowData;
}
/**
*
* @param dtm
* The DefaultTableModel to save to stream
* @param out
* The stream to which to save
*
*/
public static void defaultTableModelToStream(final DefaultTableModel dtm,
final Writer out) throws IOException {
final String LINE_SEP = System.getProperty("line.separator");
final int numCols = dtm.getColumnCount();
final int numRows = dtm.getRowCount();
// Write headers
String sep = "";
for (int i = 0; i < numCols; i++) {
out.write(sep);
out.write(dtm.getColumnName(i));
sep = ",";
}
out.write(LINE_SEP);
for (int r = 0; r < numRows; r++) {
sep = "";
for (int c = 0; c < numCols; c++) {
out.write(sep);
out.write(dtm.getValueAt(r, c).toString());
sep = ",";
}
out.write(LINE_SEP);
}
}
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
@Override protected void initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder encoder) {
super.initialize(type, oldInstance, newInstance, encoder);
DefaultTableModel m = (DefaultTableModel) oldInstance;
for (int row = 0; row < m.getRowCount(); row++) {
for (int col = 0; col < m.getColumnCount(); col++) {
Object[] o = {m.getValueAt(row, col), row, col};
encoder.writeStatement(new Statement(oldInstance, "setValueAt", o));
}
}
}
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
@Override protected void initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder encoder) {
super.initialize(type, oldInstance, newInstance, encoder);
DefaultTableModel m = (DefaultTableModel) oldInstance;
// Vector v = m.getDataVector();
// for (int i = 0; i < m.getRowCount(); i++) {
// encoder.writeStatement(new Statement(oldInstance, "addRow", new Object[] { (Vector) v.get(i) }));
// }
for (int row = 0; row < m.getRowCount(); row++) {
for (int col = 0; col < m.getColumnCount(); col++) {
Object[] o = {m.getValueAt(row, col), row, col};
encoder.writeStatement(new Statement(oldInstance, "setValueAt", o));
}
}
}
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
@Override protected void initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder encoder) {
super.initialize(type, oldInstance, newInstance, encoder);
DefaultTableModel m = (DefaultTableModel) oldInstance;
for (int row = 0; row < m.getRowCount(); row++) {
for (int col = 0; col < m.getColumnCount(); col++) {
Object[] o = {m.getValueAt(row, col), row, col};
encoder.writeStatement(new Statement(oldInstance, "setValueAt", o));
}
}
}
@Override
protected void finished()
{
super.finished();
// when the task is finished, clear it
doubleEntryTask = null;
// hide task-progress bar
jPanel1.setVisible(false);
// create tablemodel for the table data, which is not editable
DefaultTableModel tm = new DefaultTableModel(new Object[] {}, 0) {
@Override public boolean isCellEditable(int row, int column) { return false; }
};
// apply model to table
jTable1.setModel(tm);
// and delete all rows and columns
tm.setRowCount(0);
tm.setColumnCount(0);
// now fill the table
// check whether we have any multiple entries at all.
if (!doubleentries.isEmpty()) {
// create iterator for the list
Iterator<Integer[]> it = doubleentries.iterator();
// iterate all multiple entries
while (it.hasNext()) {
// retrieve the row data
Integer[] rowdata = it.next();
// if the rowdate has more columns than the table model, adjust
// column count
while (tm.getColumnCount()<rowdata.length) {
tm.addColumn(String.valueOf(tm.getColumnCount()+1));
}
// add it to the table model
tm.addRow((Object[])rowdata);
}
// finally, enable table
jTable1.setEnabled(true);
}
else {
JOptionPane.showMessageDialog(null,resourceMap.getString("noMultipleEntriesFoundMsg"),resourceMap.getString("noMultipleEntriesFoundTitle"),JOptionPane.PLAIN_MESSAGE);
setVisible(false);
dispose();
}
}
private MainPanel() {
super(new BorderLayout());
// <blockquote cite="FixedColumnExample.java">
// @author Nobuo Tamemasa
Object[][] data = {
{1, 11, "A", ES, ES, ES, ES, ES},
{2, 22, ES, "B", ES, ES, ES, ES},
{3, 33, ES, ES, "C", ES, ES, ES},
{4, 1, ES, ES, ES, "D", ES, ES},
{5, 55, ES, ES, ES, ES, "E", ES},
{6, 66, ES, ES, ES, ES, ES, "F"}
};
String[] columnNames = {"fixed 1", "fixed 2", "A", "B", "C", "D", "E", "F"};
// </blockquote>
DefaultTableModel model = new DefaultTableModel(data, columnNames) {
@Override public Class<?> getColumnClass(int column) {
return column < FIXED_COLUMN_RANGE ? Integer.class : Object.class;
}
};
RowSorter<? extends TableModel> sorter = new TableRowSorter<>(model);
JTable fixedTable = new JTable(model);
JTable table = new JTable(model);
fixedTable.setSelectionModel(table.getSelectionModel());
for (int i = model.getColumnCount() - 1; i >= 0; i--) {
if (i < FIXED_COLUMN_RANGE) {
table.removeColumn(table.getColumnModel().getColumn(i));
fixedTable.getColumnModel().getColumn(i).setResizable(false);
} else {
fixedTable.removeColumn(fixedTable.getColumnModel().getColumn(i));
}
}
fixedTable.setRowSorter(sorter);
fixedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
fixedTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fixedTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
table.setRowSorter(sorter);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
JScrollPane scroll = new JScrollPane(table);
// JViewport viewport = new JViewport();
// viewport.setView(fixedTable);
// viewport.setPreferredSize(fixedTable.getPreferredSize());
// scroll.setRowHeader(viewport);
fixedTable.setPreferredScrollableViewportSize(fixedTable.getPreferredSize());
scroll.setRowHeaderView(fixedTable);
scroll.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, fixedTable.getTableHeader());
scroll.getViewport().setBackground(Color.WHITE);
scroll.getRowHeader().setBackground(Color.WHITE);
// <blockquote cite="https://tips4java.wordpress.com/2008/11/05/fixed-column-table/">
// @author Rob Camick
scroll.getRowHeader().addChangeListener(e -> {
JViewport viewport = (JViewport) e.getSource();
scroll.getVerticalScrollBar().setValue(viewport.getViewPosition().y);
});
// </blockquote>
JButton addButton = new JButton("add");
addButton.addActionListener(e -> {
sorter.setSortKeys(null);
IntStream.range(0, 100).mapToObj(i -> new Object[] {i, i + 1, "A" + i, "B" + i}).forEach(model::addRow);
});
add(scroll);
add(addButton, BorderLayout.SOUTH);
setPreferredSize(new Dimension(320, 240));
}