下面列出了javax.swing.table.DefaultTableModel#addColumn ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private JTable getVirtualMachineTable() {
DefaultTableModel vmTableModel = new DefaultTableModel();
vmTableModel.addColumn("PID");
vmTableModel.addColumn("Application");
try {
VirtualMachineService vmService = VirtualMachineService.getInstance();
for (VirtualMachineData vmData : vmService.getVirtualMachines()) {
vmTableModel.addRow(new Object[]{
vmData.getId(),
vmData.getDescription()
});
}
} catch (VirtualMachineServiceException e) {
Main.getInstance().processException(e);
}
JTable vmTable = new JTable(vmTableModel);
return vmTable;
}
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;
}
/**
* Present data in javax.swing.table.DefaultTableModel form.
*
* @param model {@link ObjectTableModel}
* @param formats Array of {@link Format} array can contain null formatters in this case value is added as is
* @param columns Columns headers
* @return data in table form
*/
public static DefaultTableModel getAllDataAsTable(ObjectTableModel model, Format[] formats, String[] columns) {
final List<List<Object>> table = getAllTableData(model, formats);
final DefaultTableModel tableModel = new DefaultTableModel();
for (String header : columns) {
tableModel.addColumn(header);
}
for (List<Object> row : table) {
tableModel.addRow(new Vector(row));
}
return tableModel;
}
/**
* 初始化方法
*/
protected void init() {
initFlag = false;
//初始化分组
initGroup();
//初始化列
columnConfigInfo = initColumn();
tableModel = new DefaultTableModel();
for (ColumnConfig column : columnConfigInfo) {
tableModel.addColumn(column.getTitle());
}
//初始化数据
getCurrGroup().getElementList().forEach(e -> {
tableModel.addRow(toRow(e));
});
table.setModel(tableModel);
refreshEditorType();
initFlag = true;
}
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 void initTable(JTable table, String[] data, String columnName) {
DefaultTableModel model = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
model.addColumn(columnName);
for (int i = 0; i < data.length; i++) {
model.addRow(new Object[] {data[i]});
}
table.setModel(model);
}
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 void testDataSchemaForPlainIndexTables() throws ReportDataFactoryException {
final DefaultTableModel model = new DefaultTableModel();
model.addColumn( "Test" );
model.addColumn( "Test2" );
final ResourceManager mgr = new ResourceManager();
mgr.registerDefaults();
final DataSchemaDefinition schemaDefinition = DataSchemaUtility.parseDefaults( mgr );
final DataSchemaCompiler compiler =
new DataSchemaCompiler( schemaDefinition, new DefaultDataAttributeContext(), mgr );
final DataSchema compiledSchema = compiler.compile( new IndexedTableModel( model ) );
final DataAttributes attributes = compiledSchema.getAttributes( "::column::0" );
assertNotNull( attributes );
assertEquals( "Test", attributes.getMetaAttribute( MetaAttributeNames.Formatting.NAMESPACE,
MetaAttributeNames.Formatting.LABEL, String.class, new DefaultDataAttributeContext() ) );
final DataAttributes attributes2 = compiledSchema.getAttributes( "::column::1" );
assertNotNull( attributes2 );
assertEquals( "Test2", attributes2.getMetaAttribute( MetaAttributeNames.Formatting.NAMESPACE,
MetaAttributeNames.Formatting.LABEL, String.class, new DefaultDataAttributeContext() ) );
}
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 UpdateTableDialog(java.awt.Frame parent, boolean modal, String outputResult, String tableName, Object[] columnNames) {
super(parent, modal);
initComponents();
this.outputResult = outputResult;
this.tableName = tableName;
this.columnNames = columnNames;
this.mainFrame = (MainFrame) parent;
setTitle("Update table `" + tableName + "`");
setLocationRelativeTo(null);
resultTable.setModel(new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
});
resultTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
defaultTableModel = (DefaultTableModel) resultTable.getModel();
for (Object columnName : columnNames) {
defaultTableModel.addColumn(columnName);
}
tableColumnAdjuster = new TableColumnAdjuster(resultTable);
processResult();
}
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;
}
/**
* Creates a sample dataset. <!-- (Used in JUnitTest) -->
*
* @return A <code>TableModel</code>.
*/
public static TableModel createData()
{
final DefaultTableModel data = new DefaultTableModel();
data.addColumn("A");
data.addColumn("B");
data.addRow(new Object[]{new Double(43.0), new Double(127.5)});
data.addRow(new Object[]{new Double(57.0), new Double(108.5)});
data.addRow(new Object[]{new Double(35.0), new Double(164.8)});
data.addRow(new Object[]{new Double(86.0), new Double(164.0)});
data.addRow(new Object[]{new Double(12.0), new Double(103.2)});
return data;
}
/**
* Loads the Data Array into the tmodel
*
* @param table to be populated
* @param header column header
* @param rows nullRoww data
* @return populated tmodel
*/
public static JTable populatetable(JTable table, String[] header, List<String[]> rows) {
removeRowSelection(table);
DefaultTableModel tablemodel = (DefaultTableModel) table.getModel();
tablemodel.setRowCount(0);
for (String col : header) {
tablemodel.addColumn(col);
}
for (String[] row : rows) {
tablemodel.addRow(row);
}
table.setModel(tablemodel);
return table;
}
@Override
protected void updateUIComponents() {
CsvFile csvFile = getCsvFile();
if (csvFile == null) {
return;
}
CsvColumnInfoMap<PsiElement> columnInfoMap = csvFile.getColumnInfoMap();
if (Objects.equals(lastColumnInfoMap, columnInfoMap)) {
return;
}
lastColumnInfoMap = columnInfoMap;
updateInteractionElements();
DefaultTableModel tableModel = new DefaultTableModel(0, 0);
if (!columnInfoMap.hasErrors()) {
int startRow = getFileEditorState().getFixedHeaders() ? 1 : 0;
for (int columnIndex = 0; columnIndex < columnInfoMap.getColumnInfos().size(); ++columnIndex) {
CsvColumnInfo<PsiElement> columnInfo = columnInfoMap.getColumnInfo(columnIndex);
List<PsiElement> elements = columnInfo.getElements();
if (columnIndex == 0 && CsvEditorSettings.getInstance().isFileEndLineBreak() &&
lastColumnInfoMap.hasEmptyLastLine()) {
elements.remove(elements.size() - 1);
}
tableModel.addColumn(String.format("Column %s (%s entries)", columnIndex + 1, elements.size()),
elements.stream()
.skip(startRow)
.map(psiElement -> psiElement == null ? "" : CsvHelper.unquoteCsvValue(psiElement.getText(), currentEscapeCharacter))
.collect(Collectors.toList()).toArray(new String[0]));
}
}
Object[][] values = getTableComponentData(tableModel, true);
updateTableComponentData(dataManagement.addState(values));
}
/**
* populates transforms of currently selected rule
*/
private void populateTransforms() {
DeclensionGenRule curRule = (DeclensionGenRule) lstRules.getSelectedValue();
transModel = new DefaultTableModel();
transModel.addColumn("Regex");
transModel.addColumn("Replacement");
tblTransforms.setModel(transModel);
// do not populate if multiple selections
if (lstRules.getSelectedIndices().length > 1) {
return;
}
boolean useConFont = !core.getPropertiesManager().isOverrideRegexFont();
TableColumn column = tblTransforms.getColumnModel().getColumn(0);
column.setCellEditor(new PCellEditor(useConFont, core));
column.setCellRenderer(new PCellRenderer(useConFont, core));
column = tblTransforms.getColumnModel().getColumn(1);
column.setCellEditor(new PCellEditor(useConFont, core));
column.setCellRenderer(new PCellRenderer(useConFont, core));
// do nothing if nothing selected in rule list
if (curRule == null) {
return;
}
DeclensionGenTransform[] curTransforms = curRule.getTransforms();
for (DeclensionGenTransform curTrans : curTransforms) {
Object[] newRow = {curTrans.regex, curTrans.replaceText};
transModel.addRow(newRow);
}
tblTransforms.setModel(transModel);
}
private void setupRomTable() {
DefaultTableModel romTableModel = new DefaultTableModel();
romTableModel.addColumn("Character(s)");
romTableModel.addColumn("Romanization");
tblRom.setModel(romTableModel); // TODO: find way to make rom display RTL order when appropriate Maybe something on my custom cell editor
boolean useConFont = !core.getPropertiesManager().isOverrideRegexFont();
TableColumn column = tblRom.getColumnModel().getColumn(0);
column.setCellEditor(new PCellEditor(useConFont, core));
column.setCellRenderer(new PCellRenderer(useConFont, core));
column = tblRom.getColumnModel().getColumn(1);
column.setCellEditor(new PCellEditor(false, core));
column.setCellRenderer(new PCellRenderer(false, core));
// disable tab/arrow selection
InputMap procInput = tblRom.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
procInput.put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "none");
procInput.put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_DOWN_MASK), "none");
procInput.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "none");
procInput.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, InputEvent.SHIFT_DOWN_MASK), "none");
procInput.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "none");
procInput.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, InputEvent.SHIFT_DOWN_MASK), "none");
procInput.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "none");
procInput.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.SHIFT_DOWN_MASK), "none");
procInput.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "none");
procInput.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.SHIFT_DOWN_MASK), "none");
}
/**
* populates all properties of given logograph at index
*
* @param index index to populate props from
*/
private void populateLogoProps(int index) {
if (curPopulating) {
return;
}
curPopulating = true;
if (index == -1) {
txtName.setText("");
txtNotes.setText("");
txtStrokes.setText("");
lstRadicals.setModel(new DefaultListModel<>());
tblReadings.setModel(new DefaultTableModel(new Object[]{"Readings"}, 0));
lstRelWords.setModel(new DefaultListModel<>());
chkIsRad.setSelected(false);
lblLogo.setIcon(new ImageIcon(new LogoNode().getLogoGraph().getScaledInstance(
lblLogo.getWidth(), lblLogo.getHeight(), Image.SCALE_SMOOTH)));
populateRelatedWords();
setEnableControls(false);
curPopulating = false;
return;
}
LogoNode curNode = (LogoNode) lstLogos.getModel().getElementAt(index);
txtName.setText(curNode.getValue());
txtNotes.setText(curNode.getNotes());
txtStrokes.setText(String.valueOf(curNode.getStrokes()));
chkIsRad.setSelected(curNode.isRadical());
// Populate radicals
DefaultListModel<Object> radModel = new DefaultListModel<>();
for (LogoNode radNode : curNode.getRadicals()) {
try {
radModel.addElement(radNode);
} catch (Exception e) {
// do nothing
IOHandler.writeErrorLog(e);
}
}
lstRadicals.setModel(radModel);
// TODO: figure out a way to make this respect RTL languages... maybe just insert char here? and cut at save time? Messy but effective...
// Populate readings
DefaultTableModel procModel = new DefaultTableModel();
procModel.addColumn("Readings");
tblReadings.setModel(procModel);
// Fonts must be set each time the table is rebuilt
TableColumn column = tblReadings.getColumnModel().getColumn(0);
column.setCellEditor(new PCellEditor(true, core));
column.setCellRenderer(new PCellRenderer(true, core));
for (String curProc : curNode.getReadings()) {
Object[] newRow = {curProc};
procModel.addRow(newRow);
}
tblReadings.setModel(procModel);
// set logograph picture
lblLogo.setIcon(new ImageIcon(curNode.getLogoGraph().getScaledInstance(
lblLogo.getWidth(), lblLogo.getHeight(), Image.SCALE_SMOOTH)));
populateRelatedWords();
setEnableControls(true);
curPopulating = false;
}
public HeapProfilerView(Client client) {
this.client = client;
DefaultTableModel tableModel = new DefaultTableModel();
tableModel.addColumn("Class name");
tableModel.addColumn("Instances Count");
tableModel.addColumn("Collected Instances Count");
tableModel.addColumn("Total Size");
try {
for (Clazz clazz : DaoFactory.getClazzDao().getByCondition("CREATECOUNTER > 0 ORDER BY TOTALCURRENTSIZE DESC")) {
tableModel.addRow(new Object[]{
clazz.getName(),
clazz.getCreateCounter() - clazz.getCollectCounter(),
clazz.getCollectCounter(),
clazz.getTotalCurrentSize()
});
}
} catch (DaoException e) {
Main.getInstance().processException(e);
}
JTable table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setViewportView(table);
setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
}