Java Need help How can i define table width?

newbie

New Coder
Aug 20, 2019
2
0
2
I create cells from the list size and trying to set width fist 1 only number 2nd is name and next others will be only number which means i need 2nd line more spaced and others are same width. Sorry my english
 

Ghost

Active Coder
Moderator
Apr 19, 2019
317
142
43
This example isnt for Java, but covers a lot of the same iText concepts

Code:
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
cell.Colspan = 3;
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
table.AddCell(cell);
table.AddCell("Col 1 Row 1");
table.AddCell("Col 2 Row 1");
table.AddCell("Col 3 Row 1");
table.AddCell("Col 1 Row 2");
table.AddCell("Col 2 Row 2");
table.AddCell("Col 3 Row 2");
doc.Add(table);
Here is an example I found online for iText Sharp

What version of iText are you using ??
 

YNW_Kyle

New Coder
Aug 28, 2019
3
0
2
Use this method:

Java:
public static void setColumnWidths(JTable table, int... widths) {

    TableColumnModel columnModel = table.getColumnModel();

    for (int i = 0; i < widths.length; i++) {

        if (i < columnModel.getColumnCount()) {

            columnModel.getColumn(i).setMaxWidth(widths);

        }

        else break;

    }
Or extend the JTable class:

Java:
public class Table extends JTable {

    public void setColumnWidths(int... widths) {

        for (int i = 0; i < widths.length; i++) {

            if (i < columnModel.getColumnCount()) {

                columnModel.getColumn(i).setMaxWidth(widths);

            }

            else break;

        }

    }

}
And then:

table.setColumnWidths(30, 150, 100, 100);
 
Last edited by a moderator: