Example usage for javax.swing JTable setAutoResizeMode

List of usage examples for javax.swing JTable setAutoResizeMode

Introduction

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

Prototype

@BeanProperty(enumerationValues = { "JTable.AUTO_RESIZE_OFF", "JTable.AUTO_RESIZE_NEXT_COLUMN",
        "JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS", "JTable.AUTO_RESIZE_LAST_COLUMN",
        "JTable.AUTO_RESIZE_ALL_COLUMNS" }, description = "Whether the columns should adjust themselves automatically.")
public void setAutoResizeMode(int mode) 

Source Link

Document

Sets the table's auto resize mode when the table is resized.

Usage

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 a  v  a2s .  com*/
 * @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:Interface.ResultadoJanela.java

public ResultadoJanela(List<Resultado> tar, List<Resultado> jac, List<Resultado> och, List<Resultado> sbi) {
    //  super("Resultado");
    CategoryDataset dataset;// w w  w .j  a  v a 2  s.c  o  m

    //---------------------gerando resultados tarantula-------------------------------------
    dataset = gerarDataset(tar, jac, och, sbi);
    JFreeChart chart = gerarGrafico(dataset);
    ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setDomainZoomable(true);

    JLabel lAjuda = new JLabel("Ajuda", JLabel.RIGHT);
    lAjuda.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icone_informacao.gif"))); // NOI18N
    lAjuda.setPreferredSize(new Dimension(50, 50));
    lAjuda.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            lAjudaMouseClicked(evt);
        }
    });

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(lAjuda, BorderLayout.BEFORE_FIRST_LINE);
    panel.add(chartPanel, BorderLayout.LINE_START);
    JLabel lTabela = new JLabel("Tabela de Resultados", JLabel.CENTER);
    panel.add(lTabela, BorderLayout.SOUTH);

    JTable table = new JTable(criarValores(tar, jac, och, sbi), criarColunas());

    // Adiciona o JTable dentro do painel
    JScrollPane scrollPane = new JScrollPane(table);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

    panel.add(scrollPane, BorderLayout.SOUTH);

    JFrame frame = new JFrame();
    frame.setTitle("JLoc - Resultado");
    frame.setVisible(true);
    frame.add(panel);

    frame.pack();
    frame.setVisible(true);
}

From source file:com.sec.ose.osi.ui.frm.main.manage.AddProjectTableModel.java

public void setColumnView(JTable table) {
    table.setShowVerticalLines(false);//from  w ww.  ja v  a2 s . com
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.setColumnSelectionAllowed(false);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
    table.setRowHeight(30);

    JTableHeader header = table.getTableHeader();
    header.setPreferredSize(new java.awt.Dimension(table.getTableHeader().getWidth(), 30));
    header.setFont(new Font("Arial", Font.BOLD, 12));
    header.setReorderingAllowed(false);

    TableColumnModel cm = table.getColumnModel();
    TableColumn col = null;

    searchHeader = new CheckBoxHeader(new CheckboxHeaderItemListener(table, COL_ISSELECT), "");
    col = cm.getColumn(COL_ISSELECT);
    col.setHeaderRenderer(searchHeader);
}

From source file:org.encog.workbench.dialogs.validate.ResultValidationChart.java

private void drawTable(Vector<Vector<String>> tableData, Vector<String> tableHeaders) {
    JTable table = new JTable(tableData, tableHeaders) {
        private static final long serialVersionUID = 8364655578079933961L;

        public boolean isCellEditable(int rowIndex, int vColIndex) {
            return false;
        }/*from   w w  w. j a  v a 2 s .co m*/
    };
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    tabs.addTab("Data", new JScrollPane(table));
}

From source file:Visuals.RingChart.java

