javax.swing.JTable#getRowCount ( )源码实例Demo

下面列出了javax.swing.JTable#getRowCount ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: FlatLaf   文件: FlatTableCellBorder.java
/**
 * Checks whether at least one selected cell is editable.
 */
protected boolean isSelectionEditable( JTable table ) {
	if( table.getRowSelectionAllowed() ) {
		int columnCount = table.getColumnCount();
		int[] selectedRows = table.getSelectedRows();
		for( int selectedRow : selectedRows ) {
			for( int column = 0; column < columnCount; column++ ) {
				if( table.isCellEditable( selectedRow, column ) )
					return true;
			}
		}
	}

	if( table.getColumnSelectionAllowed() ) {
		int rowCount = table.getRowCount();
		int[] selectedColumns = table.getSelectedColumns();
		for( int selectedColumn : selectedColumns ) {
			for( int row = 0; row < rowCount; row++ ) {
				if( table.isCellEditable( row, selectedColumn ) )
					return true;
			}
		}
	}

	return false;
}
 
源代码2 项目: desktopclient-java   文件: ListView.java
@SuppressWarnings("unchecked")
private static <V> FlyweightItem updateFlyweight(FlyweightItem item,
        JTable table, Object value, int row, boolean isSelected) {
    V valueItem = (V) value;
    // hopefully return value is not used
    if (table == null || valueItem == null) {
        return item;
    }

    boolean isLast = row == table.getRowCount() - 1;
    item.render(valueItem, table.getWidth(), isSelected, isLast);

    int height = Math.max(table.getRowHeight(), item.getPreferredSize().height);
    // view item needs a little more then it preferres
    height += 1;
    if (height != table.getRowHeight(row)) {
        // NOTE: this calls resizeAndRepaint()
        table.setRowHeight(row, height);
    }

    return item;
}
 
源代码3 项目: Repeat   文件: SwingUtil.java
public TableSearcher(JTable table, String value, Function<Point,Void> functions){
	this.action = functions;

	this.found = new ArrayList<Point>();
	index = -1;
	for (int row = 0; row < table.getRowCount(); row++) {
		for (int column = 0; column < table.getColumnCount(); column++) {
			if (value.equals(getStringValueTable(table, row, column))) {
				found.add(new Point(row, column));
			}
		}
	}

	if (!found.isEmpty()) {
		index = 0;
	}
}
 
源代码4 项目: netbeans   文件: FmtImports.java
@Override
protected void storeTableData(final JTable table, final String optionID, final Preferences node) {
    StringBuilder sb = null;
    for (int i = 0; i < table.getRowCount(); i++) {
        if (sb == null) {
            sb = new StringBuilder();
        } else {
            sb.append(';');
        }
        for (int j = 0; j < table.getColumnCount(); j++) {
            if (Boolean.class.equals(table.getColumnClass(j))) {
                if (((Boolean)table.getValueAt(i, j)).booleanValue())
                    sb.append(j == 0 ? "static " : ".*"); //NOI18N
            } else {
                Object val = table.getValueAt(i, j);
                sb.append(allOtherImports == val ? "*" : val); //NOI18N
            }
        }
    }
    String value = sb != null ? sb.toString() : ""; //NOI18N
    if (getDefaultAsString(optionID).equals(value))
        node.remove(optionID);
    else
        node.put(optionID, value);            
}
 
private List<HashMap<String, String>> getTableData(JTable table, String moduleId) {
    List<HashMap<String, String>> data = new ArrayList<>();
    HashMap<String, String> row;
    JtableUtils.removeEmptyRows(table);
    int colCount = table.getColumnCount(),
            rowCount = table.getRowCount(), i;

    for (i = 0; i < rowCount; i++) {
        row = new HashMap<>();
        row.put("moduleId", moduleId);
        for (int j = 0; j < colCount; j++) {
            Object val = table.getValueAt(i, j);
            row.put(column[j], (val == null) ? "" : val.toString());
        }
        data.add(row);
    }
    return data;
}
 
