Example usage for javax.swing JTable getColumnCount

List of usage examples for javax.swing JTable getColumnCount

Introduction

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

Prototype

@BeanProperty(bound = false)
public int getColumnCount() 

Source Link

Document

Returns the number of columns in the column model.

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/*  ww w  .  j  a  v a2s .  co 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:OAT.ui.util.UiUtil.java

public static void appendRow(JTable table) {
    appendRow(table, new Object[table.getColumnCount()]);
}

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);
        }// w  ww  . j  a v a 2 s  .co  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:OAT.ui.util.UiUtil.java

public static void updateTable(Object value, int rowId, int columnId, JTable table) {
    if (rowId > -1 && rowId < table.getRowCount() && columnId > -1 && columnId < table.getColumnCount()) {
        table.setValueAt(value, rowId, columnId);
    }//  w  ww. j av a 2 s . co  m
}

From source file:LineChart.java

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

    XYDataset ds = null;//from w  ww  . j a va2  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: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 ww. j a v a 2  s .  co  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:modnlp.capte.AlignmentInterfaceWS.java

public static int getPreferredRowHeight(JTable table, int rowIndex, int margin) {
    // Get the current default height for all rows
    int height = table.getRowHeight();

    // Determine highest cell in the row
    for (int c = 0; c < table.getColumnCount(); c++) {
        TableCellRenderer renderer = table.getCellRenderer(rowIndex, c);
        Component comp = table.prepareRenderer(renderer, rowIndex, c);
        int h = comp.getPreferredSize().height + 2 * margin;
        height = Math.max(height, h);
    }/* w  w w  . j  a v  a 2 s . c om*/
    return height;
}

From source file:Main.java

public int toModel(JTable table, int vColIndex) {
    if (vColIndex >= table.getColumnCount()) {
        return -1;
    }/*from w  ww .  ja v a 2 s .com*/
    return table.getColumnModel().getColumn(vColIndex).getModelIndex();
}

From source file:Main.java

public int toView(JTable table, int mColIndex) {
    for (int c = 0; c < table.getColumnCount(); c++) {
        TableColumn col = table.getColumnModel().getColumn(c);
        if (col.getModelIndex() == mColIndex) {
            return c;
        }/*from  ww w  . ja  va  2 s.c  o m*/
    }
    return -1;
}

From source file:Main.java

public TableColumn[] getColumnsInView(JTable table) {
    TableColumn[] result = new TableColumn[table.getColumnCount()];

    // Use a for loop
    for (int c = 0; c < table.getColumnCount(); c++) {
        result[c] = table.getColumnModel().getColumn(c);
    }//from   w w  w . j  ava  2s. c  om

    return result;
}