Example usage for javax.swing JTable setDefaultRenderer

List of usage examples for javax.swing JTable setDefaultRenderer

Introduction

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

Prototype

public void setDefaultRenderer(Class<?> columnClass, TableCellRenderer renderer) 

Source Link

Document

Sets a default cell renderer to be used if no renderer has been set in a TableColumn.

Usage

From source file:Main.java

public static void main(String[] args) {
    JTable t = new JTable(new TimesTableModel());
    t.setDefaultRenderer(Object.class, new TimesTableRenderer());
    JOptionPane.showMessageDialog(null, t);
}

From source file:Main.java

public static void main(String[] args) {
    int maximum = 100;
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Integer[] oneRow = { 0, 0, 0, 0 };
    String[] headers = { "A", "B", "C", "D" };
    Integer[][] data = { oneRow, oneRow, oneRow, oneRow, oneRow, };
    DefaultTableModel model = new DefaultTableModel(data, headers);
    JTable table = new JTable(model);
    table.setDefaultRenderer(Object.class, new ProgressRenderer(0, maximum));
    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    frame.add(new JScrollPane(table));
    frame.pack();//from w  w w . j  a  va 2  s  . c  o m
    frame.setVisible(true);
    new Thread(new Runnable() {
        @Override
        public void run() {
            Object waiter = new Object();
            synchronized (waiter) {
                int rows = model.getRowCount();
                int columns = model.getColumnCount();
                Random random = new Random(System.currentTimeMillis());
                boolean done = false;
                while (!done) {
                    int row = random.nextInt(rows);
                    int column = random.nextInt(columns);
                    Integer value = (Integer) model.getValueAt(row, column);
                    value++;
                    if (value <= maximum) {
                        model.setValueAt(value, row, column);
                        try {
                            waiter.wait(15);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    done = true;
                    for (row = 0; row < rows; row++) {
                        for (column = 0; column < columns; column++) {
                            if (!model.getValueAt(row, column).equals(maximum)) {
                                done = false;
                                break;
                            }
                        }
                        if (!done) {
                            break;
                        }
                    }
                }
            }
        }
    }).start();
}

From source file:RendererSample.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("Renderer Sample");
    JTable table = new JTable(rows, headers);
    TableCellRenderer renderer = new EvenOddRenderer();
    table.setDefaultRenderer(Object.class, renderer);
    JScrollPane scrollPane = new JScrollPane(table);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);//from   w ww.ja v  a 2 s .c  o  m
    frame.setVisible(true);
}

From source file:EvenOddRenderer.java

public static void main(String args[]) {

    final Object rowData[][] = { { "1", "one", "I" }, { "2", "two", "II" }, { "3", "three", "III" } };
    final String columnNames[] = { "#", "English", "Roman" };

    final JTable table = new JTable(rowData, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);

    table.setDefaultRenderer(Object.class, new EvenOddRenderer());

    JFrame frame = new JFrame("Resizing Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(scrollPane, BorderLayout.CENTER);

    frame.setSize(300, 150);//from  w w  w  . j  a  v a 2  s .  co m
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] argv) {
    DefaultTableModel model = new DefaultTableModel() {
        public Class getColumnClass(int mColIndex) {
            int rowIndex = 0;
            Object o = getValueAt(rowIndex, mColIndex);
            if (o == null) {
                return Object.class;
            } else {
                return o.getClass();
            }/* w ww  .  j  a  va2  s.com*/
        }
    };
    JTable table = new JTable(model);
    model.addColumn("Col1", new Object[] { Color.red });
    model.addRow(new Object[] { Color.green });
    model.addRow(new Object[] { Color.blue });

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

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

From source file:Main.java

private static JPanel createPanel() {
    JPanel panel = new JPanel();
    DefaultTableModel model = new DefaultTableModel() {
        @Override//  www . j  av 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:org.drugis.mtc.gui.results.ResultsComponentFactory.java

public static JTable buildVarianceTable(final MTCModelWrapper<?> wrapper) {
    final JTable varTable = new EnhancedTable(new NetworkVarianceTableModel(wrapper), 150);
    varTable.setDefaultRenderer(QuantileSummary.class, new SummaryCellRenderer());
    return varTable;
}

From source file:org.drugis.mtc.gui.results.ResultsComponentFactory.java

public static JTable buildRelativeEffectsTable(final List<Treatment> treatments,
        final MTCModelWrapper<?> wrapper, final boolean isDichotomous, final boolean showDescription) {
    final JTable reTable = createTableWithoutHeaders(new NetworkRelativeEffectTableModel(treatments, wrapper));
    reTable.setDefaultRenderer(Object.class,
            new NetworkRelativeEffectTableCellRenderer(isDichotomous, showDescription));

    return reTable;
}

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//from ww  w  .  j a va 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:EvenOddRowCellRenderer.java

public EvenOddRowCellRenderer() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable table = new JTable(tmodel);
    table.setDefaultRenderer(Object.class, new MyRenderer());
    getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
    pack();//w w  w .ja v  a  2s.  co m
}