Example usage for javax.swing JTable getDefaultRenderer

List of usage examples for javax.swing JTable getDefaultRenderer

Introduction

In this page you can find the example usage for javax.swing JTable getDefaultRenderer.

Prototype

public TableCellRenderer getDefaultRenderer(Class<?> columnClass) 

Source Link

Document

Returns the cell renderer to be used when no renderer has been set in a TableColumn.

Usage

From source file:Main.java

public static void main(String[] args) {
    String[] headers = { "column 1", "column 2", "column 3", "column 4" };
    int cols = 4;
    int rows = 6;
    String[][] data = new String[rows][cols];
    for (int row = 0; row < rows; row++) {
        for (int col = 0; col < cols; col++) {
            data[row][col] = "item " + (row * cols + col + 1);
        }/*from   w  ww  . j ava2  s . c  o m*/
    }
    JTable table = new JTable(data, headers);
    DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) table.getDefaultRenderer(String.class);
    renderer.setHorizontalAlignment(JLabel.RIGHT);
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(table));
    f.setSize(400, 400);
    f.setLocation(200, 200);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) {
    DefaultTableModel model = new DefaultTableModel() {
        public Class getColumnClass(int columnIndex) {
            Object o = getValueAt(0, columnIndex);
            if (o == null) {
                return Object.class;
            } else {
                return o.getClass();
            }// w ww  .j  av  a 2  s  . c o m
        }
    };
    JTable table = new JTable(model);

    model.addColumn("Boolean", new Object[] { Boolean.TRUE });
    model.addColumn("Date", new Object[] { new Date() });
    model.addColumn("Double", new Object[] { new Double(Math.PI) });
    model.addColumn("Float", new Object[] { new Float(1.2) });
    model.addColumn("Icon", new Object[] { new ImageIcon("icon.gif") });
    model.addColumn("Number", new Object[] { new Integer(1) });
    model.addColumn("Object", new Object[] { "object" });

    Enumeration e = table.getColumnModel().getColumns();
    TableColumn col = (TableColumn) e.nextElement();

    col.setCellRenderer(table.getDefaultRenderer(Boolean.class));
    col.setCellEditor(table.getDefaultEditor(Boolean.class));

    JFrame f = new JFrame();
    f.setSize(300, 300);
    f.add(new JScrollPane(table));
    f.setVisible(true);
}

From source file:Main.java

public static void makeTransparant(JTable table, JScrollPane scrollPane) {
    table.setOpaque(false);/*  www  .j  a  v a 2s. c o  m*/
    ((DefaultTableCellRenderer) table.getDefaultRenderer(Object.class)).setOpaque(false);
    ((DefaultTableCellRenderer) table.getDefaultRenderer(String.class)).setOpaque(false);
    ((JComponent) table.getDefaultRenderer(Boolean.class)).setOpaque(false);

    scrollPane.setOpaque(false);
    scrollPane.getViewport().setOpaque(false);
}

From source file:Main.java

/**
 * Auto fit the column of a table./*from w  w w  .j a v a2s.  co m*/
 * @param table the table for which to auto fit the columns.
 * @param columnIndex the index of the column to auto fit.
 * @param maxWidth the maximum width that a column can take (like Integer.MAX_WIDTH).
 */
public static void autoFitTableColumn(JTable table, int columnIndex, int maxWidth) {
    TableModel model = table.getModel();
    TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();
    int rowCount = table.getRowCount();
    for (int i = columnIndex >= 0 ? columnIndex : model.getColumnCount() - 1; i >= 0; i--) {
        TableColumn column = table.getColumnModel().getColumn(i);
        int headerWidth = headerRenderer
                .getTableCellRendererComponent(table, column.getHeaderValue(), false, false, 0, 0)
                .getPreferredSize().width;
        int cellWidth = 0;
        for (int j = 0; j < rowCount; j++) {
            Component comp = table.getDefaultRenderer(model.getColumnClass(i))
                    .getTableCellRendererComponent(table, table.getValueAt(j, i), false, false, 0, i);
            int preferredWidth = comp.getPreferredSize().width;
            // Artificial space to look nicer.
            preferredWidth += 10;
            cellWidth = Math.max(cellWidth, preferredWidth);
        }
        // Artificial space for the sort icon.
        headerWidth += 20;
        column.setPreferredWidth(Math.min(Math.max(headerWidth, cellWidth) + table.getRowMargin(), maxWidth));
        if (columnIndex >= 0) {
            break;
        }
    }
}

From source file:Main.java

/**
 * Adjusts the widths and heights of the cells of the supplied table to fit their contents.
 *//*from  www.  j av a 2s  . c  om*/
public static void sizeToContents(JTable table) {
    TableModel model = table.getModel();
    TableColumn column = null;
    Component comp = null;
    int ccount = table.getColumnModel().getColumnCount(), rcount = model.getRowCount(), cellHeight = 0;

    for (int cc = 0; cc < ccount; cc++) {
        int headerWidth = 0, cellWidth = 0;
        column = table.getColumnModel().getColumn(cc);
        try {
            comp = column.getHeaderRenderer().getTableCellRendererComponent(null, column.getHeaderValue(),
                    false, false, 0, 0);
            headerWidth = comp.getPreferredSize().width;
        } catch (NullPointerException e) {
            // getHeaderRenderer() this doesn't work in 1.3
        }

        for (int rr = 0; rr < rcount; rr++) {
            Object cellValue = model.getValueAt(rr, cc);
            comp = table.getDefaultRenderer(model.getColumnClass(cc)).getTableCellRendererComponent(table,
                    cellValue, false, false, 0, cc);
            Dimension psize = comp.getPreferredSize();
            cellWidth = Math.max(psize.width, cellWidth);
            cellHeight = Math.max(psize.height, cellHeight);
        }
        column.setPreferredWidth(Math.max(headerWidth, cellWidth));
    }

    if (cellHeight > 0) {
        table.setRowHeight(cellHeight);
    }
}

