Example usage for javax.swing.table TableColumn setCellRenderer

List of usage examples for javax.swing.table TableColumn setCellRenderer

Introduction

In this page you can find the example usage for javax.swing.table TableColumn setCellRenderer.

Prototype

@BeanProperty(description = "The renderer to use for cell values.")
public void setCellRenderer(TableCellRenderer cellRenderer) 

Source Link

Document

Sets the TableCellRenderer used by JTable to draw individual values for this column.

Usage

From source file:au.com.jwatmuff.eventmanager.util.GUIUtils.java

public static void leftAlignTable(JTable table) {
    Enumeration<TableColumn> cols = table.getColumnModel().getColumns();
    while (cols.hasMoreElements()) {
        TableColumn col = cols.nextElement();
        final TableCellRenderer oldRenderer = col.getCellRenderer();
        if (oldRenderer == null) {
            col.setCellRenderer(new DefaultTableCellRenderer() {
                @Override//from w w  w  . jav a 2  s. c  o m
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                        boolean hasFocus, int row, int column) {
                    Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row,
                            column);
                    if (c instanceof JLabel) {
                        ((JLabel) c).setHorizontalAlignment(SwingConstants.LEFT);
                    }
                    return c;
                }
            });
        } else {
            col.setCellRenderer(new TableCellRenderer() {
                @Override
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                        boolean hasFocus, int row, int column) {
                    Component c = oldRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus,
                            row, column);
                    if (c instanceof JLabel) {
                        ((JLabel) c).setHorizontalAlignment(SwingConstants.LEFT);
                    }
                    return c;
                }
            });
        }
    }
}

From source file:OAT.ui.util.UiUtil.java

public static void setupColumns(JTable table) {
    for (int i = 0; i < table.getColumnCount(); i++) {
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        TableColumn column = table.getColumnModel().getColumn(i);
        Class columnClass = table.getColumnClass(i);
        String columnName = table.getColumnName(i);
        renderer.setToolTipText(columnName);

        if (columnClass.getSuperclass().equals(Number.class)) {
            renderer.setHorizontalAlignment(DefaultTableCellRenderer.RIGHT);
        }//from   www  . jav a 2  s .c  o m

        if (table.isEnabled()) {
            if (columnName.equals("Currency")) {
                //addComboCell(column, Product.CURRENCIES);
            } else if (columnName.equals("Holidays")) {
                //column.setCellEditor(new FileChooserCellEditor());
            }
        }

        column.setCellRenderer(renderer);
    }
}

From source file:GenderRenderer.java

public TableCellRendererJComboBox() {
    Container pane = getContentPane();
    pane.setLayout(new BorderLayout());
    TableValues tv = new TableValues();
    table = new JTable(tv);
    TableColumnModel tcm = table.getColumnModel();
    TableColumn tc = tcm.getColumn(TableValues.GENDER);
    tc.setCellRenderer(new GenderRenderer());
    JScrollPane jsp = new JScrollPane(table);
    pane.add(jsp, BorderLayout.CENTER);
}

From source file:TableIt.java

