Making Table With List Of JPanels ( With Applying

2019-05-20 21:49发布

In this post Making Table With List Of JPanels I can make a Table with lists of panel. And this table has many features but I need to a new feature and it is applying percentage columns.It means I like to width of all columns not equals and with a array of floats determines.
This is my class table :

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

public class PanelTable extends JPanel {
    private String data[][];
    private String columns[];
    private int numberOfRows;
    private int numberOfColumns;
    private JTextArea[] header;
    private JTextArea[][] cell;

    public PanelTable(String data[][], String columns[]) {
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        this.data = data;
        this.columns = columns;


        numberOfRows = data.length;
        numberOfColumns = columns.length;

        makeHeader();
        makeCells();
    }

    public void makeCells() {
        cell = new JTextArea[numberOfRows][numberOfColumns];
        for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++) {
            add(getRow(numberOfColumns, rowIndex));
        }
    }

    public void makeHeader() {
        header = new JTextArea[numberOfColumns];


        for (int columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) {
            JTextArea ta = new JTextArea();
            ta.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
            ta.setBackground(new Color(57, 77, 112));
            ta.setForeground(Color.WHITE);
            ta.setText(columns[columnIndex]);
            ta.setFont(new Font("Calibri Light", Font.BOLD, 25));
            ta.setLineWrap(true);
            ta.setWrapStyleWord(true);
            ta.setEditable(false);
            header[columnIndex] = ta;
        }

        JPanel rowHeader = new JPanel();
        rowHeader.setLayout(new BoxLayout(rowHeader, BoxLayout.X_AXIS));

        for (int i = 0; i < numberOfColumns; i++) {
            rowHeader.add(header[i]);
        }
        add(rowHeader);
    }


    public Component getRow(int numberOfColumns, int rowIndex) {

        JPanel row = new JPanel();
        //use GridLayout if you want equally spaced columns
        row.setLayout(new BoxLayout(row, BoxLayout.X_AXIS));
        for (int columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) {
            row.add(getCell(rowIndex, columnIndex));
        }
        return row;
    }

    public Component getCell(int rowIndex, int columnIndex) {
        if (cell[rowIndex][columnIndex] == null) {
            JTextArea ta = new JTextArea();
            ta.setLineWrap(true);
            ta.setWrapStyleWord(true);
            ta.setEditable(false);
            ta.setSize(50, ta.getHeight());

            ta.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
            ta.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            ta.setText(data[rowIndex][columnIndex]);
            ta.setFont(new Font("Bmitra", Font.BOLD, 15));

            cell[rowIndex][columnIndex] = ta;
        }

        return cell[rowIndex][columnIndex];
    }

}

and this is a gui example for testing :

import javax.swing.*;
import java.awt.*;

public class GUI extends JFrame {
    private PanelTable table;
    private JScrollPane scrollPane;

