Horizontal alignment not working in itextpdf layout cell - java

I'm currently working on a table and want all the values in each cell to be centered.
My current code is this:
cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
cell.setHorizontalAlignment(HorizontalAlignment.CENTER);
cell.add(new Paragraph(text).setRotationAngle(hasRotation? rotation : 0));
cell.setBorder(Border.NO_BORDER);
return cell;
But my problem is, that the horizontal alignment doesn't work. It centers the text on the vertical length of the cell, but it's bound to the left side of the cell, even though I specified the horizontal alignment to be centered. You can see what I mean in the image below. Any help would be apprechiated

For anyone interested. The problem was, that you can't set the rotation and set the text to be centered. I replaced the code with this, which seems to work for me:
cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
Paragraph p = new Paragraph(text);
p.setFontSize(fontSize);
p.setTextAlignment(TextAlignment.CENTER);
if (hasRotation) {
p.setRotationAngle(rotation);
p.setPaddingTop(factor * (cellWidth / 2) - fontSize / 1.5f);
}
cell.setBorder(Border.NO_BORDER);
cell.add(p);
return cell;

Related

How make a TableView automatically change its height based on its content in JavaFX?

If the Table has more content, it displays its content within a ScrollPane.
If the TableView has less content, it fills itself with empty space.
How to achieve adaptive?
I can't use fixedCellSize,because my cell is wrapping.
We do this for Width just try it for Height
if(TableData.size() < 15) {// Format TableView to display Vertical ScrollBar
ptable.setPrefWidth(336);// 19 is the off set
}else {
ptable.setPrefWidth(355);
} ptable.setItems(TableData);

Vaadin Grid how to wrap long text in columns

I have a Vaadin grid with 7 columns:
Grid grid = new Grid<>();
grid.setSizeFull();
grid.addColumn(User::getUserName).setCaption("Name").setExpandRatio(2);
grid.addColumn(User::getLastName).setCaption("Last Name").setExpandRatio(1);
grid.addColumn(User::getAge).setCaption("Age").setExpandRatio(2);
grid.addColumn(User::getWork).setCaption("Work").setExpandRatio(1);
grid.addColumn(User::getJobTitle).setCaption("Job Title").setExpandRatio(1);
grid.addColumn(User::getSalary).setCaption("Salary").setExpandRatio(1);
grid.addColumn(User::getOther).setCaption("Other").setExpandRatio(1);
What I need is to set columns width in a way - that all 7 will have be shown on a screen.
With my code now it works in a way that if text content of any column cell is very long - the last columns are not shown on the screen and screen must be scrolled horizontally.
I tried to use method setWidth() and as it takes value in pixels the grid view may differ on various browsers and screens.
What I need is to be sure that my grid looks the same way on different screens and with different cell values.
Recently I had the same situation, this was my solution:
Grid configuration
Grid<Item> gridItem = new Grid<>();
gridItem.setRowHeight(50.0);
Column configuration
gridItem.addComponentColumn(item -> {
Label label = new Label();
label.setValue(item.getText());
label.setWidthUndefined();
label.setStyleName(ValoTheme.LAYOUT_HORIZONTAL_WRAPPING);
return label;
})
.setCaption("Item")
.setWidth(380.0);
Result:

scrollRectToVisible --> need effect on vertical line

I have JTable with two scrollbars (horizontal and vertical). When I use scrollRectToVisible, it return me Rectangle and this action cause that vertical and horizontal scrollbars automatically move to specified rows/columns. How can I move automatically vertical scrollbar to desired place and horizontal scrollbar should stay inactive? This method should show me selected row but my JTable is very wide and I would like to avoid automatically movement (horizontal) at the beginning (left side) of track -> simple say, I expect that horizontal position is unchanged.
public void goToSelected() {
int selectedRow = this.getSelectedRow();
if (selectedRow >= 0)
this.scrollRectToVisible(this.getCellRect(selectedRow, 0, true));
}
The horizontal position might change because you are specifying a column index (0) and the specified column might be invisible. You can simply combine the values of the currently visible area and the cell’s vertical range to get the desired effect:
Rectangle target = getCellRect(selectedRow, 0, true), vis = getVisibleRect();
target.x = vis.x;
target.width = vis.width;
scrollRectToVisible(target);

Libgdx Label multiline text height

