Example usage for javax.swing.table DefaultTableCellRenderer DefaultTableCellRenderer

List of usage examples for javax.swing.table DefaultTableCellRenderer DefaultTableCellRenderer

Introduction

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

Prototype

public DefaultTableCellRenderer() 

Source Link

Document

Creates a default table cell renderer.

Usage

From source file:MainClass.java

public static void main(String args[]) {
    String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
    String headers[] = { "Upper", "Lower" };

    JFrame frame = new JFrame("Tooltip Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTable table = new JTable(rows, headers);

    JLabel headerRenderer = new DefaultTableCellRenderer();
    String columnName = table.getModel().getColumnName(0);
    headerRenderer.setText(columnName);//w w w. jav a 2  s  .co m
    headerRenderer.setToolTipText("Wave");

    TableColumnModel columnModel = table.getColumnModel();
    TableColumn column = columnModel.getColumn(0);
    column.setHeaderRenderer((TableCellRenderer) headerRenderer);

    JScrollPane scrollPane = new JScrollPane(table);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:LabelHeaderSample.java

public static void main(String args[]) {
    final Object rows[][] = { { "one", "1" }, { "two", "2" }, { "three", "3" } };
    JFrame frame = new JFrame("Label Header");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    String headers[] = { "English", "#" };
    JTable table = new JTable(rows, headers);
    JScrollPane scrollPane = new JScrollPane(table);

    JLabel headerRenderer = new DefaultTableCellRenderer();
    String columnName = table.getModel().getColumnName(0);
    headerRenderer.setText(columnName);// www .j  a v a 2s  .  c  o  m
    headerRenderer.setToolTipText("text");
    TableColumnModel columnModel = table.getColumnModel();
    TableColumn englishColumn = columnModel.getColumn(0);
    englishColumn.setHeaderRenderer((TableCellRenderer) headerRenderer);

    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:SwingToolTipSample.java

public static void main(String args[]) {
    Object rows[][] = { { "one", "ichi - \u4E00" }, { "two", "ni - \u4E8C" }, { "three", "san - \u4E09" },
            { "four", "shi - \u56DB" }, { "five", "go - \u4E94" }, { "six", "roku - \u516D" },
            { "seven", "shichi - \u4E03" }, { "eight", "hachi - \u516B" }, { "nine", "kyu - \u4E5D" },
            { "ten", "ju - \u5341" } };
    Object headers[] = { "English", "Japanese" };
    JFrame frame = new JFrame("Tooltip Sample");
    JTable table = new JTable(rows, headers);

    JLabel headerRenderer = new DefaultTableCellRenderer();
    String columnName = table.getModel().getColumnName(0);
    headerRenderer.setText(columnName);/*from  w  w  w .j a  v  a 2s  .c o  m*/
    headerRenderer.setToolTipText("Wave");
    TableColumnModel columnModel = table.getColumnModel();
    TableColumn englishColumn = columnModel.getColumn(0);
    englishColumn.setHeaderRenderer((TableCellRenderer) headerRenderer);

    JScrollPane scrollPane = new JScrollPane(table);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:Main.java

public static void setTableColumnsAlignment(int[] colAlignments, JTable table) {
    for (int i = 0; i < table.getColumnCount(); i++) {
        if (colAlignments[i] == JLabel.LEFT)
            continue;

        TableColumn col = table.getColumn(table.getColumnName(i));
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setHorizontalAlignment(colAlignments[i]);
        col.setCellRenderer(renderer);//from   w  w  w .  j  a  v a  2  s .co  m
    }
}

From source file:Main.java

public static void SetJTableAlignment(javax.swing.JTable jTable, ComponentOrientation componentOrientation) {
    Font tahoma = new Font("Tahoma", Font.PLAIN, 11);
    int labelAlighnment = JLabel.RIGHT;
    int headerAlighnment = JLabel.RIGHT;
    if (componentOrientation == ComponentOrientation.LEFT_TO_RIGHT) {
        labelAlighnment = JLabel.LEFT;
        headerAlighnment = JLabel.LEFT;
    }/* w  w  w  .  java 2 s.c o m*/

    DefaultTableCellRenderer defaultTableCellRenderer = new DefaultTableCellRenderer();
    defaultTableCellRenderer.setHorizontalAlignment(labelAlighnment);
    // defaultTableCellRenderer.setFont(tahoma);

    for (int columnIndex = 0; columnIndex < jTable.getColumnCount(); columnIndex++) {
        if (jTable.getModel().getColumnClass(columnIndex).equals(Boolean.class)) {
            jTable.getColumnModel().getColumn(columnIndex).setWidth(60);
            jTable.getColumnModel().getColumn(columnIndex).setMaxWidth(90);
            jTable.getColumnModel().getColumn(columnIndex).setMinWidth(10);
            jTable.getColumnModel().getColumn(columnIndex).setPreferredWidth(60);
            continue;
        }
        if (jTable.getModel().getColumnClass(columnIndex).equals(ImageIcon.class)) {
            continue;
        }
        jTable.getColumnModel().getColumn(columnIndex).setCellRenderer(defaultTableCellRenderer);

    }
    DefaultTableCellRenderer renderer;
    renderer = (DefaultTableCellRenderer) jTable.getTableHeader().getDefaultRenderer();
    renderer.setHorizontalAlignment(headerAlighnment);

    jTable.getTableHeader().setDefaultRenderer(renderer);

}

From source file:Main.java

private static JPanel createPanel() {
    JPanel panel = new JPanel();
    DefaultTableModel model = new DefaultTableModel() {
        @Override//from  w ww .ja v a 2  s  .c  om
        public Class<?> getColumnClass(int col) {
            if (col == 0) {
                return Icon.class;
            } else {
                return Double.class;
            }
        }
    };
    model.setColumnIdentifiers(new Object[] { "Book", "Cost" });
    for (int i = 0; i < 42; i++) {
        model.addRow(new Object[] { ICON, Double.valueOf(i) });
    }
    JTable table = new JTable(model);
    table.setDefaultRenderer(Double.class, new DefaultTableCellRenderer() {
        @Override
        protected void setValue(Object value) {
            NumberFormat format = NumberFormat.getCurrencyInstance();
            setText((value == null) ? "" : format.format(value));
        }
    });
    table.setRowHeight(ICON.getIconHeight());
    panel.add(new JScrollPane(table) {
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(320, 240);
        }
    });
    return panel;
}

From source file:com.stam.batchmove.BatchMoveUtils.java

public static void showFilesFrame(Object[][] data, String[] columnNames, final JFrame callerFrame) {
    final FilesFrame filesFrame = new FilesFrame();

    DefaultTableModel model = new DefaultTableModel(data, columnNames) {

        private static final long serialVersionUID = 1L;

        @Override//w ww. j  a  v  a 2  s  .  com
        public Class<?> getColumnClass(int column) {
            return getValueAt(0, column).getClass();
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return column == 0;
        }
    };
    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    renderer.setHorizontalAlignment(JLabel.CENTER);
    final JTable table = new JTable(model);
    for (int i = 1; i < table.getColumnCount(); i++) {
        table.setDefaultRenderer(table.getColumnClass(i), renderer);
    }
    //            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.setRowHeight(30);
    table.getTableHeader().setFont(new Font("Serif", Font.BOLD, 14));
    table.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
    table.setRowSelectionAllowed(false);
    table.getColumnModel().getColumn(0).setMaxWidth(35);
    table.getColumnModel().getColumn(1).setPreferredWidth(350);
    table.getColumnModel().getColumn(2).setPreferredWidth(90);
    table.getColumnModel().getColumn(2).setMaxWidth(140);
    table.getColumnModel().getColumn(3).setMaxWidth(90);

    JPanel tblPanel = new JPanel();
    JPanel btnPanel = new JPanel();

    tblPanel.setLayout(new BorderLayout());
    if (table.getRowCount() > 15) {
        JScrollPane scrollPane = new JScrollPane(table);
        tblPanel.add(scrollPane, BorderLayout.CENTER);
    } else {
        tblPanel.add(table.getTableHeader(), BorderLayout.NORTH);
        tblPanel.add(table, BorderLayout.CENTER);
    }

    btnPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

    filesFrame.setMinimumSize(new Dimension(800, 600));
    filesFrame.setLayout(new BorderLayout());
    filesFrame.add(tblPanel, BorderLayout.NORTH);
    filesFrame.add(btnPanel, BorderLayout.SOUTH);

    final JLabel resultsLabel = new JLabel();

    JButton cancelBtn = new JButton("Cancel");
    cancelBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            filesFrame.setVisible(false);
            callerFrame.setVisible(true);
        }
    });

    JButton moveBtn = new JButton("Copy");
    moveBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            fileChooser.setDialogTitle("Choose target directory");
            int selVal = fileChooser.showOpenDialog(null);
            if (selVal == JFileChooser.APPROVE_OPTION) {
                File selection = fileChooser.getSelectedFile();
                String targetPath = selection.getAbsolutePath();

                DefaultTableModel dtm = (DefaultTableModel) table.getModel();
                int nRow = dtm.getRowCount();
                int copied = 0;
                for (int i = 0; i < nRow; i++) {
                    Boolean selected = (Boolean) dtm.getValueAt(i, 0);
                    String filePath = dtm.getValueAt(i, 1).toString();

                    if (selected) {
                        try {
                            FileUtils.copyFileToDirectory(new File(filePath), new File(targetPath));
                            dtm.setValueAt("Copied", i, 3);
                            copied++;
                        } catch (Exception ex) {
                            Logger.getLogger(SelectionFrame.class.getName()).log(Level.SEVERE, null, ex);
                            dtm.setValueAt("Failed", i, 3);
                        }
                    }
                }
                resultsLabel.setText(copied + " files copied. Finished!");
            }
        }
    });
    btnPanel.add(cancelBtn);
    btnPanel.add(moveBtn);
    btnPanel.add(resultsLabel);

    filesFrame.revalidate();
    filesFrame.setVisible(true);

    callerFrame.setVisible(false);
}

