Java JTable Column makeTablePanel(int rows, int cols, int mainColumn, JComponent components[])

Here you can find the source of makeTablePanel(int rows, int cols, int mainColumn, JComponent components[])

Description

This method creates a JPanel which layouts given components in a table of given rows/columns.

License

Open Source License

Declaration

public static JPanel makeTablePanel(int rows, int cols, int mainColumn, JComponent components[]) 

Method Source Code

//package com.java2s;
/*/* w w  w  . j  a  v a2s. c om*/
 * Copyright 2006-2015 The MZmine 2 Development Team
 * 
 * This file is part of MZmine 2.
 * 
 * MZmine 2 is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * MZmine 2 is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * MZmine 2; if not, write to the Free Software Foundation, Inc., 51 Franklin
 * St, Fifth Floor, Boston, MA 02110-1301 USA
 */

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.Box;

import javax.swing.JComponent;

import javax.swing.JPanel;

public class Main {
    /**
     * This method creates a JPanel which layouts given components in a table of
     * given rows/columns. Last column is considered main, so its components
     * will be expanded to fill extra space.
     */
    public static JPanel makeTablePanel(int rows, int cols, JComponent components[]) {
        return makeTablePanel(rows, cols, cols - 1, components);
    }

    /**
     * This method creates a JPanel which layouts given components in a table of
     * given rows/columns. Specified column is considered main, so its
     * components will be expanded to fill extra space.
     */
    public static JPanel makeTablePanel(int rows, int cols, int mainColumn, JComponent components[]) {

        GridBagLayout layout = new GridBagLayout();
        JPanel panel = new JPanel(layout);
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(0, 0, 5, 5);

        for (int row = 0; row < rows; row++)
            for (int col = 0; col < cols; col++) {

                constraints.gridx = col;
                constraints.gridy = row;
                constraints.weightx = (col == mainColumn) ? 1 : 0;
                JComponent component = components[row * cols + col];
                panel.add(component);
                layout.setConstraints(component, constraints);
            }

        /*
         * Create one extra invisible component, which is vertically expandable.
         * This will align the whole table to the top.
         */
        JComponent space = (JComponent) Box.createGlue();
        constraints.gridx = 0;
        constraints.gridy = rows;
        constraints.weightx = 0;
        constraints.weighty = 1;
        panel.add(space);
        layout.setConstraints(space, constraints);

        return panel;
    }
}

Related

  1. isColumnDefinitionChange(TableModelEvent e)
  2. isColumnSorted(JTable table, int column)
  3. isNotDuplicateKeyOfTable(String text, TableModel model, int column)
  4. layoutColumns(JTable p_Table, boolean p_OnlyVisibleRows)
  5. loadToTableModel(String fileName, Object[] columns)
  6. newTable(String name, Object[][] rowData, Object[] columnNames)
  7. pack(JTable table, boolean packColumns, boolean packRows, int padding)
  8. ponerAncho(TableColumn loColumn, int plAncho)
  9. removeColumn(int index, JTable table)