From source file:TableRenderDemo.java

private void initColumnSizes(JTable table) {
    MyTableModel model = (MyTableModel) table.getModel();
    TableColumn column = null;/*from w w  w.  j  a  v a  2s. com*/
    Component comp = null;
    int headerWidth = 0;
    int cellWidth = 0;
    Object[] longValues = model.longValues;
    TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();

    for (int i = 0; i < 5; i++) {
        column = table.getColumnModel().getColumn(i);

        comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0);
        headerWidth = comp.getPreferredSize().width;

        comp = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(table,
                longValues[i], false, false, 0, i);
        cellWidth = comp.getPreferredSize().width;

        if (DEBUG) {
            System.out.println("Initializing width of column " + i + ". " + "headerWidth = " + headerWidth
                    + "; cellWidth = " + cellWidth);
        }

        // XXX: Before Swing 1.1 Beta 2, use setMinWidth instead.
        column.setPreferredWidth(Math.max(headerWidth, cellWidth));
    }
}

From source file:components.TableRenderDemo.java

private void initColumnSizes(JTable table) {
    MyTableModel model = (MyTableModel) table.getModel();
    TableColumn column = null;/*www . j a  va  2 s . c  o m*/
    Component comp = null;
    int headerWidth = 0;
    int cellWidth = 0;
    Object[] longValues = model.longValues;
    TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();

    for (int i = 0; i < 5; i++) {
        column = table.getColumnModel().getColumn(i);

        comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0);
        headerWidth = comp.getPreferredSize().width;

        comp = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(table,
                longValues[i], false, false, 0, i);
        cellWidth = comp.getPreferredSize().width;

        if (DEBUG) {
            System.out.println("Initializing width of column " + i + ". " + "headerWidth = " + headerWidth
                    + "; cellWidth = " + cellWidth);
        }

        column.setPreferredWidth(Math.max(headerWidth, cellWidth));
    }
}

From source file:TableCellRenderTest.java

public TableCellRenderFrame() {
    setTitle("TableCellRenderTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    TableModel model = new PlanetTableModel();
    JTable table = new JTable(model);
    table.setRowSelectionAllowed(false);

    // set up renderers and editors

    table.setDefaultRenderer(Color.class, new ColorTableCellRenderer());
    table.setDefaultEditor(Color.class, new ColorTableCellEditor());

    JComboBox moonCombo = new JComboBox();
    for (int i = 0; i <= 20; i++)
        moonCombo.addItem(i);/*from   w  w w.  ja v a2  s  .  c  o m*/

    TableColumnModel columnModel = table.getColumnModel();
    TableColumn moonColumn = columnModel.getColumn(PlanetTableModel.MOONS_COLUMN);
    moonColumn.setCellEditor(new DefaultCellEditor(moonCombo));
    moonColumn.setHeaderRenderer(table.getDefaultRenderer(ImageIcon.class));
    moonColumn.setHeaderValue(new ImageIcon("Moons.gif"));

    // show table

    table.setRowHeight(100);
    add(new JScrollPane(table), BorderLayout.CENTER);
}

From source file:com.vgi.mafscaling.VECalc.java

protected void formatTable(JTable table) {
    if (table == corrCountTable) {
        Format[][] formatMatrix = { { new DecimalFormat("#"), new DecimalFormat("0.00") },
                { new DecimalFormat("#"), new DecimalFormat("#") } };
        NumberFormatRenderer renderer = (NumberFormatRenderer) table.getDefaultRenderer(Object.class);
        renderer.setFormats(formatMatrix);
    } else {//  www  .j a va 2 s.c o  m
        Format[][] formatMatrix = { { new DecimalFormat("#"), new DecimalFormat("0.00") } };
        NumberFormatRenderer renderer = (NumberFormatRenderer) table.getDefaultRenderer(Object.class);
        renderer.setFormats(formatMatrix);
    }
}

From source file:org.jdal.swing.table.AnnotationFormatTableCellRenderer.java

/**
 * {@inheritDoc}/*  www .  java2s.  c  o m*/
 */
@SuppressWarnings("unchecked")
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {

    Class<?> clazz = table.getColumnClass(column);
    if (Number.class.isAssignableFrom(clazz)) {
        TableModel tableModel = table.getModel();
        if (tableModel instanceof ListTableModel) {
            ListTableModel listTableModel = (ListTableModel) tableModel;
            Class<?> modelClass = ((ListTableModel) tableModel).getModelClass();
            String propertyName = listTableModel.getPropertyName(column);
            Printer<Object> printer = (Printer<Object>) FormatUtils.getPrinter(modelClass, propertyName);
            if (printer != null) {
                value = printer.print(value, Locale.getDefault());
                JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
                        row, column);
                label.setHorizontalAlignment(JLabel.RIGHT);
                return label;
            }
        }
    }
    TableCellRenderer renderer = table.getDefaultRenderer(clazz);

    return renderer != null
            ? renderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column)
            : null;
}