本文整理汇总了Java中javax.swing.JTable.getColumnCount方法的典型用法代码示例。如果您正苦于以下问题:Java JTable.getColumnCount方法的具体用法?Java JTable.getColumnCount怎么用?Java JTable.getColumnCount使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.JTable
的用法示例。
在下文中一共展示了JTable.getColumnCount方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: resizeColumnWidth
import javax.swing.JTable; //导入方法依赖的package包/类
public void resizeColumnWidth(JTable table) {
final TableColumnModel columnModel = table.getColumnModel();
for (int column = 0; column < table.getColumnCount(); column++) {
int width = 50; // Min width
for (int row = 0; row < table.getRowCount(); row++) {
TableCellRenderer renderer = table.getCellRenderer(row, column);
Component comp = table.prepareRenderer(renderer, row, column);
width = Math.max(comp.getPreferredSize().width, width);
}
columnModel.getColumn(column).setPreferredWidth(width);
columnModel.getColumn(column).setMinWidth(minColumnWidths[column]);
}
}
示例2: autoSizeTableColumns
import javax.swing.JTable; //导入方法依赖的package包/类
public static void autoSizeTableColumns(JTable table) {
TableModel model = table.getModel();
TableColumn column = null;
Component comp = null;
int headerWidth = 0;
int maxCellWidth = Integer.MIN_VALUE;
int cellWidth = 0;
TableCellRenderer headerRenderer =
table.getTableHeader().getDefaultRenderer();
for (int i = 0; i < table.getColumnCount(); i++) {
column = table.getColumnModel().getColumn(i);
comp = headerRenderer.getTableCellRendererComponent(table,
column.getHeaderValue(), false, false, 0, 0);
headerWidth = comp.getPreferredSize().width + 10;
maxCellWidth = Integer.MIN_VALUE;
for (int j = 0; j < Math.min(model.getRowCount(), 30); j++) {
TableCellRenderer r = table.getCellRenderer(j, i);
comp = r.getTableCellRendererComponent(table,
model.getValueAt(j, i),
false, false, j, i);
cellWidth = comp.getPreferredSize().width;
if (cellWidth >= maxCellWidth) {
maxCellWidth = cellWidth;
}
}
column.setPreferredWidth(Math.max(headerWidth, maxCellWidth)
+ 10);
}
}
示例3: addcol
import javax.swing.JTable; //导入方法依赖的package包/类
/**
* Adding column is done by creating new model by modifying older one.<p>
*
* Insert new column if column is outside the <code>limit</code> Adds new
* column if selected column inside the <code>limit</code>[email protected] _table
* target table
*
* @param limit the range to avoid inserting
*/
static void addcol(JTable table, int limit) {
try {
int sc = table.getSelectedColumn();
if (sc < limit - 1) {
sc = table.getColumnCount() - 1;
}
DefaultTableModel tableM = (DefaultTableModel) table.getModel();
DefaultTableModel tableM1 = new DefaultTableModel();
TableModelListener[] listeners = tableM.getTableModelListeners();
tableM1.setDataVector(newvectoraddcol(tableM.getDataVector(), sc), getColumnIdentifiersaddcol(sc + 1, table));
table.setModel(tableM1);
for (TableModelListener l : listeners) {
tableM1.addTableModelListener(l);
}
} catch (Exception ex) {
Logger.getLogger(JtableUtils.class.getName()).log(Level.SEVERE, null, ex);
}
}
示例4: checkforduplicaterow
import javax.swing.JTable; //导入方法依赖的package包/类
/**
* check whether the <code>row</code> present or not
*
* @param table target table
* @param row target row
* @return <code>true</code> if present else <code>false</code>
*/
static boolean checkforduplicaterow(JTable table, Object[] row) {
String value = "", value1 = "";
int cRow = table.getRowCount(), cCol = table.getColumnCount();
for (Object cell : row) {
value1 += cell != null ? cell.toString() : "";
}
for (int i = 0; i < cRow; i++) {
for (int j = 0; j < cCol; j++) {
Object tmp = table.getModel().getValueAt(i, j);
if (j == 4) {
tmp = "NoRun";
}
value += tmp != null ? tmp.toString() : "";
}
if (value.equals(value1)) {
return true;
}
value = "";
}
return false;
}
示例5: pasteFromClipboard
import javax.swing.JTable; //导入方法依赖的package包/类
/**
* Reads clipboard data and converts it into supported format and fills the
* tmodel cells
*
* @param table the target tmodel
*/
private static void pasteFromClipboard(JTable table) {
int startRow = table.getSelectedRows()[0];
int startCol = table.getSelectedColumns()[0];
String pasteString;
try {
pasteString = (String) (CLIPBOARD.getContents(CLIPBOARD).getTransferData(DataFlavor.stringFlavor));
} catch (UnsupportedFlavorException | IOException ex) {
Logger.getLogger(JtableUtils.class.getName()).log(Level.SEVERE, null, ex);
return;
}
String[] lines = pasteString.split(LINE_BREAK);
for (int i = 0; i < lines.length; i++) {
String[] cells = lines[i].split(CELL_BREAK);
if (table.getRowCount() <= startRow + i) {
((DefaultTableModel) table.getModel()).addRow(nullRow);
}
for (int j = 0; j < cells.length; j++) {
if (table.getColumnCount() > startCol + j) {
if (table.isCellEditable(startRow + i, startCol + j)) {
table.setValueAt(cells[j], startRow + i, startCol + j);
}
}
}
}
}
示例6: pasteFromClipboard
import javax.swing.JTable; //导入方法依赖的package包/类
public static void pasteFromClipboard(JTable table) {
int startRow = table.getSelectedRows()[0];
int startCol = table.getSelectedColumns()[0];
String pasteString;
try {
pasteString = (String) (CLIPBOARD.getContents(null).getTransferData(DataFlavor.stringFlavor));
} catch (Exception e) {
Logger.getLogger(XTableUtils.class.getName()).log(Level.WARNING, "Invalid Paste Type", e);
return;
}
if (table.getModel() instanceof UndoRedoModel) {
((UndoRedoModel) table.getModel()).startGroupEdit();
}
String[] lines = pasteString.split(LINE_BREAK);
for (int i = 0; i < lines.length; i++) {
String[] cells = lines[i].split(CELL_BREAK);
for (int j = 0; j < cells.length; j++) {
if (table.getRowCount() <= startRow + i) {
if (table.getModel() instanceof DataModel) {
if (!((DataModel) table.getModel()).addRow()) {
return;
}
}
}
if (table.getRowCount() > startRow + i && table.getColumnCount() > startCol + j) {
table.setValueAt(cells[j], startRow + i, startCol + j);
}
}
}
if (table.getModel() instanceof UndoRedoModel) {
((UndoRedoModel) table.getModel()).stopGroupEdit();
}
}
示例7: getColumnIdentifiersaddcol
import javax.swing.JTable; //导入方法依赖的package包/类
/**
* creates modified column model
*
* @param scol position to insert new column
* @param table target table
* @return modified column model
*/
private static Vector<String> getColumnIdentifiersaddcol(int scol, JTable table) {
Vector<String> columnIdentifiers = new Vector<>();
int j = 0;
while (j < table.getColumnCount()) {
columnIdentifiers.add(table.getColumnName(j));
j++;
}
columnIdentifiers.add(scol, "");
return columnIdentifiers;
}
示例8: installEditableHeader
import javax.swing.JTable; //导入方法依赖的package包/类
/**
* This method installs the given renderer and editors to the header of the given table. The
* previous header names will be discarded and replaced by the objects given as initivalValues.
* These might be changed by the editor components. After this, they might be returned by
* getHeaderValue.
*/
public static void installEditableHeader(JTable table, TableCellRenderer headerRenderer, TableCellEditor headerEditor,
Object[] initialValues) {
TableColumnModel columnModel = table.getColumnModel();
table.setTableHeader(new EditableTableHeader(columnModel));
for (int i = 0; i < table.getColumnCount(); i++) {
EditableTableHeaderColumn col = (EditableTableHeaderColumn) table.getColumnModel().getColumn(i);
col.setHeaderValue(initialValues[i]);
col.setHeaderRenderer(headerRenderer);
col.setHeaderEditor(headerEditor);
}
}
示例9: _getText
import javax.swing.JTable; //导入方法依赖的package包/类
@Override public String _getText() {
JTable table = (JTable) getComponent();
int rows = table.getRowCount();
int cols = table.getColumnCount();
JSONArray r = new JSONArray();
for (int i = 0; i < rows; i++) {
JSONArray c = new JSONArray();
for (int j = 0; j < cols; j++) {
c.put(new JTableCellJavaElement(JTableJavaElement.this, i, j)._getText());
}
r.put(c);
}
return r.toString();
}
示例10: emptyTable
import javax.swing.JTable; //导入方法依赖的package包/类
public static void emptyTable(JTable table, int column) {
for (int i = 0; i < table.getRowCount(); i++) {
for (int j = 0; j < table.getColumnCount(); j++) {
try {
if (column != j) {
table.setValueAt(null, i, j);
}
} catch (Exception ex) {
Logger.getLogger(JtableUtils.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
示例11: getDuplicateRows
import javax.swing.JTable; //导入方法依赖的package包/类
/**
* returns the list of duplicated rows customized for test set Table
*
* @param table
* @return the list
*
*/
public static ArrayList<Integer> getDuplicateRows(JTable table) {
removeEmptyRows(table);
cancelEditing(table);
ArrayList<Integer> dRows = new ArrayList<>();
HashMap<String, Integer> uniqRows = new HashMap<>();
List<Integer> dontCheckIndex = Arrays.asList(new Integer[]{0, 4});//left the first column [ececute] and 5 column status
int colCount = table.getColumnCount(),
rowCount = table.getModel().getRowCount(), i;
for (i = 0; i < rowCount; i++) {
String row = "";
for (int j = 0; j < colCount; j++) {
if (dontCheckIndex.contains(j)) {
continue;
}
Object val = table.getValueAt(i, j);
row += ((val == null) ? "" : val.toString());
}
if (uniqRows.containsKey(row)) {
String exe = table.getValueAt(i, dontCheckIndex.get(0)).toString();
String status = table.getValueAt(i, dontCheckIndex.get(1)).toString();
if ("false".equals(exe)) {
dRows.add(i);
} else if ("norun".equalsIgnoreCase(status)) {
dRows.add(i);
} else {
dRows.add(uniqRows.get(row));
uniqRows.put(row, i);
}
} else {
uniqRows.put(row, i);
}
}
return dRows;
}
示例12: printDebugData
import javax.swing.JTable; //导入方法依赖的package包/类
private void printDebugData(JTable table) {
int numRows = table.getRowCount();
int numCols = table.getColumnCount();
javax.swing.table.TableModel model = table.getModel();
System.out.println("Value of data: ");
for (int i = 0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j = 0; j < numCols; j++) {
System.out.print(" " + model.getValueAt(i, j));
}
System.out.println();
}
System.out.println("--------------------------");
}
示例13: setHeadersWidth
import javax.swing.JTable; //导入方法依赖的package包/类
public void setHeadersWidth(JTable theTable) {
int index = theTable.getColumnCount();
for(int i=0; i < index; i++) {
column = theTable.getColumnModel().getColumn(i);
if (i == 0 || i == 1) {
column.setPreferredWidth(50);
} else {
column.setPreferredWidth(450);//third column is bigger
}
}
}
开发者ID:Coder-ACJHP,项目名称:Hotel-Properties-Management-System,代码行数:14,代码来源:CleaningRoomTableColumnsMaker.java
示例14: SimpleTableDemo
import javax.swing.JTable; //导入方法依赖的package包/类
public SimpleTableDemo(LazyReplicatedMap<String,StringBuilder> map) {
super();
this.map = map;
this.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
//final JTable table = new JTable(data, columnNames);
table = new JTable(dataModel);
table.setPreferredScrollableViewportSize(new Dimension(WIDTH, 150));
for ( int i=0; i<table.getColumnCount(); i++ ) {
TableColumn tm = table.getColumnModel().getColumn(i);
tm.setCellRenderer(new ColorRenderer());
}
if (DEBUG) {
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
printDebugData(table);
}
});
}
//setLayout(new GridLayout(5, 0));
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
//create a add value button
JPanel addpanel = new JPanel();
addpanel.setPreferredSize(new Dimension(WIDTH,30));
addpanel.add(createButton("Add","add"));
addpanel.add(txtAddKey);
addpanel.add(txtAddValue);
addpanel.setMaximumSize(new Dimension(WIDTH,30));
add(addpanel);
//create a remove value button
JPanel removepanel = new JPanel( );
removepanel.setPreferredSize(new Dimension(WIDTH,30));
removepanel.add(createButton("Remove","remove"));
removepanel.add(txtRemoveKey);
removepanel.setMaximumSize(new Dimension(WIDTH,30));
add(removepanel);
//create a change value button
JPanel changepanel = new JPanel( );
changepanel.add(createButton("Change","change"));
changepanel.add(txtChangeKey);
changepanel.add(txtChangeValue);
changepanel.setPreferredSize(new Dimension(WIDTH,30));
changepanel.setMaximumSize(new Dimension(WIDTH,30));
add(changepanel);
//create sync button
JPanel syncpanel = new JPanel( );
syncpanel.add(createButton("Synchronize","sync"));
syncpanel.add(createButton("Replicate","replicate"));
syncpanel.add(createButton("Random","random"));
syncpanel.setPreferredSize(new Dimension(WIDTH,30));
syncpanel.setMaximumSize(new Dimension(WIDTH,30));
add(syncpanel);
}
示例15: getJTableOptions
import javax.swing.JTable; //导入方法依赖的package包/类
public static void getJTableOptions(final String name, final JTable table,
final Properties properties) {
final Integer colCount = getObjectInteger(name + "_col_count",
properties);
if (colCount == null || colCount.intValue() != table.getColumnCount())
return;
final Object cols[] = new Object[table.getColumnCount()];
for (int i = 0; i < colCount.intValue(); i++) {
cols[i] = table.getColumnModel().getColumn(
table.convertColumnIndexToView(i));
}
for (int i = 0; i < colCount.intValue(); i++) {
try {
int index = table.convertColumnIndexToView(i);
final int width = getInteger(name + "_col_" + i + "_width",
table.getColumnModel().getColumn(index).getWidth(),
properties);
table.getColumnModel().getColumn(index)
.setPreferredWidth(width);
} catch (Exception e) {
}
}
final TableColumnModel cm = table.getColumnModel();
final int tci[] = new int[colCount.intValue()];
for (int i = 0; i < colCount.intValue(); i++)
cm.removeColumn((TableColumn) cols[i]);
for (int i = 0; i < colCount.intValue(); i++) {
tci[i] = getInteger(name + "_col_" + i + "_index", i, properties);
}
for (int i = 0; i < colCount.intValue(); i++)
for (int j = 0; j < colCount.intValue(); j++)
if (tci[j] == i)
cm.addColumn((TableColumn) cols[j]);
}