From source file:Main.java

/**
 * Setups the given table for usage as row-header. This method setups the background color to
 * the same one than the column headers.
 *
 * {@note In a previous version, we were assigning to the row headers the same cell renderer than
 *        the one created by <cite>Swing</cite> for the column headers. But it produced strange
 *        effects when the L&F uses a vertical grandiant instead than a uniform color.}
 *
 * @param  table The table to setup as row headers.
 * @return The renderer which has been assigned to the table.
 *//*  w  ww  .  j  a v  a2 s .  c o  m*/
public static TableCellRenderer setupAsRowHeader(final JTable table) {
    final JTableHeader header = table.getTableHeader();
    Color background = header.getBackground();
    Color foreground = header.getForeground();
    if (background == null || background.equals(table.getBackground())) {
        if (!SystemColor.control.equals(background)) {
            background = SystemColor.control;
            foreground = SystemColor.controlText;
        } else {
            final Locale locale = table.getLocale();
            background = UIManager.getColor("Label.background", locale);
            foreground = UIManager.getColor("Label.foreground", locale);
        }
    }
    final DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    renderer.setBackground(background);
    renderer.setForeground(foreground);
    renderer.setHorizontalAlignment(DefaultTableCellRenderer.RIGHT);
    final TableColumn column = table.getColumnModel().getColumn(0);
    column.setCellRenderer(renderer);
    column.setPreferredWidth(60);
    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.setCellSelectionEnabled(false);
    return renderer;
}

