Example usage for javax.swing JTable getColumnModel

List of usage examples for javax.swing JTable getColumnModel

Introduction

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

Prototype

public TableColumnModel getColumnModel() 

Source Link

Document

Returns the TableColumnModel that contains all column information of this table.

Usage

From source file:ErroresPorTraza.TabbedForm.java

private void setSizeColumn(JTable jTable1) {
    TableColumnModel model1 = jTable1.getColumnModel();
    model1.getColumn(0).setPreferredWidth(300);
    model1.getColumn(1).setPreferredWidth(75);
}

From source file:ErroresPorTraza.TabbedForm.java

private void setSizeColumnMil(JTable jTable1) {
    TableColumnModel model1 = jTable1.getColumnModel();
    model1.getColumn(0).setPreferredWidth(195);
    model1.getColumn(1).setPreferredWidth(60);
    model1.getColumn(2).setPreferredWidth(60);
    model1.getColumn(3).setPreferredWidth(60);
}

From source file:edu.ku.brc.specify.plugins.sgr.SGRResultsDisplay.java

public SGRResultsDisplay(int width, MatchResults results) {
    if (Main.m_settings == null) {
        Main.m_settings = new JSettings(true);
        Main.m_settings.load(/*from  www .  j a va  2 s. c  o  m*/
                JPathHelper.addSeparator(System.getProperty("user.home")) + JSettings.SETTINGS_FILE, "1.8");

        Main.m_sysLocale = Locale.getDefault();
        if (Main.m_settings.getLocale().length() > 0) {
            Locale.setDefault(new Locale(Main.m_settings.getLocale()));
        }
    }

    SGRColumnOrdering columns = SGRColumnOrdering.getInstance();
    String[] fields = columns.getFields();

    DefaultTableModel resultsTableModel = new DefaultTableModel(columns.getHeadings(), 0);

    final List<List<Color>> rowColors = new LinkedList<List<Color>>();

    for (Match result : results) {
        SGRRecord match = result.match;
        float score = result.score;
        float maxScore = 22.0f; //results.maxScore;

        List<Color> cellColors = new LinkedList<Color>();

        List<String> data = new LinkedList<String>();
        for (String field : fields) {
            if (field.equals("id")) {
                data.add(match.id);
                cellColors.add(SGRColors.colorForScore(score, maxScore));
            } else if (field.equals("score")) {
                data.add(((Float) score).toString());
                cellColors.add(SGRColors.colorForScore(score, maxScore));
            } else {
                data.add(StringUtils.join(match.getFieldValues(field).toArray(), ';'));

                Float fieldContribution = result.fieldScoreContributions().get(field);
                Color color = SGRColors.colorForScore(score, maxScore, fieldContribution);
                cellColors.add(color);
            }
        }
        resultsTableModel.addRow(data.toArray());
        rowColors.add(cellColors);
    }

    JTable table = createTable(resultsTableModel, rowColors);
    Dimension preferredSize = table.getPreferredSize();
    preferredSize.width = Math.min((int) (0.9 * width), preferredSize.width);
    table.setPreferredScrollableViewportSize(preferredSize);
    table.getColumnModel().addColumnModelListener(this);

    add(new JScrollPane(table));
}

From source file:net.sourceforge.atunes.kernel.modules.statistics.StatsDialogController.java

/**
 * Sets the table./*w ww.  j  a va  2 s.  c  o m*/
 * 
 * @param table
 *            the table
 * @param headers
 *            the headers
 * @param content
 *            the content
 */
private void setTable(final JTable table, final Object[] headers, final Object[][] content) {
    table.setModel(new StatsDialogDefaultTableModel(content, headers));
    table.getColumnModel().getColumn(0).setPreferredWidth(420);
    table.getColumnModel().getColumn(0).setWidth(table.getColumnModel().getColumn(0).getWidth());
    table.getColumnModel().getColumn(0).setCellRenderer(this.lookAndFeelManager.getCurrentLookAndFeel()
            .getTableCellRenderer(new SwingOrientationTableCellRendererCode(this.controlsBuilder)));
    table.getColumnModel().getColumn(2).setPreferredWidth(30);
    table.getColumnModel().getColumn(2).setWidth(table.getColumnModel().getColumn(2).getWidth());

    table.getColumnModel().getColumn(1).setCellRenderer(this.lookAndFeelManager.getCurrentLookAndFeel()
            .getTableCellRenderer(new RightAlignmentTableCellRendererCode()));
    table.getColumnModel().getColumn(2).setCellRenderer(this.lookAndFeelManager.getCurrentLookAndFeel()
            .getTableCellRenderer(new RightAlignmentTableCellRendererCode()));
}

From source file:components.TableRenderDemo.java

private void initColumnSizes(JTable table) {
    MyTableModel model = (MyTableModel) table.getModel();
    TableColumn column = null;//from  w  ww  . j  a va 2s . c o  m
    Component comp = null;
    int headerWidth = 0;
    int cellWidth = 0;
    Object[] longValues = model.longValues;
    TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();

    for (int i = 0; i < 5; i++) {
        column = table.getColumnModel().getColumn(i);

        comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0);
        headerWidth = comp.getPreferredSize().width;

        comp = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(table,
                longValues[i], false, false, 0, i);
        cellWidth = comp.getPreferredSize().width;

        if (DEBUG) {
            System.out.println("Initializing width of column " + i + ". " + "headerWidth = " + headerWidth
                    + "; cellWidth = " + cellWidth);
        }

        column.setPreferredWidth(Math.max(headerWidth, cellWidth));
    }
}