i have done a small test on LibGdx, on Multi-line Label, it seems that i cant get the wrapped line's height. Following is the code. Theoretically, height for aLebel should be > bLabel. But the result appear the same.
code:
aLabel.setText("this is a super long long long text that need wrapping."); // line wrapped into 3 lines
aLabel.setWrap(true);
aLabel.setWidth(470);
doLog("aLabel.getHeight(): " + aLabel.getHeight());
bLabel.setText("this is short."); // unwrapped line
bLabel.setWrap(true);
bLabel.setWidth(470);
doLog("bLabel.getHeight(): " + bLabel.getHeight());
result:
aLabel.getHeight(): 45.0
bLabel.getHeight(): 45.0
Do anyone have any idea how to get the actual multi-line height in LibGdx? Thanks in advance.
I had this issue for years and accidentally solved it by setting the width and packing the label twice. Note that multiline labels were never intended to figure out their own width, so you have to set them externally, preferably from it's parent.
public Label createLabel() {
// Create label and set wrap
Label label = new Label("Some long string here...", skin);
label.setWrap(true);
// Pack label
label.pack(); // This might not be necessary, unless you're changing other attributes such as font scale.
// Manual sizing
label.setWidth(textWidth); // Set the width directly
label.pack(); // Label calculates it's height here, but resets width to 0 (bug?)
label.setWidth(textWidth); // Set width again
return label;
}
LibGDX version used: 1.6.4
Pack sizes the widget to its pref size, nothing more. Pref width of a label with wrapping is 0.
Label label = new Label(...);
label.setWrap(true);
label.setWidth(123);
label.setHeight(label.getPrefHeight());
I had the same issue and it seems there doesn't exist a method in Label class to solve this. Also, I agree with you, the getHeight() method should return the real height of the Actor, so I don't know if that's a bug or there is a reasoning behind that behaviour.
Anyways, how I solved the issue is by using BitmapFont's getWrappedBounds method. It's not short, but for your example it would be the following:
doLog("aLabel.getHeight(): " + aLabel.getStyle().font.getWrappedBounds(aLabel.getText(), aLabel.getWidth()).height);
This could be done by adding a restriction to the cell that contains the Label in the Table:
Label label = new Label("Example", new Label.LabelStyle(font, Color.WHITE));
label.setWrap(true);
Table table = new Table();
table.add(label).width(WITH);
For more information about how to use Table go to: https://github.com/libgdx/libgdx/wiki/Table

Java Swing; Problems with Horizontal Scroll for a JTable embedded in a JScrolledPane

I followed some tips in this guide: JTable with horizontal scrollbar but still having problems.
I have two column headers: Name and Description (for our purposes in this ex.)
I want to be able to scroll horizontally whenever an entry is added (ie: Name/Description is just 1 line). However, Swing doesn't seem to default to that behavior!
I have a JTable embedded in a ScrollPane. JScrollPane has the following parameters: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED and JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
If I disable with: myTable.AutoResizeMode(AUTO_RESIZE_OFF) the column headers don't fill up the whole table (maybe just 1/2 of the table). I have to manually resize the "Description" Column to see the whole thing.
How can I have autoresizing, but the horizontal scrolling still works?
You can extend JTable as follows:
public class JHorizontalFriendlyTable extends JTable {
#Override
public Dimension getPreferredSize() {
if (getParent () instanceof JViewport) {
if (
((JViewport) getParent()).getWidth() > super.getPreferredSize().width)
) {
return getMinimumSize();
}
}
return super.getPreferredSize();
}
#Override
public boolean getScrollableTracksViewportWidth () {
if (autoResizeMode != AUTO_RESIZE_OFF) {
if (getParent() instanceof JViewport) {
return (((JViewport) getParent()).getWidth() > getPreferredSize().width);
}
return true;
}
return false;
}
}
sample usage:
jScrollPane1 = new JScrollPane();
TableModel jTable1Model = new DefaultTableModel(...);
JTable jTable1 = new JHorizontalFriendlyTable();
jScrollPane1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
jScrollPane1.setViewPortView(jTable1);
jTable1.setModel(jTable1Model);
jTable1.setPreferredSize(new java.awt.Dimension(1051,518));
jTable1.setPreferredScrollableViewPortSize(new java.awt.Dimension(1000,528));
jTable1.getSize(new java.awt.Dimension(1051, 528));
if (jTable1.getPreferredScrollableViewPortSize().getWidth() >
((JViewPort) jTable1.getParent()).getPreferredSize().getWidth())
{
jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
jTable1.doLayout();
}
jTable1.setDragEnabled(false);
jTable1.setColumnSelectionAllowed(false);
jTable1.getTableHeader().setReorderingAllowed(false);
Auto resize means automatically resizing to avoid scrolling.
So the answer is no with standard JTable.
Check out JXTable and see if it does what you want (I haven't used it myself).
One approach might be to use setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS) when you create the table and then use setPreferredScrollableViewportSize() to specify your desired size. After you pack() the window, use setAutoResizeMode(JTable.AUTO_RESIZE_OFF) to allow horizontal scrolling.
If I disable with:
myTable.AutoResizeMode(AUTO_RESIZE_OFF)
the column headers don't fill up the
whole table (maybe just 1/2 of the
table).
By default each column is only 75 pixels wide. You can change the width of the description column to be whatever you want. Read the JTable API and you will find a link to the Swing tutorial on "How to Use Tables" which explains how to do this.
However, even using this approach the column size will not change if the user resizes the frame. It you want to handle this situation then you need to add a ComponentListener to the table (or maybe the scroll pane). Then whenever the componentResized() method is fired you can total the widths of the two columns and then adjust the width of the description column as required to make sure the column total width is not less than the width of the table.
I have to manually resize the
"Description" Column to see the whole
thing.
If you know that your description column will always be large enough to fill the viewport of the scrollpane then you can use the Table Column Adjuster to automatically calculate the maximum width of the description column

Categories

Resources