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();
}
}
Do this: First create an array of width size. for example :
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.
Let me know if it works for you :)
Below is the complete code: For GUI:
For Table Model:
The key are these rows :
to apply constrains to columns you can use GridBagLayout instead of
BoxLayout
.Here is a very basic mcve of using
GridBagLayout
: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.