public JPanel addDefenceCharts() {
    lowValue = "Low (" + low + ")";
    ChartPanel ringPanel = drawRingChart();
    ringPanel.setDomainZoomable(true);/*  w  w  w .j  a  v a  2s .com*/
    JPanel thisRingPanel = new JPanel();
    thisRingPanel.setLayout(new BorderLayout());
    String[][] finalRisks = new String[riskCount][4];
    for (int i = 0; i < riskCount; i++) {
        finalRisks[i][0] = risks[i][0];
        finalRisks[i][1] = risks[i][1];
        finalRisks[i][2] = risks[i][2];
        finalRisks[i][3] = risks[i][3];
    }

    JTable table = new JTable(finalRisks, networkDefenceColumns);
    TableColumn tcol = table.getColumnModel().getColumn(2);
    table.removeColumn(tcol);
    table.setEnabled(false);

    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    int width = 0;
    for (int row = 0; row < table.getRowCount(); row++) {
        TableCellRenderer renderer = table.getCellRenderer(row, 1);
        Component comp = table.prepareRenderer(renderer, row, 1);
        width = Math.max(comp.getPreferredSize().width, width);
    }

    table.getColumnModel().getColumn(1).setPreferredWidth(width);

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

    table.getColumnModel().getColumn(2).setPreferredWidth(width);
    table.setShowHorizontalLines(true);
    table.setRowHeight(40);

    JScrollPane tableScrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    Dimension d = table.getPreferredSize();
    tableScrollPane
            .setPreferredSize(new Dimension((d.width - 400), (table.getRowHeight() + 1) * (riskCount + 1)));

    JLabel right = new JLabel(
            "                                                                                                    ");
    thisRingPanel.add(right, BorderLayout.EAST);
    thisRingPanel.add(ringPanel, BorderLayout.CENTER);
    thisRingPanel.add(tableScrollPane, BorderLayout.SOUTH);
    return thisRingPanel;
}

From source file:org.drugis.addis.gui.builder.NetworkMetaAnalysisView.java

private void setColumnWidths(final JTable table) {
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    for (final TableColumn c : Collections.list(table.getColumnModel().getColumns())) {
        c.setMinWidth(170);//w  ww. ja va  2  s .c  om
        c.setPreferredWidth(170);
    }
}

From source file:com.sec.ose.osi.ui.frm.main.manage.ManagedProjectTableModel.java

public void setColumnType(JTable table) {
    table.setShowVerticalLines(false);//from  w w  w .  j  a v a 2 s .  c o m
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.setColumnSelectionAllowed(false);
    table.setRowSelectionAllowed(false);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.setRowHeight(30);

    JTableHeader header = table.getTableHeader();
    header.setPreferredSize(new java.awt.Dimension(table.getTableHeader().getWidth(), 30));
    header.setFont(new Font("Arial", Font.BOLD, 12));
    header.setReorderingAllowed(false);

    TableColumnModel cm = table.getColumnModel();
    TableColumn col = null;

    analyzeHeader = new CheckBoxHeader(new CheckboxHeaderItemListener(table, COL_ANALYZE_TARGET),
            "Analyze Target");
    col = cm.getColumn(COL_ANALYZE_TARGET);
    col.setHeaderRenderer(analyzeHeader);

    JCheckBox chkbox = new JCheckBox();
    chkbox.setBackground(Color.white);
    chkbox.setHorizontalAlignment(JLabel.CENTER);
    col.setCellRenderer(new DefaultTableCellRenderer() {
        private static final long serialVersionUID = 1L;

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                boolean hasFocus, int row, int column) {
            ManagedProjectTableModel model = (ManagedProjectTableModel) table.getModel();

            String sPrjName = (String) table.getValueAt(row, ManagedProjectTableModel.COL_PROJECT_NAME);
            OSIProjectInfo item = model.getProjectInfo(sPrjName);

            JCheckBox chkbox = new JCheckBox();
            chkbox.setSelected(((Boolean) value).booleanValue());
            if (item != null) {
                chkbox.setEnabled(item.isLocationValid());
            }
            chkbox.setHorizontalAlignment(JLabel.CENTER);
            chkbox.setBackground(Color.white);

            return (Component) chkbox;
        }
    });
    col.setCellEditor(new DefaultCellEditor(chkbox));

    col = cm.getColumn(COL_ANALYZE_STATUS);
    col.setCellRenderer(new StatusIconCellRenderer());

    col = cm.getColumn(COL_SOURCE_LOCATION);
    col.setCellRenderer(new FileBrowseCellRenderer());
    col.setCellEditor(new FileBrowseCellEditor());
}

