JavaFX table contours of the table - java

Hi could you help me how I can do contours of the table. I want that all rows in table have contours .Thanks for help. My table look like this :
TableColumn busNumberCol = new TableColumn("Linia");
busNumberCol.setCellValueFactory(
new PropertyValueFactory<>("busNumber"));
busNumberCol.setPrefWidth(tb.getPrefWidth()/5);
TableColumn courseCol = new TableColumn("Kierunek");
courseCol.setCellValueFactory(
new PropertyValueFactory<>("nameBusStpo"));
courseCol.setPrefWidth((tb.getPrefWidth()-tb.getPrefWidth()/5)/2-1);
TableColumn departureCol = new TableColumn("Odjazd");
departureCol.setPrefWidth((tb.getPrefWidth()-tb.getPrefWidth()/5)/2-1);
departureCol.setCellValueFactory(
new PropertyValueFactory<>("busTimetable"));
table.setPrefHeight(tb.getPrefHeight());
table.setStyle("-fx-background-color: orange");
table.setPrefWidth(tb.getPrefWidth());
table.setItems(list);
table.getColumns().addAll(busNumberCol, courseCol, departureCol);
table.setPlaceholder(new Label(""));

You can use:
.table-row-cell{
-fx-border-color:red;
-fx-border-width:1.0;
/* -fx-background-color */
}
Mention that it this adds a border around every row.For background color you can uncomment the above with /* -fx-background-color*/
Also
For more styling on TableView look Change JavaFX TableView font size

try this in the css(for me is working):
.table-row-cell{
-fx-background-color: -fx-table-cell-border-color, /*coloryouwant*/;
}

Related

Text not getting center aligned horizontally in itext? [duplicate]

I have a C# application that generates a PDF invoice. In this invoice is a table of items and prices. This is generated using a PdfPTable and PdfPCells.
I want to be able to right-align the price column but I cannot seem to be able to - the text always comes out left-aligned in the cell.
Here is my code for creating the table:
PdfPTable table = new PdfPTable(2);
table.TotalWidth = invoice.PageSize.Width;
float[] widths = { invoice.PageSize.Width - 70f, 70f };
table.SetWidths(widths);
table.AddCell(new Phrase("Item Name", tableHeadFont));
table.AddCell(new Phrase("Price", tableHeadFont));
SqlCommand cmdItems = new SqlCommand("SELECT...", con);
using (SqlDataReader rdrItems = cmdItems.ExecuteReader())
{
while (rdrItems.Read())
{
table.AddCell(new Phrase(rdrItems["itemName"].ToString(), tableFont));
double price = Convert.ToDouble(rdrItems["price"]);
PdfPCell pcell = new PdfPCell();
pcell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
pcell.AddElement(new Phrase(price.ToString("0.00"), tableFont));
table.AddCell(pcell);
}
}
Can anyone help?
I'm the original developer of iText, and the problem you're experiencing is explained in my book.
You're mixing text mode and composite mode.
In text mode, you create the PdfPCell with a Phrase as the parameter of the constructor, and you define the alignment at the level of the cell. However, you're working in composite mode. This mode is triggered as soon as you use the addElement() method. In composite mode, the alignment defined at the level of the cell is ignored (which explains your problem). Instead, the alignment of the separate elements is used.
How to solve your problem?
Either work in text mode by adding your Phrase to the cell in a different way.
Or work in composite mode and use a Paragraph for which you define the alignment.
The advantage of composite mode over text mode is that different paragraphs in the same cell can have different alignments, whereas you can only have one alignment in text mode. Another advantage is that you can add more than just text: you can also add images, lists, tables,... An advantage of text mode is speed: it takes less processing time to deal with the content of a cell.
private static PdfPCell PhraseCell(Phrase phrase, int align)
{
PdfPCell cell = new PdfPCell(phrase);
cell.BorderColor = BaseColor.WHITE;
// cell.VerticalAlignment = PdfCell.ALIGN_TOP;
//cell.VerticalAlignment = align;
cell.HorizontalAlignment = align;
cell.PaddingBottom = 2f;
cell.PaddingTop = 0f;
return cell;
}
Here is my derivation of user2660112's answer - one method to return a cell for insertion into a bordered and background-colored table, and a similar, but borderless/colorless variety:
private static PdfPCell GetCellForBorderedTable(Phrase phrase, int align, BaseColor color)
{
PdfPCell cell = new PdfPCell(phrase);
cell.HorizontalAlignment = align;
cell.PaddingBottom = 2f;
cell.PaddingTop = 0f;
cell.BackgroundColor = color;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
return cell;
}
private static PdfPCell GetCellForBorderlessTable(Phrase phrase, int align)
{
PdfPCell cell = new PdfPCell(phrase);
cell.HorizontalAlignment = align;
cell.PaddingBottom = 2f;
cell.PaddingTop = 0f;
cell.BorderWidth = PdfPCell.NO_BORDER;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
return cell;
}
These can then be called like so:
Font timesRoman9Font = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 9, BaseColor.BLACK);
Font timesRoman9BoldFont = FontFactory.GetFont(FontFactory.TIMES_BOLD, 9, BaseColor.BLACK);
Phrase phrasesec1Heading = new Phrase("Duckbills Unlimited", timesRoman9BoldFont);
PdfPCell cellSec1Heading = GetCellForBorderedTable(phrasesec1Heading, Element.ALIGN_LEFT, BaseColor.YELLOW);
tblHeadings.AddCell(cellSec1Heading);
Phrase phrasePoisonToe = new Phrase("Poison Toe Toxicity Level (Metric Richter Scale, adjusted for follicle hue)", timesRoman9Font);
PdfPCell cellPoisonToe = GetCellForBorderlessTable(phrasePoisonToe, Element.ALIGN_LEFT);
tblFirstRow.AddCell(cellPoisonToe);
I ended up here searching for java Right aligning text in PdfPCell. So no offense if you are using java please use given snippet to achieve right alignment.
private PdfPCell getParagraphWithRightAlignCell(Paragraph paragraph) {
PdfPCell cell = new PdfPCell(paragraph);
cell.setBorderColor( BaseColor.WHITE);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
return cell;
}
In getParagraphWithRightAlignCell pass paragraph
Thanks
Perhaps its because you are mixing the different ways to add the cells? Have you tried explicitly creating a cell object, massaging it however you want then adding it for every cell?
Another thing you could try is setting the vertical alignment as well as the horizontal.
cell.HorizontalAlignment = Element.ALIGN_RIGHT;

