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

The following examples show how to use javax.swing.JTable#getCellRenderer() . 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: AddressBookPanel.java    From zencash-swing-wallet-ui with MIT License 6 votes vote down vote up
private JScrollPane buildTablePanel() {
      table = new JTable(new AddressBookTableModel(),new DefaultTableColumnModel());
      TableColumn nameColumn = new TableColumn(0);
      TableColumn addressColumn = new TableColumn(1);
      table.addColumn(nameColumn);
      table.addColumn(addressColumn);
      table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // one at a time
      table.getSelectionModel().addListSelectionListener(new AddressListSelectionListener());
      table.addMouseListener(new AddressMouseListener());
      
      // TODO: isolate in utility
TableCellRenderer renderer = table.getCellRenderer(0, 0);
Component comp = renderer.getTableCellRendererComponent(table, "123", false, false, 0, 0);
table.setRowHeight(new Double(comp.getPreferredSize().getHeight()).intValue() + 2);
      
      JScrollPane scrollPane = new JScrollPane(table);
      return scrollPane;
  }
 
Example 2
Source File: SwingUtilities2.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Returns true if the given point is outside the preferredSize of the
 * item at the given row of the table.  (Column must be 0).
 * Does not check the "Table.isFileList" property. That should be checked
 * before calling this method.
 * This is used to make Windows {@literal L&F} JFileChooser act
 * like native dialogs.
 */
public static boolean pointOutsidePrefSize(JTable table, int row, int column, Point p) {
    if (table.convertColumnIndexToModel(column) != 0 || row == -1) {
        return true;
    }
    TableCellRenderer tcr = table.getCellRenderer(row, column);
    Object value = table.getValueAt(row, column);
    Component cell = tcr.getTableCellRendererComponent(table, value, false,
            false, row, column);
    Dimension itemSize = cell.getPreferredSize();
    Rectangle cellBounds = table.getCellRect(row, column, false);
    cellBounds.width = itemSize.width;
    cellBounds.height = itemSize.height;

    // See if coords are inside
    // ASSUME: mouse x,y will never be < cell's x,y
    assert (p.x >= cellBounds.x && p.y >= cellBounds.y);
    return p.x > cellBounds.x + cellBounds.width ||
            p.y > cellBounds.y + cellBounds.height;
}
 
Example 3
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 4
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 5
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 6
Source File: GridSwing.java    From evosql with Apache License 2.0 5 votes vote down vote up
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);
        }
    }
 
Example 7
Source File: MainView.java    From HiJson with Apache License 2.0 5 votes vote down vote up
private int widestCellInColumn(JTable table,TableColumn col) {
    int c = col.getModelIndex();
    int width = 0, maxw = 0;
    for (int r = 0; r < table.getRowCount(); r++) {
        TableCellRenderer renderer = table.getCellRenderer(r, c);
        Component comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, c), false, false, r, c);
        width = comp.getPreferredSize().width;
        maxw = width > maxw ? width : maxw;
    }
    if(maxw<90)maxw = 90;
    return maxw + 10;
}
 
Example 8
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 9
Source File: ColumnResizer.java    From beast-mcmc with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static void adjustColumnPreferredWidths(JTable table) {

		TableColumnModel columnModel = table.getColumnModel();

		for (int col = 0; col < table.getColumnCount(); col++) {

			int maxwidth = 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);
				maxwidth = Math.max(comp.getPreferredSize().width, maxwidth);

			}// END: row loop

			TableColumn column = columnModel.getColumn(col);
			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);
			maxwidth = Math.max(maxwidth, headerComp.getPreferredSize().width);
			column.setPreferredWidth(maxwidth);
		}// END: col loop
	}
 
Example 10
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 11
Source File: TableRendererTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testRenderers() {
    setUpModel();
    JTable t = ot.treeTable.getTable();
    ot.revalidate();

    javax.swing.JFrame f = new javax.swing.JFrame();
    f.setLayout(new BorderLayout());
    f.add(ot, BorderLayout.CENTER);
    f.setSize(600, 500);
    f.setVisible(true);
    //while (f.isVisible()) {
        try {
            Thread.sleep(333);
        } catch (InterruptedException ex) {
            Exceptions.printStackTrace(ex);
        }
    //}
    
    System.out.println("table rows = "+t.getRowCount());
    TableCellRenderer tcr = t.getCellRenderer(0, 0);
    Component c = tcr.getTableCellRendererComponent(t, null, true, true, 0, 0);
    //System.err.println("c = "+c);
    assertTrue("Renderer component = "+c, c instanceof RendererComponent);
    assertEquals("Renderer of 0:DN", ((RendererComponent) c).getText());

    tcr = t.getCellRenderer(0, 1);
    c = tcr.getTableCellRendererComponent(t, null, true, true, 0, 1);
    assertEquals("Renderer of 0:col1", ((RendererComponent) c).getText());

    tcr = t.getCellRenderer(0, 2);
    c = tcr.getTableCellRendererComponent(t, null, true, true, 0, 2);
    assertEquals("Renderer of 0:col2", ((RendererComponent) c).getText());

    tcr = t.getCellRenderer(1, 0);
    c = tcr.getTableCellRendererComponent(t, null, true, true, 1, 0);
    assertFalse("Renderer component = "+c, c instanceof RendererComponent);

    tcr = t.getCellRenderer(1, 1);
    c = tcr.getTableCellRendererComponent(t, null, true, true, 1, 1);
    assertFalse("Renderer component = "+c, c instanceof RendererComponent);

    tcr = t.getCellRenderer(2, 1);
    c = tcr.getTableCellRendererComponent(t, null, true, true, 2, 1);
    assertEquals("Renderer of 2:col1", ((RendererComponent) c).getText());

    tcr = t.getCellRenderer(6, 0);
    c = tcr.getTableCellRendererComponent(t, null, true, true, 6, 0);
    assertEquals("Renderer of 6:DN", ((RendererComponent) c).getText());

    tcr = t.getCellRenderer(8, 2);
    c = tcr.getTableCellRendererComponent(t, null, true, true, 8, 2);
    assertEquals("Renderer of 8:col2", ((RendererComponent) c).getText());
}
 
Example 12
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 13
Source File: AnimPlayList.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);
 
      return table;
  }
 
Example 14
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);

        }
    }