From source file:ExpenseReport.java

public ExpenseReport() {
    super("Expense Report");
    setSize(570, 200);//from w  w  w  . ja va2 s .com

    m_data = new ExpenseReportData(this);

    m_table = new JTable();
    m_table.setAutoCreateColumnsFromModel(false);
    m_table.setModel(m_data);
    m_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    for (int k = 0; k < ExpenseReportData.m_columns.length; k++) {
        TableCellRenderer renderer;
        if (k == ExpenseReportData.COL_APPROVED)
            renderer = new CheckCellRenderer();
        else {
            DefaultTableCellRenderer textRenderer = new DefaultTableCellRenderer();
            textRenderer.setHorizontalAlignment(ExpenseReportData.m_columns[k].m_alignment);
            renderer = textRenderer;
        }

        TableCellEditor editor;

        if (k == ExpenseReportData.COL_CATEGORY)
            editor = new DefaultCellEditor(new JComboBox(ExpenseReportData.CATEGORIES));
        else if (k == ExpenseReportData.COL_APPROVED)
            editor = new DefaultCellEditor(new JCheckBox());
        else
            editor = new DefaultCellEditor(new JTextField());

        TableColumn column = new TableColumn(k, ExpenseReportData.m_columns[k].m_width, renderer, editor);
        m_table.addColumn(column);
    }

    JTableHeader header = m_table.getTableHeader();
    header.setUpdateTableInRealTime(false);

    JScrollPane ps = new JScrollPane();
    ps.setSize(550, 150);
    ps.getViewport().add(m_table);
    getContentPane().add(ps, BorderLayout.CENTER);

    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));

    ImageIcon penny = new ImageIcon("penny.gif");
    m_title = new JLabel("Total: $", penny, JButton.LEFT);
    m_title.setForeground(Color.black);
    m_title.setAlignmentY(0.5f);
    p.add(m_title);
    p.add(Box.createHorizontalGlue());

    JButton bt = new JButton("Insert before");
    bt.setMnemonic('b');
    bt.setAlignmentY(0.5f);
    ActionListener lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = m_table.getSelectedRow();
            m_data.insert(row);
            m_table.tableChanged(
                    new TableModelEvent(m_data, row, row, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
            m_table.repaint();
        }
    };
    bt.addActionListener(lst);
    p.add(bt);

    bt = new JButton("Insert after");
    bt.setMnemonic('a');
    bt.setAlignmentY(0.5f);
    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = m_table.getSelectedRow();
            m_data.insert(row + 1);
            m_table.tableChanged(new TableModelEvent(m_data, row + 1, row + 1, TableModelEvent.ALL_COLUMNS,
                    TableModelEvent.INSERT));
            m_table.repaint();
        }
    };
    bt.addActionListener(lst);
    p.add(bt);

    bt = new JButton("Delete row");
    bt.setMnemonic('d');
    bt.setAlignmentY(0.5f);
    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = m_table.getSelectedRow();
            if (m_data.delete(row)) {
                m_table.tableChanged(new TableModelEvent(m_data, row, row, TableModelEvent.ALL_COLUMNS,
                        TableModelEvent.INSERT));
                m_table.repaint();
                calcTotal();
            }
        }
    };
    bt.addActionListener(lst);
    p.add(bt);

    getContentPane().add(p, BorderLayout.SOUTH);

    calcTotal();

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);

    setVisible(true);
}

From source file:StocksTable.java

public StocksTable() {
    super("Stocks Table");
    setSize(600, 300);//w ww  .j ava  2s .  c  o m

    m_data = new StockTableData();

    m_title = new JLabel(m_data.getTitle(), new ImageIcon("money.gif"), SwingConstants.LEFT);
    m_title.setFont(new Font("TimesRoman", Font.BOLD, 24));
    m_title.setForeground(Color.black);
    getContentPane().add(m_title, BorderLayout.NORTH);

    m_table = new JTable();
    m_table.setAutoCreateColumnsFromModel(false);
    m_table.setModel(m_data);

    for (int k = 0; k < StockTableData.m_columns.length; k++) {
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setHorizontalAlignment(StockTableData.m_columns[k].m_alignment);
        TableColumn column = new TableColumn(k, StockTableData.m_columns[k].m_width, renderer, null);
        m_table.addColumn(column);
    }

    JTableHeader header = m_table.getTableHeader();
    header.setUpdateTableInRealTime(false);

    JScrollPane ps = new JScrollPane();
    ps.getViewport().add(m_table);
    getContentPane().add(ps, BorderLayout.CENTER);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);
    setVisible(true);
}