Example usage for javax.swing JTable getColumnClass

List of usage examples for javax.swing JTable getColumnClass

Introduction

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

Prototype

public Class<?> getColumnClass(int column) 

Source Link

Document

Returns the type of the column appearing in the view at column position column.

Usage

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   w  ww .  j a  v a 2s.c  o m*/
        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:LineChart.java

private static JFreeChart createDataset(JTable table) {
    TimeZone tz = TimeZone.getTimeZone("GMT");

    XYDataset ds = null;//from  w  w w .j  a  v a  2  s.c o m

    if (table.getColumnCount() > 0) {
        Class klass = table.getColumnClass(0);

        if ((klass == Date.class) || (klass == Time.class) || (klass == c.Month.class)
                || (klass == c.Minute.class) || (klass == c.Second.class) || (klass == Timestamp.class)) {
            TimeSeriesCollection tsc = new TimeSeriesCollection();

            for (int col = 1; col < table.getColumnCount(); col++) {
                TimeSeries series = null;

                try {
                    if (klass == Date.class) {
                        series = new TimeSeries(table.getColumnName(col), Day.class);

                        for (int row = 0; row < table.getRowCount(); row++) {
                            Date date = (Date) table.getValueAt(row, 0);
                            Day day = new Day(date, tz);

                            Object o = table.getValueAt(row, col);
                            if (o instanceof Number) {
                                ((TimeSeries) series).addOrUpdate(day, (Number) table.getValueAt(row, col));
                            }
                        }
                    } else if (klass == Time.class) {
                        series = new TimeSeries(table.getColumnName(col), Millisecond.class);

                        for (int row = 0; row < table.getRowCount(); row++) {
                            Time time = (Time) table.getValueAt(row, 0);
                            Millisecond ms = new Millisecond(time, tz);

                            //    Millisecond ms = new Millisecond(time);
                            Object o = table.getValueAt(row, col);
                            if (o instanceof Number) {
                                series.addOrUpdate(ms, (Number) table.getValueAt(row, col));
                            }
                        }
                    } else if (klass == Timestamp.class) {
                        series = new TimeSeries(table.getColumnName(col), Millisecond.class);

                        for (int row = 0; row < table.getRowCount(); row++) {
                            Timestamp time = (Timestamp) table.getValueAt(row, 0);
                            Millisecond ms = new Millisecond(time, tz);

                            Object o = table.getValueAt(row, col);
                            if (o instanceof Number) {
                                series.addOrUpdate(ms, (Number) table.getValueAt(row, col));
                            }

                        }
                    } else if (klass == c.Month.class) {
                        series = new TimeSeries(table.getColumnName(col), Month.class);

                        for (int row = 0; row < table.getRowCount(); row++) {
                            c.Month time = (c.Month) table.getValueAt(row, 0);
                            int m = time.i + 24000;
                            int y = m / 12;
                            m = 1 + m % 12;

                            Month month = new Month(m, y);

                            Object o = table.getValueAt(row, col);
                            if (o instanceof Number) {
                                series.addOrUpdate(month, (Number) table.getValueAt(row, col));
                            }

                        }
                    } else if (klass == c.Second.class) {
                        series = new TimeSeries(table.getColumnName(col), Second.class);

                        for (int row = 0; row < table.getRowCount(); row++) {
                            c.Second time = (c.Second) table.getValueAt(row, 0);

                            Second second = new Second(time.i % 60, time.i / 60, 0, 1, 1, 2001);

                            Object o = table.getValueAt(row, col);
                            if (o instanceof Number) {
                                series.addOrUpdate(second, (Number) table.getValueAt(row, col));
                            }

                        }
                    } else if (klass == c.Minute.class) {
                        series = new TimeSeries(table.getColumnName(col), Minute.class);

                        for (int row = 0; row < table.getRowCount(); row++) {
                            c.Minute time = (c.Minute) table.getValueAt(row, 0);

                            Minute minute = new Minute(time.i % 60, time.i / 60, 1, 1, 2001);

                            Object o = table.getValueAt(row, col);
                            if (o instanceof Number) {
                                series.addOrUpdate(minute, (Number) table.getValueAt(row, col));
                            }
                        }
                    }
                } catch (SeriesException e) {
                    System.err.println("Error adding to series");
                }

                if (series.getItemCount() > 0)
                    tsc.addSeries(series);
            }

            ds = tsc;
        } else if ((klass == double.class) || (klass == int.class) || (klass == short.class)
                || (klass == long.class) || (klass == Time.class)) {
            XYSeriesCollection xysc = new XYSeriesCollection();

            for (int col = 1; col < table.getColumnCount(); col++) {
                XYSeries series = null;

                try {
                    series = new XYSeries(table.getColumnName(col));

                    for (int row = 0; row < table.getRowCount(); row++) {
                        Number x = (Number) table.getValueAt(row, 0);

                        Object y = table.getValueAt(row, col);
                        if (y instanceof Number) {
                            series.add(x, (Number) y);
                        }
                    }
                } catch (SeriesException e) {
                    System.err.println("Error adding to series");
                }

                if (series.getItemCount() > 0)
                    xysc.addSeries(series);
            }

            ds = xysc;
        }
    }

    if (ds != null) {
        boolean legend = false;

        if (ds.getSeriesCount() > 1) {
            legend = true;
        }

        if (ds instanceof XYSeriesCollection) {
            return ChartFactory.createXYLineChart("", "", "", ds, PlotOrientation.VERTICAL, legend, true, true);
        } else if (ds instanceof TimeSeriesCollection)
            return ChartFactory.createTimeSeriesChart("", "", "", ds, legend, true, true);
    }

    return null;
}

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 w ww .  j a  va2s  .  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:org.jdal.swing.table.AnnotationFormatTableCellRenderer.java

/**
 * {@inheritDoc}//  w  w  w.  j av a 2  s.  com
 */
@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;
}

From source file:fll.subjective.SubjectiveFrame.java

/**
 * Set the tab and return behavior for a table.
 *//*from  www  . j  av a 2s . c  o  m*/
private void setupTabReturnBehavior(final JTable table) {
    final InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    // Have the enter key work the same as the tab key
    final KeyStroke tab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0);
    final KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
    im.put(enter, im.get(tab));

    // Override the default tab behavior
    // Tab to the next editable cell. When no editable cells goto next cell.
    final Action oldTabAction = table.getActionMap().get(im.get(tab));
    final Action tabAction = new AbstractAction() {
        public void actionPerformed(final ActionEvent e) {
            if (null != oldTabAction) {
                oldTabAction.actionPerformed(e);
            }

            final JTable table = (JTable) e.getSource();
            final int rowCount = table.getRowCount();
            final int columnCount = table.getColumnCount();
            int row = table.getSelectedRow();
            int column = table.getSelectedColumn();

            // skip the no show when tabbing
            while (!table.isCellEditable(row, column) || table.getColumnClass(column) == Boolean.class) {
                column += 1;

                if (column == columnCount) {
                    column = 0;
                    row += 1;
                }

                if (row == rowCount) {
                    row = 0;
                }

                // Back to where we started, get out.
                if (row == table.getSelectedRow() && column == table.getSelectedColumn()) {
                    break;
                }
            }

            table.changeSelection(row, column, false, false);
        }
    };
    table.getActionMap().put(im.get(tab), tabAction);
}