From source file:MainClass.java

public MainClass() {
    super("Row Header Test");
    setSize(300, 200);/*from ww  w .j a  va 2 s .  co  m*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    TableModel tm = new AbstractTableModel() {
        String data[] = { "", "a", "b", "c", "d", "e" };

        String headers[] = { "Row #", "Column 1", "Column 2", "Column 3", "Column 4", "Column 5" };

        public int getColumnCount() {
            return data.length;
        }

        public int getRowCount() {
            return 1000;
        }

        public String getColumnName(int col) {
            return headers[col];
        }

        public Object getValueAt(int row, int col) {
            return data[col] + row;
        }
    };

    TableColumnModel cm = new DefaultTableColumnModel() {
        boolean first = true;

        public void addColumn(TableColumn tc) {
            if (first) {
                first = false;
                return;
            }
            tc.setMinWidth(150);
            super.addColumn(tc);
        }
    };

    TableColumnModel rowHeaderModel = new DefaultTableColumnModel() {
        boolean first = true;

        public void addColumn(TableColumn tc) {
            if (first) {
                tc.setMaxWidth(tc.getPreferredWidth());
                super.addColumn(tc);
                first = false;
            }
        }
    };

    JTable jt = new JTable(tm, cm);
    JTable headerColumn = new JTable(tm, rowHeaderModel);
    jt.createDefaultColumnsFromModel();
    headerColumn.createDefaultColumnsFromModel();

    jt.setSelectionModel(headerColumn.getSelectionModel());

    headerColumn.setBackground(Color.lightGray);
    headerColumn.setColumnSelectionAllowed(false);
    headerColumn.setCellSelectionEnabled(false);

    JViewport jv = new JViewport();
    jv.setView(headerColumn);
    jv.setPreferredSize(headerColumn.getMaximumSize());

    jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    JScrollPane jsp = new JScrollPane(jt);
    jsp.setRowHeader(jv);
    jsp.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, headerColumn.getTableHeader());
    getContentPane().add(jsp, BorderLayout.CENTER);
}

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++;/*  www  . j  a v  a  2 s . c o  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:Visuals.BarChart.java

public JPanel addCharts() {
    ChartPanel barPanel = drawBarChart();
    barPanel.setDomainZoomable(true);//from   w w  w . j  a  va 2s  .  c om
    JPanel thisBarPanel = new JPanel();
    thisBarPanel.setLayout(new BorderLayout());

    String[][] finalRisks = new String[riskCount][4];
    for (int i = 0; i < riskCount; i++) {
        finalRisks[i][0] = risks[i][0];
        finalRisks[i][1] = risks[i][1];
        finalRisks[i][2] = risks[i][2];
        finalRisks[i][3] = risks[i][3];
    }

    JTable table = new JTable(finalRisks, columns);
    //table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    TableRowSorter<TableModel> sorter = new TableRowSorter<>(table.getModel());
    table.setRowSorter(sorter);
    List<RowSorter.SortKey> sortKeys = new ArrayList<>();

    int columnIndexToSort = 2;
    sortKeys.add(new RowSorter.SortKey(columnIndexToSort, SortOrder.ASCENDING));

    sorter.setSortKeys(sortKeys);
    sorter.sort();

    TableColumn tcol = table.getColumnModel().getColumn(2);
    table.removeColumn(tcol);

    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    table.getColumnModel().getColumn(1).setPreferredWidth(600);
    table.getColumnModel().getColumn(2).setPreferredWidth(600);

    table.setShowHorizontalLines(true);
    table.setRowHeight(40);
    table.setEnabled(false);

    JScrollPane tableScrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    Dimension d = table.getPreferredSize();
    tableScrollPane
            .setPreferredSize(new Dimension((d.width - 400), (table.getRowHeight() + 1) * (riskCount + 1)));

    JLabel right = new JLabel(
            "                                                                                                    ");
    thisBarPanel.add(right, BorderLayout.EAST);
    thisBarPanel.add(barPanel, BorderLayout.CENTER);
    thisBarPanel.add(tableScrollPane, BorderLayout.SOUTH);
    return thisBarPanel;
}