public TableIt() {
    JFrame f = new JFrame();
    TableModel tm = new MyTableModel();
    final JTable table = new JTable(tm);

    TableColumnModel tcm = table.getColumnModel();
    TableColumn column = tcm.getColumn(tcm.getColumnCount() - 1);
    TableCellRenderer renderer = new MyTableCellRenderer();
    column.setCellRenderer(renderer);

    JButton selectionType = new JButton("Next Type");
    selectionType.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            boolean rowSet = table.getRowSelectionAllowed();
            boolean colSet = table.getColumnSelectionAllowed();
            boolean cellSet = table.getCellSelectionEnabled();

            boolean setRow = !rowSet;
            boolean setCol = rowSet ^ colSet;
            boolean setCell = rowSet & colSet;

            table.setCellSelectionEnabled(setCell);
            table.setColumnSelectionAllowed(setCol);
            table.setRowSelectionAllowed(setRow);
            System.out.println("Row Selection Allowed? " + setRow);
            System.out.println("Column Selection Allowed? " + setCol);
            System.out.println("Cell Selection Enabled? " + setCell);
            table.repaint();/*from www  .  j a  v  a2  s.  c  o m*/
        }
    });
    JButton selectionMode = new JButton("Next Mode");
    selectionMode.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ListSelectionModel lsm = table.getSelectionModel();
            int mode = lsm.getSelectionMode();
            int nextMode;
            String nextModeString;
            if (mode == ListSelectionModel.SINGLE_SELECTION) {
                nextMode = ListSelectionModel.SINGLE_INTERVAL_SELECTION;
                nextModeString = "Single Interval Selection";
            } else if (mode == ListSelectionModel.SINGLE_INTERVAL_SELECTION) {
                nextMode = ListSelectionModel.MULTIPLE_INTERVAL_SELECTION;
                nextModeString = "Multiple Interval Selection";
            } else {
                nextMode = ListSelectionModel.SINGLE_SELECTION;
                nextModeString = "Single Selection";
            }
            lsm.setSelectionMode(nextMode);
            System.out.println("Selection Mode: " + nextModeString);
            table.repaint();
        }
    });
    JPanel jp = new JPanel();
    jp.add(selectionType);
    jp.add(selectionMode);
    JScrollPane jsp = new JScrollPane(table);
    Container c = f.getContentPane();
    c.add(jsp, BorderLayout.CENTER);
    c.add(jp, BorderLayout.SOUTH);
    f.setSize(300, 250);
    f.show();
}

From source file:PropertyTable.java

/**
 * This constructor method specifies what data the table will display (the
 * table model) and uses the TableColumnModel to customize the way that the
 * table displays it. The hard work is done by the TableModel implementation
 * below.//from   w  w  w .jav  a  2s  . c  om
 */
public PropertyTable(Class beanClass) {
    // Set the data model for this table
    try {
        setModel(new JavaBeanPropertyTableModel(beanClass));
    } catch (IntrospectionException e) {
        System.err.println("WARNING: can't introspect: " + beanClass);
    }

    // Tweak the appearance of the table by manipulating its column model
    TableColumnModel colmodel = getColumnModel();

    // Set column widths
    colmodel.getColumn(0).setPreferredWidth(125);
    colmodel.getColumn(1).setPreferredWidth(200);
    colmodel.getColumn(2).setPreferredWidth(75);
    colmodel.getColumn(3).setPreferredWidth(50);

    // Right justify the text in the first column
    TableColumn namecol = colmodel.getColumn(0);
    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    renderer.setHorizontalAlignment(SwingConstants.RIGHT);
    namecol.setCellRenderer(renderer);
}

From source file:DefaultsDisplay.java

protected JTable createDefaultsTable() {
    JTable table = new JTable(new UIDefaultsTableModel());
    table.setRowHeight(rowHeight);/*from  w  ww  .j  a  v a  2s .  c o m*/
    table.setShowHorizontalLines(false);
    table.setShowVerticalLines(false);
    table.setIntercellSpacing(new Dimension(0, 0));
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    initFilters(table);

    DefaultTableColumnModel columnModel = new DefaultTableColumnModel();

    Color rowColors[] = new Color[2];
    rowColors[0] = UIManager.getColor("Table.background");
    rowColors[1] = new Color((int) (rowColors[0].getRed() * .90), (int) (rowColors[0].getGreen() * .95),
            (int) (rowColors[0].getBlue() * .95));

    int width = 0;

    TableColumn column = new TableColumn();
    column.setCellRenderer(new KeyRenderer(rowColors));
    column.setModelIndex(UIDefaultsTableModel.KEY_COLUMN);
    column.setHeaderValue("Key");
    column.setPreferredWidth(250);
    columnModel.addColumn(column);
    width += column.getPreferredWidth();

    column = new TableColumn();
    column.setCellRenderer(new RowRenderer(rowColors));
    column.setModelIndex(UIDefaultsTableModel.TYPE_COLUMN);
    column.setHeaderValue("Type");
    column.setPreferredWidth(250);
    columnModel.addColumn(column);
    width += column.getPreferredWidth();

    column = new TableColumn();
    column.setCellRenderer(new ValueRenderer(rowColors));
    column.setModelIndex(UIDefaultsTableModel.VALUE_COLUMN);
    column.setHeaderValue("Value");
    column.setPreferredWidth(300);
    columnModel.addColumn(column);
    width += column.getPreferredWidth();

    table.setColumnModel(columnModel);

    table.setPreferredScrollableViewportSize(new Dimension(width, 12 * rowHeight));

    return table;

}

