jxl how to add border to a range of cells? - java

Is there a way to add border to a range of cells, using JXL? Setting border for one cell at a time is quite tedious. Choosing a range of cells and merging them works, but not sure how to set border for a bunch of cells at a time.

There is a way, by merging the cells first then add border to one cell
sheet.mergeCells(ColumnX, rowX, columnY, rowX);
WritableCellFormat cellFormat = new WritableCellFormat();
cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
jxl.write.Label lab11 = new jxl.write.Label(columnX, rowX, "Label", cellFormat);
smrysheet.addCell(lab11);

using WritableCellFormat
WritableCellFormat cellFormat = new WritableCellFormat();
cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
Label label = new Label(col, row, desc, cellFormat);
sheet.addCell(label);

Related

iText 7 borderless table (no border)

This code below does not work.
Table table = new Table(2);
table.setBorder(Border.NO_BORDER);
I am new to iText 7 and all I wanted is to have my table borderless.
Like how to do it?
The table itself is by default not responsible for borders in iText7, the cells are. You need to set every cell to be borderless if you want a borderless table (or set the outer cells to have no border on the edge if you still want inside borders).
Cell cell = new Cell();
cell.add("contents go here");
cell.setBorder(Border.NO_BORDER);
table.addCell(cell);
You could write a method which runs though all children of a Table and sets NO_BORDER.
private static void RemoveBorder(Table table)
{
for (IElement iElement : table.getChildren()) {
((Cell)iElement).setBorder(Border.NO_BORDER);
}
}
This gives you the advantage that you can still use
table.add("whatever");
table.add("whatever");
RemoveBorder(table);
instead of changing it on all cells manual.

How set miminum height for split PdfPCell

On page I have table, and at the end of the page I have one paragraph, if rows contains small count of rows paragraph must still stay at the end of the page, but if rows too much and table take more than one page paragraph must be just after the table end. At first it looks easy for me and I just set minimum height for PdfPCell which contains table, but then I discover if page is split on pages minimum height apply to each part of table, and because of it paragraph isn't under the table on second page. Is there any solution for it?
I found one not very good solution for this problem, I've added one column to the table, which width it too small and it isn't visible for users, in this column I've add table with two rows, for first row I set minimum height which I need, here the code for creating this additional column
private PdfPCell createMinHeight(float minH) {
PdfPCell cell = new PdfPCell();
PdfPTable table = new PdfPTable(1);
cell.setBorder(Rectangle.NO_BORDER);
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
table.setHeaderRows(0);
PdfPCell firstRow = new PdfPCell();
firstRow.setBorder(Rectangle.NO_BORDER);
firstRow.setMinimumHeight(minH);
firstRow.setPadding(0);
table.addCell(firstRow);
table.addCell("");
cell.setPadding(0);
cell.addElement(table);
return cell;
}
If I understand you right, you don't want the paragraph splits? You can use setKeepTogether(boolean); for the elements you need:
paragraph.setKeepTogether(true);
or
table.setKeepTogether(true);

Make scene2d.ui table with alternate row colours?

I have created a scene2d.ui Table and would like to alternate the background colours of each row.
I've had a look through the various table and cell methods but there doesn't seem to be an obvious way of doing it.
Is it even possible? And if so, what's the easiest way of going about it?
I’ve used drawable backgrounds with one colored pixel. I haven’t found easier way even though it is far from elegant.
//Pixmap with one pixel
Pixmap pm1 = new Pixmap(1, 1, Format.RGB565);
pm1.setColor(Color.GREEN);
pm1.fill();
Pixmap pm2 = new Pixmap(1, 1, Format.RGB565);
pm2.setColor(Color.RED);
pm2.fill();
dialogueWindow = getWindow();
dialogueWindow.setTitle("New Game");
// The table that will have the green color
Table row1 = new Table(mySkin);
row1.add(nameStation);
row1.add(nameStationField);
row1.setBackground(new TextureRegionDrawable(new TextureRegion(new Texture(pm1))));
dialogueWindow.add(row1);
dialogueWindow.row();
// The table that will have the green color
Table row2 = new Table(mySkin);
row2.setBackground(new TextureRegionDrawable(new TextureRegion(new Texture(pm2))));
row2.add(cancel).size(120, 60);
row2.add(ok).size(100, 60);
dialogueWindow.add(row2).fillX();
dialogueWindow.row();

How to adjust row width according to data in JTable?

I have a scrollable JTable inside a JDialog and i wish to autoadjust the row width according to data length.
Any ideas?
Use the combination of JTable.setRowHeight method and a cell renderer based on JTextArea
This depends on what is being rendered and if you know the number if pixels...
Changing the column widths will be effected by the column auto resize policy.
If you want to set the pixel width...
int columnIndex = //... the index of the column
TableColumnModel columnModel = table.getColumnModel();
TableColumn tableColumn = columnModel.getColumn(columnIndex);
tableColumn.setWidth(pixelWidth);
If the column is rendering text and you know the number of characters that the column will display you can get the font metrics for the renderer/table...
// If you're using a cell renderer, you will need to get the cell renderer
// to get th font metrics
FontMerics fm = table.getFontMetrics(table.getFont());
int pixelWidth = fm.stringWidth("M") * characterCount;
If you're not using a text based renderer, you can use the renderer to gain some idea of the width...
TableCellRenderer renderer = table.getCellRenderer(0, columnIndex);
// You can also use table.getDefaultRenderer(Class)
Component component = renderer.getTableCellRendererComponent(table,
prototypeValue, // Some value the best represents the average value
false,
false,
0,
columnIndex);
int width = component.getPreferredWidth().width;

JAVA+POI API Excel- Need to increase width of the column

I want to increase the width of the column of excel sheet. as the i am writing trough code is long.
and I need to drag the column manually to see the full text.
I did this –
HSSFRow dataRow = sampleDataSheet.createRow(0);
HSSFCellStyle cellStyle = setHeaderStyle(sampleWorkbook);
cellStyle.setWrapText(true);
***sampleDataSheet.autoSizeColumn(1000000);***
But its not changing anything..
This should work. However,
sampleDataSheet.autoSizeColumn(1000000);
auto-expands column 1000000.
If you want to auto-expand column 0(the first column), use:
sampleDataSheet.autoSizeColumn(0);
To auto-expand column 0 to 9(the first 10 columns):
for (int i=0; i<10; i++){
sampleDataSheet.autoSizeColumn(i);
}
Also, you should create all your rows and fill them with content first, before you call autoSizeColumn(so the column gets the width of the value with the broadest width).
(If you want to set the column width to a fixed value, use HSSFSheet.setColumnWidth(int,int) instead.)
// We can set column width for each cell in the sheet
sheet.setColumnWidth(0, 1000);
sheet.setColumnWidth(1, 7500);
sheet.setColumnWidth(2, 7500);
// By applying style for cells we can see the total text in the cell for specified width
HSSFCellStyle cellStyle = workBook.createCellStyle();
cell.setCellStyle(cellStyle );
cellStyle.setWrapText(true);
sheet.autoSizeColumn(columnNumber) works. this will resize the column to the largest cell length.

Categories