源代码6 项目: pcgen   文件: EquipInfoTab.java
private static List<EquipNode> getMenuTargets(JTable table, MouseEvent e)
{
	int row = table.rowAtPoint(e.getPoint());
	if (!table.isRowSelected(row))
	{
		if ((row >= 0) && (table.getRowCount() > row))
		{
			table.setRowSelectionInterval(row, row);
		}
	}
	return Arrays.stream(table.getSelectedRows())
	             .mapToObj(selRow -> table.getModel().getValueAt(selRow, 0))
	             .filter(value -> value instanceof EquipNode)
	             .map(value -> (EquipNode) value)
	             .collect(Collectors.toList());
}
 
源代码7 项目: Tomcat8-Source-Read   文件: MapDemo.java
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("--------------------------");
}
 
源代码8 项目: CQL   文件: GuiUtil.java
@SuppressWarnings("serial")
public static JPanel makeTable(Border b, String border, Object[][] rowData, Object... colNames) {
	JTable t = new JTable(rowData, colNames) {
		@Override
		public Dimension getPreferredScrollableViewportSize() {
			Dimension d = getPreferredSize();
			return new Dimension(Integer.max(640, d.width), (d.height));
		}
	};
	JPanel p = new JPanel(new GridLayout(1, 1));
	TableRowSorter<?> sorter = new MyTableRowSorter(t.getModel());
	if (colNames.length > 0) {
		sorter.toggleSortOrder(0);
	}
	t.setRowSorter(sorter);
	sorter.allRowsChanged();
	p.add(new JScrollPane(t));

	for (int row = 0; row < t.getRowCount(); row++) {
		int rowHeight = t.getRowHeight();

		for (int column = 0; column < t.getColumnCount(); column++) {
			Component comp = t.prepareRenderer(t.getCellRenderer(row, column), row, column);
			rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
		}

		t.setRowHeight(row, rowHeight);
	}

	Font font = UIManager.getFont("TableHeader.font");
	p.setBorder(BorderFactory.createTitledBorder(b, border, TitledBorder.DEFAULT_JUSTIFICATION,
			TitledBorder.DEFAULT_POSITION, font, Color.black));
	return p;

}
 
private List<HashMap<String, String>> getTableData(JTable table) {
    List<HashMap<String, String>> data = new ArrayList<>();
    HashMap<String, String> row;
    JtableUtils.removeEmptyRows(table);
    int rowCount = table.getRowCount(), i;
    for (i = 0; i < rowCount; i++) {
        row = new HashMap<>();
        Object val = table.getValueAt(i, 1);
        row.put("moduleId", (val == null) ? "" : val.toString());
        data.add(row);
    }
    return data;
}
 
源代码10 项目: Method_Trace_Tool   文件: Utils.java
public static void fitTableColumns(JTable myTable) {

        JTableHeader header = myTable.getTableHeader();
        int rowCount = myTable.getRowCount();
        Enumeration columns = myTable.getColumnModel().getColumns();
        while (columns.hasMoreElements()) {
            TableColumn column = (TableColumn) columns.nextElement();
            int col = header.getColumnModel().getColumnIndex(
                    column.getIdentifier());
            int width = (int) myTable
                    .getTableHeader()
                    .getDefaultRenderer()
                    .getTableCellRendererComponent(myTable,
                            column.getIdentifier(), false, false, -1, col)
                    .getPreferredSize().getWidth();
            for (int row = 0; row < rowCount; row++) {
                int preferedWidth = (int) myTable
                        .getCellRenderer(row, col)
                        .getTableCellRendererComponent(myTable,
                                myTable.getValueAt(row, col), false, false,
                                row, col).getPreferredSize().getWidth();
                width = Math.max(width, preferedWidth);
            }
            header.setResizingColumn(column); // 此行很重要
            column.setWidth(width + myTable.getIntercellSpacing().width + 4);// 使表格看起来不是那么拥挤,起到间隔作用
        }
    }
 
