Example usage for javax.swing JTable getColumn

List of usage examples for javax.swing JTable getColumn

Introduction

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

Prototype

public TableColumn getColumn(Object identifier) 

Source Link

Document

Returns the TableColumn object for the column in the table whose identifier is equal to identifier, when compared using equals.

Usage

From source file:net.sourceforge.atunes.kernel.controllers.stats.StatsDialogController.java

private void setTable(JTable table, Object[] headers, Object[][] content) {
    table.setModel(new DefaultTableModel(content, headers));
    table.getColumnModel().getColumn(0).setPreferredWidth(420);
    table.getColumnModel().getColumn(0).setWidth(table.getColumnModel().getColumn(0).getWidth());
    table.getColumnModel().getColumn(2).setPreferredWidth(30);
    table.getColumnModel().getColumn(2).setWidth(table.getColumnModel().getColumn(2).getWidth());
    table.getColumnModel().getColumn(1).setCellRenderer(new SubstanceDefaultTableCellRenderer() {
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                boolean hasFocus, int row, int column) {
            JLabel l = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row,
                    column);/*from ww  w. j  a v  a 2  s  .  c o m*/
            l.setHorizontalAlignment(SwingConstants.RIGHT);
            return l;
        }
    });
    table.getColumnModel().getColumn(2).setCellRenderer(new SubstanceDefaultTableCellRenderer() {
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                boolean hasFocus, int row, int column) {
            JLabel l = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row,
                    column);
            l.setHorizontalAlignment(SwingConstants.RIGHT);
            return l;
        }
    });
}

From source file:de.wusel.partyplayer.gui.PartyPlayer.java

private Component createPlayListPanel() {
    final PlaylistTableModel model = new PlaylistTableModel(playerModel, this);
    final JTable playlistTable = new JTable(model);
    playlistTable.getColumn(getText("table.playlist.column.votes.label")).setMaxWidth(40);
    playlistTable.getColumn(getText("table.playlist.column.votes.label")).setResizable(false);
    JScrollPane scrollPane = new JScrollPane(playlistTable);
    playlistTable.setFillsViewportHeight(true);
    return scrollPane;
}

From source file:com.sander.verhagen.frame.LineWrapCellRenderer.java

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    String text = value.toString();
    setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());
    setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
    int columnWidth = table.getColumnModel().getColumn(column).getWidth();
    setSize(columnWidth, 0 /* don't know yet */);
    setText(text);/*w  ww. j  av a2  s .com*/

    int fontHeight = this.getFontMetrics(this.getFont()).getHeight();
    if (StringUtils.isEmpty(text)) {
        table.setRowHeight(row, fontHeight);
    } else {
        table.setRowHeight(row, fontHeight * getLineCount(this));
    }
    return this;
}

From source file:Visuals.RingChart.java

