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

The following examples show how to use javax.swing.JTable#prepareRenderer() . 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: LipidDatabaseTableDialog.java    From mzmine2 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * This method resizes the columns
 */
private void resizeColumnWidth(JTable table) {
  final TableColumnModel columnModel = table.getColumnModel();
  for (int column = 0; column < table.getColumnCount(); column++) {
    Object headerValue = columnModel.getColumn(column).getHeaderValue();
    TableCellRenderer headerRenderer = columnModel.getColumn(column).getHeaderRenderer();
    if (headerRenderer == null) {
      headerRenderer = table.getTableHeader().getDefaultRenderer();
    }
    Component headerComp =
        headerRenderer.getTableCellRendererComponent(table, headerValue, false, false, 0, column);
    int width = 30; // Min width
    width = Math.max(width, headerComp.getPreferredSize().width + 10);
    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 + 10, width);
    }
    columnModel.getColumn(column).setPreferredWidth(width);
  }
}
 
Example 2
Source File: MoveChooserService.java    From Shuffle-Move with GNU General Public License v3.0 6 votes vote down vote up
/**
 * From Stackoverflow: http://stackoverflow.com/a/17627497
 * 
 * @param table
 *           The table to be resized
 */
public void resizeColumnWidth(JTable table) {
   table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
   final TableColumnModel columnModel = table.getColumnModel();
   for (int column = 0; column < table.getColumnCount(); column++) {
      TableColumn tableColumn = columnModel.getColumn(column);
      TableCellRenderer r = tableColumn.getHeaderRenderer();
      if (r == null) {
         r = table.getTableHeader().getDefaultRenderer();
      }
      Component component = r.getTableCellRendererComponent(table, tableColumn.getHeaderValue(), false, false, 0,
            column);
      int width = component.getPreferredSize().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 + 1, width);
      }
      tableColumn.setPreferredWidth(width);
   }
}
 
Example 3
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 4
Source File: CoverageReportTopComponent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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]);
    }
}
 
Example 5
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 6
Source File: ItemListImageViewerEvent.java    From stendhal with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adjust the column widths of a table based on the table contents.
 *
 * @param table adjusted table
 */
private void adjustColumnWidths(JTable table) {
	TableColumnModel model = table.getColumnModel();
	for (int column = 0; column < table.getColumnCount(); column++) {
		TableColumn tc = model.getColumn(column);
		int width = tc.getWidth();
		for (int row = 0; row < table.getRowCount(); row++) {
			Component comp = table.prepareRenderer(table.getCellRenderer(row, column), row, column);
			width = Math.max(width, comp.getPreferredSize().width);
		}

		tc.setPreferredWidth(width);
	}
}
 
Example 7
Source File: ItemListImageViewerEvent.java    From stendhal with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adjust the row heights of a table based on the table contents.
 *
 * @param table adjusted table
 */
private void adjustRowHeights(JTable table) {
	for (int row = 0; row < table.getRowCount(); row++) {
		int rowHeight = table.getRowHeight();

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

		table.setRowHeight(row, rowHeight);
	}
}
 
Example 8
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 9
Source File: ShowUsagesAction.java    From dagger-intellij-plugin with Apache License 2.0 5 votes vote down vote up
private static int columnMaxWidth(@NotNull JTable table, int col) {
  TableColumn column = table.getColumnModel().getColumn(col);
  int width = 0;
  for (int row = 0; row < table.getRowCount(); row++) {
    Component component = table.prepareRenderer(column.getCellRenderer(), row, col);

    int rendererWidth = component.getPreferredSize().width;
    width = Math.max(width, rendererWidth + table.getIntercellSpacing().width);
  }
  return width;
}
 
Example 10
Source File: ShowUsagesAction.java    From otto-intellij-plugin with Apache License 2.0 5 votes vote down vote up
private static int columnMaxWidth(@NotNull JTable table, int col) {
  TableColumn column = table.getColumnModel().getColumn(col);
  int width = 0;
  for (int row = 0; row < table.getRowCount(); row++) {
    Component component = table.prepareRenderer(column.getCellRenderer(), row, col);

    int rendererWidth = component.getPreferredSize().width;
    width = Math.max(width, rendererWidth + table.getIntercellSpacing().width);
  }
  return width;
}
 
Example 11
Source File: ColumnResizer.java    From mars-sim with GNU General Public License v3.0 4 votes vote down vote up
public static 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++) {
            // System.out.println ("--- col " + col + " ---");
            int maxwidth = 40;
            //int minwidth = 40;
            int colWidth = 0;
            for (int row=0; row<table.getRowCount(); row++) {
                TableCellRenderer rend = table.getCellRenderer (row, col);
//                Object value = table.getValueAt (row, col);
//                Component comp =
//                    rend.getTableCellRendererComponent (table,
//                                                        value,
//                                                        false,
//                                                        false,
//                                                        row,
//                                                        col);

                Component comp = table.prepareRenderer(rend, row, col);
                colWidth = comp.getPreferredSize().width;
                //minwidth = colWidth;
                //if (colWidth > maxwidth) maxwidth = colWidth;
                //if (colWidth < minwidth) minwidth = colWidth;
                maxwidth = Math.max (colWidth, maxwidth);
                //minwidth = Math.max (colWidth, minwidth);

                //System.out.println ("col " + col +
                //                    " pref width now " +
                //                    maxwidth);
            }

            // Considers the column header's preferred width too
            TableColumn column = columnModel.getColumn (col);

//            int headerWidth = 0;
//            TableCellRenderer headerRenderer = column.getHeaderRenderer();
//            if (headerRenderer == null)
//                headerRenderer = table.getTableHeader().getDefaultRenderer();
//            Object headerValue = column.getHeaderValue();
//            Component headerComp =
//                    headerRenderer.getTableCellRendererComponent (table,
//                                                                  headerValue,
//                                                                  false,
//                                                                  false,
//                                                                  0,
//                                                                  col);
//            headerWidth = headerComp.getPreferredSize().width;
//
//            maxwidth = Math.max (colWidth, headerWidth);
//            minwidth = Math.min (colWidth, headerWidth);

            column.setPreferredWidth (maxwidth);
            //column.setMaxWidth(maxwidth); // very bad!
            column.setMinWidth(maxwidth);

        }
    }