源代码11 项目: marathonv5   文件: JTableJavaElement.java
@Override
public boolean marathon_select(String text) {
	JTable table = (JTable) component;
	boolean cellEditing = table.isEditing();
	if (cellEditing) {
		return true;
	}
	if ("".equals(text)) {
		table.clearSelection();
		return true;
	}
	int[] rows;
	int[] cols;
	if ("all".equals(text)) {
		int rowCount = table.getRowCount();
		int columnCount = table.getColumnCount();
		rows = new int[rowCount];
		cols = new int[columnCount];
		for (int i = 0; i < rowCount; i++) {
			rows[i] = i;
		}
		for (int i = 0; i < columnCount; i++) {
			cols[i] = i;
		}
	} else {
		rows = parseRows(text);
		String[] colNames = parseCols(text);
		cols = new int[colNames.length];
		for (int i = 0; i < colNames.length; i++) {
			cols[i] = getColumnIndex(colNames[i]);
		}
	}

	return selectRowsColumns(table, rows, cols);
}
 
源代码12 项目: freeinternals   文件: JDialogPlugins.java
private 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 + 10);
    }
}
 
源代码13 项目: Zettelkasten   文件: ZettelkastenViewUtil.java
/**
 * This method updates a jTable and a possible linked list which holds
 * filtered values from the jTables, by increasing ({@code diff} must be 1)
 * or decreasing ({@code diff} must be -1) an entry's occurences or
 * frequencies from the tablemodel and the linked list.
 * <br><br>
 * If no increase or decrease of frequencies (occurences) is requested, but
 * a complete removal, call
 * {@link #updateTableFrequencyRemove(javax.swing.JTable, java.util.LinkedList) updateTableFrequencyRemove(javax.swing.JTable, java.util.LinkedList)}
 * instead.
 *
 * @param table the table were we have to add a new value with frequency
 * @param list the possible linked list were we have to add a new value with
 * frequency
 * @param value the new value, for instance the author-string or
 * keyword-value
 * @param diff either +1, if a value was added, so frequency is increased by
 * 1. or -1, if a value was removed, so frequency is decreaded.
 * @return an updated linked list that was passed as parameter {@code list}
 */
public static LinkedList<Object[]> updateTableFrequencyChange(JTable table, LinkedList<Object[]> list, String value, int diff) {
    // iterate all table rows
    for (int cnt = 0; cnt < table.getRowCount(); cnt++) {
        // check whether we have found the value that should be changed
        if (value.equals(table.getValueAt(cnt, 0).toString())) {
            // retrieve table data
            Object[] o = new Object[2];
            o[0] = table.getValueAt(cnt, 0);
            o[1] = table.getValueAt(cnt, 1);
            // convert frquency-counter to int
            int freq = Integer.parseInt(table.getValueAt(cnt, 1).toString());
            // set new value
            table.setValueAt(freq + diff, cnt, 1);
            // check whether we have a filtered list
            if (list != null) {
                // if so, iterate list
                for (int pos = 0; pos < list.size(); pos++) {
                    Object[] v = list.get(pos);
                    // check whether we have found the value that should be changed
                    if (o[0].toString().equals(v[0].toString())) {
                        // change frequency
                        o[1] = freq + diff;
                        list.set(pos, o);
                        break;
                    }
                }
            }
        }
    }
    return list;
}
 
源代码14 项目: triplea   文件: BattleDisplay.java
/** Shorten columns with no units. */
private static void setDefaultWidths(final JTable table) {
  for (int column = 0; column < table.getColumnCount(); column++) {
    boolean hasData = false;
    for (int row = 0; row < table.getRowCount(); row++) {
      hasData |= (table.getValueAt(row, column) != TableData.NULL);
    }
    if (!hasData) {
      table.getColumnModel().getColumn(column).setPreferredWidth(8);
    }
  }
}
 
源代码15 项目: Zettelkasten   文件: TableUtils.java
/**
 * This method selects the first entry in the JTable {@code table} that
 * start with the text that is entered in the filter-textfield
 * {@code textfield}.
 *
 * @param table the jTable where the item should be selected
 * @param textfield the related filtertextfield that contains the user-input
 * @param column the column where the filtering-comparison should be applied
 * to. in most cases, the relevant information (i.e. the string/text) is in
 * column 0, but sometimes also in column 1
 */