From source file:net.sf.profiler4j.console.ClassListPanel.java

/**
 * This method initializes classesTable/*from  w w w  .j  ava2s  .  c  o m*/
 * 
 * @return javax.swing.JTable
 */
private JTable getClassesTable() {
    if (classesTable == null) {
        classesTable = new JTable();
        classesTable.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        classesTable.setFont(new java.awt.Font("Tahoma", java.awt.Font.PLAIN, 12));
        classesTable.setModel(getClassListTableModel());
        classesTable.setRowHeight(20);
        classesTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                if (classesTable.getSelectedRowCount() > 0) {
                    addAsRuleButton.setEnabled(true);
                } else {
                    addAsRuleButton.setEnabled(false);
                }

            }
        });
        TableColumn c = classesTable.getColumnModel().getColumn(0);
        c.setMinWidth(50);
        c.setMaxWidth(50);

        c = classesTable.getColumnModel().getColumn(1);
        c.setMinWidth(300);
        c.setCellRenderer(new DefaultTableCellRenderer() {
            Font f1 = new Font("Tahoma", java.awt.Font.PLAIN, 12);
            Font f2 = new Font("Tahoma", java.awt.Font.BOLD, 12);

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                    boolean hasFocus, int row, int column) {
                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

                if (isSelected) {
                    setFont(f2);
                } else {
                    setFont(f1);
                }
                if (classListTableModel.getRow(row).info.isInstrumented()) {
                    if (isSelected) {
                        setForeground(Color.YELLOW);
                        setBackground(Color.BLUE);
                    } else {
                        setBackground(Color.decode("#bbffbb"));
                        setForeground(Color.BLACK);
                    }
                } else {
                    if (isSelected) {
                        setForeground(Color.WHITE);
                        setBackground(Color.BLUE);
                    } else {
                        setBackground(Color.WHITE);
                        setForeground(Color.BLACK);
                    }
                }
                setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 4));
                return this;
            }
        });

    }
    return classesTable;
}

From source file:medsavant.enrichment.app.OntologyAggregatePanel.java

@Override
public void recalculate() {
    if (chooser != null && chooser.getSelectedItem() != null) {
        // We only want to cancel the VariantFetcher.
        if (variantFetcher != null) {
            variantFetcher.cancel(true);
            variantFetcher = null;// ww  w .  ja v  a 2 s.  c o  m
        }

        // It's quite possible that we've successfully fetched all the terms, in which case the dead worker tells us that
        // we don't need to do it again.
        if (termFetcher == null) {
            tree.setModel(new OntologyTreeModel(null));

            termFetcher = new MedSavantWorker<OntologyTerm[]>(pageName) {
                @Override
                protected OntologyTerm[] doInBackground() throws Exception {
                    showProgress(0.0);
                    return MedSavantClient.OntologyManager.getAllTerms(
                            LoginController.getInstance().getSessionID(),
                            ((OntologyListItem) chooser.getSelectedItem()).getType());
                }

                @Override
                protected void showProgress(double fract) {
                    if (fract == 1.0) {
                        // Next stage will be indeterminate in duration.
                        progress.setIndeterminate(true);
                    } else {
                        if (fract == 0.0) {
                            progress.setIndeterminate(false);
                            startProgressTimer();
                        }

                        double prog = fract * TERMS_PERCENT;
                        progress.setValue((int) prog);
                        progress.setString(String.format("%.1f%%", prog));
                    }
                }

                @Override
                protected void showSuccess(OntologyTerm[] result) {
                    tree.setModel(new OntologyTreeModel(result));
                    TableColumn col = tree.getColumnModel().getColumn(3);
                    col.setCellRenderer(new NodeProgressRenderer());
                }

                @Override
                protected ProgressStatus checkProgress() throws RemoteException {
                    ProgressStatus stat;
                    try {
                        stat = MedSavantClient.OntologyManager
                                .checkProgress(LoginController.getInstance().getSessionID(), false);
                    } catch (SessionExpiredException ex) {
                        MedSavantExceptionHandler.handleSessionExpiredException(ex);
                        return null;
                    }
                    LOG.info("OntologyManager returned status " + stat);
                    return stat;
                }
            };

            termFetcher.execute();
        } else {
            // Already have our terms.  Just reset and start a new variant fetcher.
            if (tree.getModel() != null) {
                for (int i = 0; i < tree.getRowCount(); i++) {
                    OntologyNode rowNode = (OntologyNode) tree.getRowAt(i);
                    rowNode.resetCount();
                }
                OntologyTreeModel actualModel = (OntologyTreeModel) TableModelWrapperUtils
                        .getActualTableModel(tree.getModel());
                actualModel.geneCounts.clear();
                actualModel.fireTableDataChanged();
                variantFetcher = new VariantFetcher(actualModel);
                variantFetcher.execute();
            }
        }
    }
}