Can I gain granular control of how a row is rendered in JAVA?

I am building a table in JAVA but some rows need to be completely different than most based on options.
In PHP I could loop a data set and apply an IF statement and change how a row is renedred in a webpage
while($r = $sth->fetch()){
if ($r['status']=='good'){
echo '<tr><td>This is good</td><td>yes it is</td></tr>';
}
else{
echo '<tr><td colspan="2">this is bad</td></tr>';
}
}
I only know one way to render a table in JAVA:
TableView<StatuTable> table = new TableView();
TableColumn<StatuTable, String> statusColumn = new TableColumn<>("Status");
statusColumn .setCellValueFactory(new PropertyValueFactory<>("status"));
TableColumn<StatuTable, String> noteColumn = new TableColumn<>("Note");
noteColumn .setCellValueFactory(new PropertyValueFactory<>("note"));
//loop the statusGroup defined elsewhere
ObservableList<StatusTable> statusList = FXCollections.observableArrayList();
for (Status status: statusGroup.getStatusRows()){
Can I alter the table row here:
if (status.getStatusType.equals("good")){
//make a different kind of row.
}
statusList.add(status);
}
table .setItems(statusList);
//this part is black magic as far as i am concerned
scheduleTable.getColumns().addAll(statusColumn, noteColumn);
Borrowing off of the example in this answer (https://stackoverflow.com/a/16864130/866021), you can do this via the getStyleClass() function on your TableRow.
Add an appropriate style class to your row:
nameRow.getStyleClass().add("italic");
Add a stylesheet to your scene which defines the italic style for table cells:
.italic.table-cell { -fx-font-style: italic; }

Libgdx adding a cell to a table

I'm trying to add label here
Label missionTitle = new Label(mission.title, skin);
Label missionDesct = new Label(mission.desct, skin);
Label remainDay = new Label(TaskManager.getRemainMissionDay(mission.deadLine) + "day remain", skin);
missionDesct.setFontScale(width * 1.4f / 1080);
remainDay.setFontScale(width * 1.2f / 1080);
missionDesct.setWrap(true);
Table table = new Table();
table.debug();
table.background(new TextureRegionDrawable(new TextureRegion(new Texture("image/imgray.png"))));
table.add(missionTitle).width(width / 1.1f);
table.row();
table.add(missionDesct).width(width / 1.1f);
table.row();
table.add(remainDay).width(width/2.3f).left();
for that i'm adding this lines;
Label goldLabel = new Label("30 gold", skin);
table.add(goldLabel);
But result is;
this
How can i add a new cell over there?
I tried to adding ".width(width/2);" but it's not working.
...
table.row();
table.add(remainDay).width(width/2.3f).left();
Label goldLabel = new Label("30 gold", skin);
table.add(goldLabel).width(width/2.3f);
You can do that by creating a table with the 2 labels and then adding the table into the container table.
Example -
Table nestedTable = new Table();
nestedTable.add(remainDay).width(THE_WIDTH);
nestedTable.add(goldLabel).width(THE_WIDTH);
mainTable.add(nestedTable);
You can play around more with this to get the desired result :)
This class can also help - HorizontalGroup
Finally i understand my mistake.I was trying to add 2 cell to a row.But that was effecting the other row's cell count.I changed my code to this and solved;
Table table = new Table();
table.background(new TextureRegionDrawable(new TextureRegion(new Texture("image/imgray.png"))));
table.add(missionTitle).width(width/1.5f).left();
table.add(new TextButton("!", skin)).height(height/23).width(width / 5).right();
table.row().fillX();
table.add(missionDesct).expandX();
table.add(goldAndExp).right();
table.row();
table.add(remainDay).left();
Screenshot
I'll be happy if someone fix my grammar problems.