public static void selectByTyping(JTable table, javax.swing.JTextField textfield, int column) {
    String text = textfield.getText().toLowerCase();
    for (int cnt = 0; cnt < table.getRowCount(); cnt++) {
        String val = table.getValueAt(cnt, column).toString();
        if (val.toLowerCase().startsWith(text)) {
            table.getSelectionModel().setSelectionInterval(cnt, cnt);
            table.scrollRectToVisible(table.getCellRect(cnt, column, false));
            // and leave method
            return;
        }
    }
}
 
源代码16 项目: mars-sim   文件: TableTab.java
public void adjustColumnPreferredWidths(JTable table) {
	// Gets max width for cells in column as the preferred width
	TableColumnModel columnModel = table.getColumnModel();
	for (int col = 0; col < table.getColumnCount(); col++) {
		int width = 45;
		for (int row = 0; row < table.getRowCount(); row++) {
			if (tableCellRenderer == null)
				tableCellRenderer = table.getCellRenderer(row, col);
			Component comp = table.prepareRenderer(tableCellRenderer, row, col);
			width = Math.max(comp.getPreferredSize().width, width);
		}
		columnModel.getColumn(col).setPreferredWidth(width);
	}
}
 
public static Properties getPropertiesFromTable(Properties x, JTable table) {
    JtableUtils.stopEditing(table);
    int rowcount = table.getRowCount();
    for (int i = 0; i < rowcount; i++) {
        String prop = getString(table.getValueAt(i, 0));
        String val = getString(table.getValueAt(i, 1));
        if (!prop.isEmpty()) {
            x.setProperty(prop, val);
        }
    }
    return x;
}
 
源代码18 项目: Repeat   文件: SwingUtil.java
public static void setRowNumber(JTable table, int numberOfRow) {
	ensureRowNumber(table, numberOfRow);
	// Now table has at least numberOfRow. We make sure table does not have any extra row
	int toDelete = table.getRowCount() - numberOfRow;
	removeLastRowTable(table, toDelete);
}
 
源代码19 项目: open-ig   文件: GUIUtils.java
/**
	 * Resizes the table columns based on the column and data preferred widths.
	 * @param table the original table
	 * @param model the data model
	 * @return the table itself
	 */
    public static JTable autoResizeColWidth(JTable table, AbstractTableModel model) {
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.setModel(model);
 
        int margin = 5;
 
        for (int i = 0; i < table.getColumnCount(); i++) {
            int                     vColIndex = i;
            DefaultTableColumnModel colModel  = (DefaultTableColumnModel) table.getColumnModel();
            TableColumn             col       = colModel.getColumn(vColIndex);
            int                     width;
 
            // Get width of column header
            TableCellRenderer renderer = col.getHeaderRenderer();
 
            if (renderer == null) {
                renderer = table.getTableHeader().getDefaultRenderer();
            }
 
            Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
 
            width = comp.getPreferredSize().width;
 
            // Get maximum width of column data
            for (int r = 0; r < table.getRowCount(); r++) {
                renderer = table.getCellRenderer(r, vColIndex);
                comp     = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false,
                        r, vColIndex);
                width = Math.max(width, comp.getPreferredSize().width);
            }
 
            // Add margin
            width += 2 * margin;
 
            // Set the width
            col.setPreferredWidth(width);
        }
 
        ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(
            SwingConstants.LEFT);
 
        // table.setAutoCreateRowSorter(true);
//        table.getTableHeader().setReorderingAllowed(false);
 
//        for (int i = 0; i < table.getColumnCount(); i++) {
//            TableColumn column = table.getColumnModel().getColumn(i);
// 
//            column.setCellRenderer(new DefaultTableColour());
//        }
 
        return table;
    }
 
源代码20 项目: CodenameOne   文件: TableHelper.java
public static int getLastVisibleRow(JTable p_Table) {
    Point p = p_Table.getVisibleRect().getLocation();
    p.y = p.y + p_Table.getVisibleRect().height - 1;
    int result = p_Table.rowAtPoint(p);
    if (result > 0)
      return result;

    // if there is no rows at this point,rowatpoint() return -1,
    // It means that there is not enough rows to fill the rectangle where
    // the table is displayed.
    // if this case we return getRowCount()-1 because
    // we are sure that the last row is visible
    if (p_Table.getVisibleRect().height > 0)
      return p_Table.getRowCount() - 1;
    else
      return -1;
  } 
 方法所在类
 同类方法