public JPanel addAVCharts() {
    JPanel thisPanel = new JPanel();
    thisPanel.setLayout(new BorderLayout());
    int tempCriticalCount = 0, tempUpdatedCount = 0;
    String[][] criticalList = new String[critical][4];
    String[][] updatedList = new String[low][4];
    for (int i = 0; i < riskCount; i++) {
        if (risks[i][2].equals("Critical")) {
            criticalList[tempCriticalCount][0] = risks[i][0];
            criticalList[tempCriticalCount][1] = risks[i][1];
            criticalList[tempCriticalCount][3] = risks[i][3];
            tempCriticalCount++;/*  w  w w  .  j  a  va  2s. co m*/
        }
        if (risks[i][2].equals("Low")) {
            updatedList[tempUpdatedCount][0] = risks[i][0];
            updatedList[tempUpdatedCount][1] = risks[i][1];
            tempUpdatedCount++;
        }
    }

    JTable criticalTable = new JTable(criticalList, criticalColumns);
    JTable updatedTable = new JTable(updatedList, updatedColumns);
    TableColumn tcol = criticalTable.getColumnModel().getColumn(2);
    criticalTable.removeColumn(tcol);
    TableColumn tcol2 = updatedTable.getColumnModel().getColumn(2);
    updatedTable.removeColumn(tcol2);

    criticalTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    updatedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    criticalTable.setEnabled(false);
    updatedTable.setEnabled(false);

    int width = 0;
    for (int i = 0; i < 3; i++) {
        for (int row = 0; row < criticalTable.getRowCount(); row++) {
            TableCellRenderer renderer = criticalTable.getCellRenderer(row, i);
            Component comp = criticalTable.prepareRenderer(renderer, row, i);
            width = Math.max(comp.getPreferredSize().width, width);
        }
        criticalTable.getColumnModel().getColumn(i).setPreferredWidth(width);
        width = 0;
    }

    for (int i = 0; i < 2; i++) {
        for (int row = 0; row < updatedTable.getRowCount(); row++) {
            TableCellRenderer renderer = updatedTable.getCellRenderer(row, i);
            Component comp = updatedTable.prepareRenderer(renderer, row, i);
            width = Math.max(comp.getPreferredSize().width, width);
        }
        updatedTable.getColumnModel().getColumn(i).setPreferredWidth(width);
        width = 0;
    }

    criticalTable.setShowHorizontalLines(true);
    criticalTable.setRowHeight(40);
    updatedTable.setShowHorizontalLines(true);
    updatedTable.setRowHeight(40);

    JScrollPane criticalTableScrollPane = new JScrollPane(criticalTable,
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    JScrollPane updatedTableScrollPane = new JScrollPane(updatedTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    Dimension d = criticalTable.getPreferredSize();
    criticalTableScrollPane.setPreferredSize(
            new Dimension((d.width - 400), (criticalTable.getRowHeight() + 1) * (tempCriticalCount + 1)));

    Dimension d2 = updatedTable.getPreferredSize();
    updatedTableScrollPane.setPreferredSize(
            new Dimension((d.width - 400), (updatedTable.getRowHeight() + 1) * (tempUpdatedCount + 1)));

    Font titleFonts = new Font("Calibri", Font.BOLD, 30);

    JLabel criticalLabel = new JLabel(" Critical    ");
    criticalLabel.setFont(titleFonts);
    criticalLabel.setForeground(new Color(230, 27, 27));
    JPanel leftPanel = new JPanel();
    leftPanel.setLayout(new BorderLayout());
    leftPanel.add(criticalLabel, BorderLayout.WEST);
    leftPanel.add(criticalTableScrollPane, BorderLayout.CENTER);

    JLabel updatedLabel = new JLabel(" Updated ");
    updatedLabel.setFont(titleFonts);
    updatedLabel.setForeground(new Color(47, 196, 6));
    JPanel rightPanel = new JPanel();
    rightPanel.setLayout(new BorderLayout());
    rightPanel.add(updatedLabel, BorderLayout.WEST);
    rightPanel.add(updatedTableScrollPane, BorderLayout.CENTER);

    if (tempCriticalCount != 0)
        thisPanel.add(leftPanel, BorderLayout.NORTH);
    if (tempUpdatedCount != 0)
        thisPanel.add(rightPanel, BorderLayout.SOUTH);
    return thisPanel;
}

From source file:gui.accessories.BattleSimFx.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 ww  .j av  a2  s . co m*/
    JTable table = new JTable(tableModel);
    table.setAutoCreateRowSorter(true);
    table.setGridColor(Color.DARK_GRAY);
    BattleSimFx.DecimalFormatRenderer renderer = new BattleSimFx.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:TableRenderDemo.java

public TableRenderDemo() {
    super(new GridLayout(1, 0));

    JTable table = new JTable(new MyTableModel());
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));

    // Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    // Set up column sizes.
    initColumnSizes(table);//from  www . j a v a  2  s  . co m

    // Fiddle with the Sport column's cell editors/renderers.
    setUpSportColumn(table, table.getColumnModel().getColumn(2));

    // Add the scroll pane to this panel.
    add(scrollPane);
}

From source file:TableRenderDemo.java

private void initColumnSizes(JTable table) {
    MyTableModel model = (MyTableModel) table.getModel();
    TableColumn column = null;/*from w w w. j a  v  a2  s .  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);
        }

        // XXX: Before Swing 1.1 Beta 2, use setMinWidth instead.
        column.setPreferredWidth(Math.max(headerWidth, cellWidth));
    }
}

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

/**
 * Sets the table.//from  w  w w .  ja v a  2s.  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;/*w w  w  .j a  v  a 2s  .co 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  . j  av a 2  s  .c  o  m*/
    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();
        }
    });
}