Libgdx - Creating larger background for scrollpane

I'm trying to create a scrollpane that has a background. However, I want the background to take up more than the area of the scrollpane. For instance, Here is my background and colored in red is where I want the scrollpane to be:
However, when I add buttons I get the following result:
How can I limit the actual scrollpane part to just a section (colored in red in the above picture)?
Here is what I have so far. I toyed around with spacing/padding but that did not produce any good results:
Skin skin = Resources.getSkin(Resources.SKIN_JSON);
container = new Table();
container.setSize(Resources.VIRTUAL_WIDTH, Resources.VIRTUAL_HEIGHT);
this.addActor(container);
Table table = new Table();
// table.debug();
final ScrollPane scroll = new ScrollPane(table, skin);
table.pad(10).defaults().expandX().space(4);
table.setSize(Resources.VIRTUAL_WIDTH, Resources.VIRTUAL_HEIGHT);
container.add(scroll).expand().fill().colspan(1);
container.row().space(10).padBottom(10);
Image background = new Image(Resources.getAtlas(Resources.SKIN_ATLAS).findRegion("main-panel-horz"));
container.setBackground(background.getDrawable());
TextButton button1 = new TextButton("Button1", skin);
table.add(button1);
table.row();
TextButton button2 = new TextButton("button2", skin);
table.add(button2);
table.row();
TextButton button3 = new TextButton("button3", skin);
table.add(button3);
table.row();
TextButton button4 = new TextButton("button4", skin);
table.add(button4);
What you need to do is pad the outer table to get its contents to fit in the region of the background that you would like. (Or you could alternatively pad the cell that you put the scroll pane in.)
If you use a 9-patch drawable for the background, the padding of the outer table will be done for you automatically. You could also specify a TextureRegionDrawable in your skin Json and use that as the background. That would also allow you to pad the table automatically.
You can easily get texture region drawables from the skin with skin.getDrawable(drawableName);--no need to create an intermediate Image just to create a drawable out of a region.
Here's how you would create a TextureRegionDrawable with explicit padding in json:
com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable: {
main-panel-horz-padded: { region: main-panel-horz, leftWidth: 50, rightWidth: 50, topHeight: 50, bottomHeight: 50 }
},
Then in your code:
container.setBackground(skin.getDrawable("main-panel-horz-padded"));

JAVAFX Tableview Split cell (or) Cell Span

How to put common table heading for two columns in JAVAFX
Like the image below
Any help would really be appreciated! Thanks!
You can try below code
TableColumn parentCol = new TableColumn("Parent");
TableColumn firstCol = new TableColumn("First");
TableColumn secondCol = new TableColumn("Second");
parentCol.getColumns().addAll(firstCol , secondCol );

Categories