From source file:net.sf.keystore_explorer.gui.dialogs.extensions.DViewExtensions.java

private void initComponents() {
    ExtensionsTableModel extensionsTableModel = new ExtensionsTableModel();
    jtExtensions = new JKseTable(extensionsTableModel);

    TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(extensionsTableModel);
    sorter.setComparator(2, new ObjectIdComparator());
    jtExtensions.setRowSorter(sorter);/*from w  ww  .ja v  a 2s. c  om*/

    jtExtensions.setShowGrid(false);
    jtExtensions.setRowMargin(0);
    jtExtensions.getColumnModel().setColumnMargin(0);
    jtExtensions.getTableHeader().setReorderingAllowed(false);
    jtExtensions.setAutoResizeMode(JKseTable.AUTO_RESIZE_ALL_COLUMNS);
    jtExtensions.setRowHeight(Math.max(18, jtExtensions.getRowHeight()));

    for (int i = 0; i < jtExtensions.getColumnCount(); i++) {
        TableColumn column = jtExtensions.getColumnModel().getColumn(i);
        column.setHeaderRenderer(
                new ExtensionsTableHeadRend(jtExtensions.getTableHeader().getDefaultRenderer()));
        column.setCellRenderer(new ExtensionsTableCellRend());
    }

    TableColumn criticalCol = jtExtensions.getColumnModel().getColumn(0);
    criticalCol.setResizable(false);
    criticalCol.setMinWidth(28);
    criticalCol.setMaxWidth(28);
    criticalCol.setPreferredWidth(28);

    ListSelectionModel selectionModel = jtExtensions.getSelectionModel();
    selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    selectionModel.addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent evt) {
            if (!evt.getValueIsAdjusting()) {
                try {
                    CursorUtil.setCursorBusy(DViewExtensions.this);
                    updateExtensionValue();
                } finally {
                    CursorUtil.setCursorFree(DViewExtensions.this);
                }
            }
        }
    });

    jspExtensionsTable = PlatformUtil.createScrollPane(jtExtensions,
            ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    jspExtensionsTable.getViewport().setBackground(jtExtensions.getBackground());

    jpExtensionsTable = new JPanel(new BorderLayout(5, 5));
    jpExtensionsTable.setPreferredSize(new Dimension(500, 200));
    jpExtensionsTable.add(jspExtensionsTable, BorderLayout.CENTER);

    jpExtensionValue = new JPanel(new BorderLayout(5, 5));

    jlExtensionValue = new JLabel(res.getString("DViewExtensions.jlExtensionValue.text"));

    jpExtensionValue.add(jlExtensionValue, BorderLayout.NORTH);

    jepExtensionValue = new JEditorPane();
    jepExtensionValue.setFont(new Font(Font.MONOSPACED, Font.PLAIN, LnfUtil.getDefaultFontSize()));
    jepExtensionValue.setEditable(false);
    jepExtensionValue.setToolTipText(res.getString("DViewExtensions.jtaExtensionValue.tooltip"));
    // JGoodies - keep uneditable color same as editable
    jepExtensionValue.putClientProperty("JTextArea.infoBackground", Boolean.TRUE);

    // for displaying URLs in extensions as clickable links
    jepExtensionValue.setContentType("text/html");
    jepExtensionValue.addHyperlinkListener(this);
    // use default font and foreground color from the component
    jepExtensionValue.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);

    jspExtensionValue = PlatformUtil.createScrollPane(jepExtensionValue,
            ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    jpExtensionValueTextArea = new JPanel(new BorderLayout(5, 5));
    jpExtensionValueTextArea.setPreferredSize(new Dimension(500, 200));
    jpExtensionValueTextArea.add(jspExtensionValue, BorderLayout.CENTER);

    jpExtensionValue.add(jpExtensionValueTextArea, BorderLayout.CENTER);

    jbAsn1 = new JButton(res.getString("DViewExtensions.jbAsn1.text"));

    PlatformUtil.setMnemonic(jbAsn1, res.getString("DViewExtensions.jbAsn1.mnemonic").charAt(0));
    jbAsn1.setToolTipText(res.getString("DViewExtensions.jbAsn1.tooltip"));
    jbAsn1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            try {
                CursorUtil.setCursorBusy(DViewExtensions.this);
                asn1DumpPressed();
            } finally {
                CursorUtil.setCursorFree(DViewExtensions.this);
            }
        }
    });

    jpExtensionValueAsn1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    jpExtensionValueAsn1.add(jbAsn1);

    jpExtensionValue.add(jpExtensionValueAsn1, BorderLayout.SOUTH);

    jpExtensions = new JPanel(new GridLayout(2, 1, 5, 5));
    jpExtensions.setBorder(new CompoundBorder(new EmptyBorder(5, 5, 5, 5),
            new CompoundBorder(new EtchedBorder(), new EmptyBorder(5, 5, 5, 5))));

    jpExtensions.add(jpExtensionsTable);
    jpExtensions.add(jpExtensionValue);

    jbOK = new JButton(res.getString("DViewExtensions.jbOK.text"));
    jbOK.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            okPressed();
        }
    });

    jpOK = PlatformUtil.createDialogButtonPanel(jbOK, false);

    extensionsTableModel.load(extensions);

    if (extensionsTableModel.getRowCount() > 0) {
        jtExtensions.changeSelection(0, 0, false, false);
    }

    getContentPane().add(jpExtensions, BorderLayout.CENTER);
    getContentPane().add(jpOK, BorderLayout.SOUTH);

    setResizable(false);

    addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent evt) {
            closeDialog();
        }
    });

    getRootPane().setDefaultButton(jbOK);

    pack();

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            jbOK.requestFocus();
        }
    });
}

From source file:edu.ku.brc.af.ui.forms.formatters.DataObjFieldFormatMultiplePanel.java

/**
 * //  w  w w.  ja v  a2s .c o m
 */
private void setFormatSwitchTblColumnProperties() {
    // set details of 1st column (field values)
    TableColumnModel model = formatSwitchTbl.getColumnModel();
    TableColumn column = model.getColumn(model.getColumnIndex(FIELD_VALUE_COL));
    column.setMinWidth(20);
    column.setMaxWidth(300);
    column.setPreferredWidth(70);
    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    renderer.setHorizontalAlignment(SwingConstants.CENTER);
    column.setCellRenderer(renderer);

    // set details of 3rd column (ellipsis buttons)
    column = model.getColumn(model.getColumnIndex(ELLIPSIS_BUTTON_COL));
    column.setCellRenderer(new EditDataObjFormatButtonRenderer());
    column.setCellEditor(new EditDataObjFormatButtonEditor(createCheckBox()));
    column.setMinWidth(20);
    column.setMaxWidth(20);
    column.setPreferredWidth(20);
}