From source file:SampleTableModel.java

@Override
public void init() {
    tableModel = new SampleTableModel();
    // create javafx panel for charts
    chartFxPanel = new JFXPanel();
    chartFxPanel.setPreferredSize(new Dimension(PANEL_WIDTH_INT, PANEL_HEIGHT_INT));

    //JTable/*from w w w .  ja v  a 2s  .  c om*/
    JTable table = new JTable(tableModel);
    table.setAutoCreateRowSorter(true);
    table.setGridColor(Color.DARK_GRAY);
    SwingInterop.DecimalFormatRenderer renderer = new SwingInterop.DecimalFormatRenderer();
    renderer.setHorizontalAlignment(JLabel.RIGHT);
    for (int i = 0; i < table.getColumnCount(); i++) {
        table.getColumnModel().getColumn(i).setCellRenderer(renderer);
    }
    JScrollPane tablePanel = new JScrollPane(table);
    tablePanel.setPreferredSize(new Dimension(PANEL_WIDTH_INT, TABLE_PANEL_HEIGHT_INT));

    JPanel chartTablePanel = new JPanel();
    chartTablePanel.setLayout(new BorderLayout());

    //Split pane that holds both chart and table
    JSplitPane jsplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    jsplitPane.setTopComponent(chartTablePanel);
    jsplitPane.setBottomComponent(tablePanel);
    jsplitPane.setDividerLocation(410);
    chartTablePanel.add(chartFxPanel, BorderLayout.CENTER);

    //          add(tablePanel, BorderLayout.CENTER);
    add(jsplitPane, BorderLayout.CENTER);

    // create JavaFX scene
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            createScene();
        }
    });
}

From source file:com.floreantpos.bo.ui.explorer.MenuItemExplorer.java

public void resizeColumnWidth(JTable table) {
    final TableColumnModel columnModel = table.getColumnModel();
    for (int column = 0; column < table.getColumnCount(); column++) {
        columnModel.getColumn(column).setPreferredWidth((Integer) getColumnWidth().get(column));
    }//from   ww  w  .j  a  v  a2 s  . com
}

From source file:edu.ku.brc.specify.plugins.sgr.SGRResultsDisplay.java

/**
 * From http://www.pikopong.com/blog/2008/08/13/auto-resize-jtable-column-width/
 * //from   w w w.j  ava 2 s .  co  m
 * @param table
 * @param model
 * @return
 */
private JTable autoResizeColWidth(JTable table, DefaultTableModel model) {
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.setModel(model);

    int margin = 5;

    for (int i = 0; i < table.getColumnCount(); i++) {
        int vColIndex = i;
        DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
        TableColumn col = colModel.getColumn(vColIndex);
        int width = 0;

        // Get width of column header
        TableCellRenderer renderer = col.getHeaderRenderer();

        if (renderer == null) {
            renderer = table.getTableHeader().getDefaultRenderer();
        }

        Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0,
                0);

        width = comp.getPreferredSize().width;

        // Get maximum width of column data
        for (int r = 0; r < table.getRowCount(); r++) {
            renderer = table.getCellRenderer(r, vColIndex);
            comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false,
                    r, vColIndex);
            width = Math.max(width, comp.getPreferredSize().width);
        }

        // Add margin
        width += 2 * margin;

        // Set the width
        col.setPreferredWidth(width);
    }

    ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer())
            .setHorizontalAlignment(SwingConstants.LEFT);

    // table.setAutoCreateRowSorter(true);
    //table.getTableHeader().setReorderingAllowed(false);

    return table;
}

From source file:TableCellRenderTest.java

public TableCellRenderFrame() {
    setTitle("TableCellRenderTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    TableModel model = new PlanetTableModel();
    JTable table = new JTable(model);
    table.setRowSelectionAllowed(false);

    // set up renderers and editors

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

    JComboBox moonCombo = new JComboBox();
    for (int i = 0; i <= 20; i++)
        moonCombo.addItem(i);//from   www  .  jav  a2 s  . c om

    TableColumnModel columnModel = table.getColumnModel();
    TableColumn moonColumn = columnModel.getColumn(PlanetTableModel.MOONS_COLUMN);
    moonColumn.setCellEditor(new DefaultCellEditor(moonCombo));
    moonColumn.setHeaderRenderer(table.getDefaultRenderer(ImageIcon.class));
    moonColumn.setHeaderValue(new ImageIcon("Moons.gif"));

    // show table

    table.setRowHeight(100);
    add(new JScrollPane(table), BorderLayout.CENTER);
}

From source file:logdruid.ui.mainpanel.EventRecordingSelectorPanel.java

private void initColumnSizes(JTable theTable) {
    TableColumn column = null;/*from  ww w.  j a  v  a  2s.co m*/
    Component comp = null;
    int headerWidth = 0;
    int cellWidth = 0;
    TableCellRenderer headerRenderer = theTable.getTableHeader().getDefaultRenderer();

    for (int i = 0; i < 4; i++) {
        column = theTable.getColumnModel().getColumn(i);
        comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0);
        headerWidth = comp.getPreferredSize().width;
        cellWidth = comp.getPreferredSize().width;
        column.setPreferredWidth(Math.max(headerWidth, cellWidth));
    }
}