    public GUI(){
        super("Bank Table");
        JPanel contentPanel = new JPanel();
        setContentPane(contentPanel);
        contentPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        setLayout(new BorderLayout());
        setMinimumSize(new Dimension(1000,700));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        String[][] data= new String[][]{{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/01"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/02"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/03"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/04"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/05"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/06"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/07"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/08"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/09"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/10"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/11"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/12"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/13"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/14"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/15"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/16"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/17"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/18"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/19"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/20"},
                {"0212670003009", "ص 318", "77081634","380dddddddddddddddddddddddddddddddddddddddddddddddddddddddd00000","0","331142","2102","وصول چک","08:56:46","1397/05/21"},
                {"0212670003009", "ص 318", "77081634","380dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd00000","0","331142","2102","وصول چک","08:56:46","1397/05/22"},
                {"0212670003009", "ص 318", "77081634","3800ddddddddddddddddddddddddddddddddddddddddd0000","0","331142","2102","وصول چک","08:56:46","1397/05/23"},
                {"0212670003009", "ص 318", "77081634","38000ddddddddddddddddddddddddddddddddddddddddddddddddddd000","0","331142","2102","وصول چک","08:56:46","1397/05/24"},
                {"0212670003009", "ص 318", "77081634","38000ddddddddddddddddddddddddddddddddddddddd000","0","331142","2102","وصول چک","08:56:46","1397/05/25"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/26"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/15"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/16"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/17"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/18"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/19"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/20"},
                {"0212670003009", "ص 318", "77081634","380dddddddddddddddddddddddddddddddddddddddddddddddddddddddd00000","0","331142","2102","وصول چک","08:56:46","1397/05/21"},
                {"0212670003009", "ص 318", "77081634","380dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd00000","0","331142","2102","وصول چک","08:56:46","1397/05/22"},
                {"0212670003009", "ص 318", "77081634","3800ddddddddddddddddddddddddddddddddddddddddd0000","0","331142","2102","وصول چک","08:56:46","1397/05/23"},
                {"0212670003009", "ص 318", "77081634","38000ddddddddddddddddddddddddddddddddddddddddddddddddddd000","0","331142","2102","وصول چک","08:56:46","1397/05/24"},
                {"0212670003009", "ص 318", "77081634","38000ddddddddddddddddddddddddddddddddddddddd000","0","331142","2102","وصول چک","08:56:46","1397/05/25"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/26"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/15"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/16"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/17"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/18"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/19"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/20"},
                {"0212670003009", "ص 318", "77081634","380dddddddddddddddddddddddddddddddddddddddddddddddddddddddd00000","0","331142","2102","وصول چک","08:56:46","1397/05/21"},
                {"0212670003009", "ص 318", "77081634","380dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd00000","0","331142","2102","وصول چک","08:56:46","1397/05/22"},
                {"0212670003009", "ص 318", "77081634","3800ddddddddddddddddddddddddddddddddddddddddd0000","0","331142","2102","وصول چک","08:56:46","1397/05/23"},
                {"0212670003009", "ص 318", "77081634","38000ddddddddddddddddddddddddddddddddddddddddddddddddddd000","0","331142","2102","وصول چک","08:56:46","1397/05/24"},
                {"0212670003009", "ص 318", "77081634","38000ddddddddddddddddddddddddddddddddddddddd000","0","331142","2102","وصول چک","08:56:46","1397/05/25"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/26"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/15"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/16"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/17"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/18"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/19"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/20"},
                {"0212670003009", "ص 318", "77081634","380dddddddddddddddddddddddddddddddddddddddddddddddddddddddd00000","0","331142","2102","وصول چک","08:56:46","1397/05/21"},
                {"0212670003009", "ص 318", "77081634","380dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd00000","0","331142","2102","وصول چک","08:56:46","1397/05/22"},
                {"0212670003009", "ص 318", "77081634","3800ddddddddddddddddddddddddddddddddddddddddd0000","0","331142","2102","وصول چک","08:56:46","1397/05/23"},
                {"0212670003009", "ص 318", "77081634","38000ddddddddddddddddddddddddddddddddddddddddddddddddddd000","0","331142","2102","وصول چک","08:56:46","1397/05/24"},
                {"0212670003009", "ص 318", "77081634","38000ddddddddddddddddddddddddddddddddddddddd000","0","331142","2102","وصول چک","08:56:46","1397/05/25"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/26"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/27"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/28"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/29"},
                {"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/30"}};


        String[] columns= new String[]{"شماره حساب", "اطلاعات اضافه", "مانده","واریز","برداشت","فیش/ حواله","کد شعبه","شرح","ساعت","تاریخ"};
        System.out.println("Hey");
        PanelTable table = new PanelTable(data,columns);
        System.out.println("!!!");

        scrollPane = new JScrollPane(table);
        scrollPane.getVerticalScrollBar().setUnitIncrement(50);

        add(scrollPane,BorderLayout.CENTER);
//        JTextArea textArea = new JTextArea();
//        textArea.setLineWrap(true);
//        textArea.setWrapStyleWord(true);
//        textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
//        textArea.setText("Alireza");
//        add(textArea,BorderLayout.NORTH);

        setVisible(true);
    }

    public static void main(String args[]){
        GUI gui =new GUI();
    }


}

2条回答
再贱就再见
2楼-- · 2019-05-20 22:05

Do this: First create an array of width size. for example :

 final int[] columnsWidth = { 300, 100, 50, 300, 50 };

then on your method makeHeader and getCell, where you are setting all the attributes of text area, set preferred size also. See the code below. This should give you the width based on the size array.

ta.setPreferredSize(new Dimension(columnsWidth[columnIndex], ta.getHeight())); 

Let me know if it works for you :)

Below is the complete code: For GUI:

import javax.swing.*;
import java.awt.*;

public class GUI extends JFrame {
    private PanelTable2 table;
    private JScrollPane scrollPane;

    public GUI(){
        super("Bank Table");
        JPanel contentPanel = new JPanel();
        setContentPane(contentPanel);
        contentPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        setLayout(new BorderLayout());
        setMinimumSize(new Dimension(1000,700));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final String[] columns = { "Big Column", "Serodys", "Ramírez", "Entrada", "MercaSur" };


        final String[][] data = {{"2323", "Hundreds","2323", "Hundreds","2323"},{"2323", "Hundreds","2323", "Hundreds","2323"}}; 
        // final PanelTable22 panel = new PanelTable22(data, columns);

        System.out.println("Hey");
        PanelTable2 table = new PanelTable2(data,columns);
        System.out.println("!!!");

        scrollPane = new JScrollPane(table);
        scrollPane.getVerticalScrollBar().setUnitIncrement(50);

        add(scrollPane,BorderLayout.CENTER);

        setVisible(true);
    }

    public static void main(String args[]){
        GUI gui =new GUI();
    }


}

For Table Model:

import java.awt.Color;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class PanelTable2 extends JPanel {
    private String data[][];
    private String columns[];
    private int numberOfRows;
    private int numberOfColumns;
    private JTextArea[] header;
    private JTextArea[][] cell;
    final int[] columnsWidth = { 300, 100, 50, 300, 50 };


    public PanelTable2(String data[][], String columns[]) {
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        this.data = data;
        this.columns = columns;


        numberOfRows = data.length;
        numberOfColumns = columns.length;

        makeHeader();
        makeCells();
    }

    public void makeCells() {
        cell = new JTextArea[numberOfRows][numberOfColumns];
        for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++) {
            add(getRow(numberOfColumns, rowIndex));
        }
    }

    public void makeHeader() {
        header = new JTextArea[numberOfColumns];


        for (int columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) {
            JTextArea ta = new JTextArea();
            ta.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 
            ta.setPreferredSize(new Dimension(columnsWidth[columnIndex], ta.getHeight())); 
            ta.setBackground(new Color(57, 77, 112));
            ta.setForeground(Color.WHITE);
            ta.setText(columns[columnIndex]);
            ta.setFont(new Font("Calibri Light", Font.BOLD, 25));
            ta.setLineWrap(true);

            ta.setWrapStyleWord(true);
            ta.setEditable(false);
            header[columnIndex] = ta;
        }

        JPanel rowHeader = new JPanel();
        rowHeader.setLayout(new BoxLayout(rowHeader, BoxLayout.X_AXIS));

        for (int i = 0; i < numberOfColumns; i++) {
            rowHeader.add(header[i]);
        }
        add(rowHeader);
    }


    public Component getRow(int numberOfColumns, int rowIndex) {

        JPanel row = new JPanel();
        //use GridLayout if you want equally spaced columns
        row.setLayout(new BoxLayout(row, BoxLayout.X_AXIS));
        for (int columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) {
            row.add(getCell(rowIndex, columnIndex));
        }
        return row;
    }

    public Component getCell(int rowIndex, int columnIndex) {
        if (cell[rowIndex][columnIndex] == null) {
            JTextArea ta = new JTextArea();
            ta.setLineWrap(true);
            ta.setWrapStyleWord(true);
            ta.setEditable(false);

            ta.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
            ta.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            ta.setText(data[rowIndex][columnIndex]);
            ta.setFont(new Font("Bmitra", Font.BOLD, 15));
            ta.setPreferredSize(new Dimension(columnsWidth[columnIndex], ta.getHeight())); 

            cell[rowIndex][columnIndex] = ta;
        }

        return cell[rowIndex][columnIndex];
    }

}
查看更多
Juvenile、少年°
3楼-- · 2019-05-20 22:13

The key are these rows :

//use GridLayout if you want equally spaced columns
row.setLayout(new BoxLayout(row, BoxLayout.X_AXIS));

to apply constrains to columns you can use GridBagLayout instead of BoxLayout.

Here is a very basic mcve of using GridBagLayout:

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class PanelTable2 extends JPanel {

    public PanelTable2() {

        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        setPreferredSize(new Dimension(400, 150));
        add(getRow());
    }

    Component getRow() {

        int[] percent =  {10,40,50};

        JPanel row = new JPanel();
        GridBagLayout layout = new GridBagLayout();
        row.setLayout(layout);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.CENTER;

        for (int i= 0; i < percent.length; i++) {
            gbc.gridy = 0 ; gbc.gridx = i;
            gbc.weightx = percent[i];
            Component component =  getCell();
            row.add(component, gbc);
        }
        return row;
    }

    Component getCell() {

        JTextArea ta = new JTextArea(4,11);
        ta.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        ta.setFont(new Font("Bmitra", Font.BOLD, 15));
        return ta;
    }

    public static void main(String[] args) throws IOException{

        JFrame window = new JFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.add(new PanelTable2());
        window.pack();
        window.setVisible(true);
    }
}

Note that the mcve should not represent your application. It should be a simple representation of the problem.
GridBagLayout is by no means the only option. There are other alternatives you can explore like SpringLayout.

查看更多
登录 后发表回答