Java Code Examples for javax.swing.JTable#getRowCount()

The following examples show how to use javax.swing.JTable#getRowCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EquipInfoTab.java    From pcgen with GNU Lesser General Public License v2.1 6 votes vote down vote up
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());
}
 
Example 2
Source File: FlatTableCellBorder.java    From FlatLaf with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: ListView.java    From desktopclient-java with GNU General Public License v3.0 6 votes vote down vote up
@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;
}
 
Example 4
Source File: SettingsUI.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 5
Source File: SwingUtil.java    From Repeat with Apache License 2.0 6 votes vote down vote up
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;
	}
}
 
Example 6
Source File: FmtImports.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@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);            
}
 
Example 7
Source File: JDialogPlugins.java    From freeinternals with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 8
Source File: PropUtils.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: TableTab.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
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);
	}
}
 
Example 10
Source File: TableUtils.java    From Zettelkasten with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
        }
    }
}
 
Example 11
Source File: BattleDisplay.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
/** 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);
    }
  }
}
 
Example 12
Source File: ZettelkastenViewUtil.java    From Zettelkasten with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 13
Source File: MapDemo.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
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("--------------------------");
}
 
Example 14
Source File: JTableJavaElement.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 15
Source File: Utils.java    From Method_Trace_Tool with Apache License 2.0 5 votes vote down vote up
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);// 使表格看起来不是那么拥挤,起到间隔作用
        }
    }
 
Example 16
Source File: SettingsUI.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 17
Source File: GuiUtil.java    From CQL with GNU Affero General Public License v3.0 5 votes vote down vote up
@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;

}
 
Example 18
Source File: GUIUtils.java    From open-ig with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
	 * 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;
    }
 
Example 19
Source File: SwingUtil.java    From Repeat with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 20
Source File: TableHelper.java    From CodenameOne with GNU General Public License v2.0 votes vote down vote up
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;
  }