JAVAFX Tableview Split cell (or) Cell Span - java

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 );

Related

Set cell background color and text size using Google Java API

I want to set Spreadsheet cell background color and text size. I use this Java code to set the text into the cell but I can't find a solution how to set the style.
CellData setUserEnteredValue = new CellData()
.setUserEnteredValue(new ExtendedValue()
.setStringValue("cell text"));
Is there any solution?
I had to go through allot of useless answers, but this worked for me. Here you go:
requests.add(new Request()
.setRepeatCell(new RepeatCellRequest()
.setCell(new CellData()
.setUserEnteredValue( new ExtendedValue().setStringValue("cell text"))
.setUserEnteredFormat(new CellFormat()
.setBackgroundColor(new Color()
.setRed(Float.valueOf("1"))
.setGreen(Float.valueOf("0"))
.setBlue(Float.valueOf("0"))
)
.setTextFormat(new TextFormat()
.setFontSize(15)
.setBold(Boolean.TRUE)
)
)
)
.setRange(new GridRange()
.setSheetId(sheetID)
.setStartRowIndex(1)
.setEndRowIndex(0)
.setStartColumnIndex(0)
.setEndColumnIndex(1)
)
.setFields("*")
)
);
AFAIK this is not possible in the Java Spreadsheet API, you instead have to use the Apps Script;
https://developers.google.com/apps-script/reference/spreadsheet/
From their documentation, how to set the background;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D5");
range.setBackground("red");
and how to set the font size;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("B2");
cell.setFontSize(20);
Update
See flo5783's answer below, v4 now offers the ability to do this.
Looks like there is a class designed exactly for this:
CellFormat
In particular these following methods:
public CellFormat setBackgroundColor(ColorbackgroundColor)
and
public CellFormat setTextFormat(TextFormattextFormat)
I haven't coded in Java in ages so I won't try to give you a working code example, but I think you'll be able to figure it out easily from this.
EDIT: Here's a basic example starting from your code:
CellData setUserEnteredValue = new CellData()
.setUserEnteredValue(new ExtendedValue()
.setStringValue("cell text"));
CellFormat myFormat = new CellFormat();
myFormat.setBackgroundColor(new Color().setRed(1)); // red background
myFormat.setTextFormat(new TextFormat().setFontSize(16)); // 16pt font
setUserEnteredValue.setUserEnteredFormat(myFormat);
// For handling single cells this is how you do it, where j and k are row and columns in g sheet .
CellData setUserEnteredValue =new CellData()
.setUserEnteredValue(new ExtendedValue()
.setStringValue(maxs));
List<CellData> values1.add(setUserEnteredValue);
requests.add(new Request()
.setUpdateCells(new UpdateCellsRequest()
.setStart(new GridCoordinate()
.setRowIndex(j)
.setColumnIndex(k))
.setRows(Arrays.asList(
new RowData().setValues(values1)))
.setFields("userEnteredValue,userEnteredFormat.backgroundColor")));
CellFormat myFormat = new CellFormat();
myFormat.setBackgroundColor(new Color().setRed(Float.valueOf("1"))); // red background
setUserEnteredValue.setUserEnteredFormat(myFormat);
You can't change the background color or the font size on a CellData object. You instead need to iterate over some cell ranges. From there, you can set the background color based on the cell value. You can then make your code dependable, like a 2-step process.
From another answer on SO:
//Sets the row color depending on the value in the "Status" column.
function setRowColors() {
var range = SpreadsheetApp.getActiveSheet().getDataRange();
var statusColumnOffset = getStatusColumnOffset();
for (var i = range.getRow(); i < range.getLastRow(); i++) {
rowRange = range.offset(i, 0, 1); //Play with this range to get your desired columns.
status = rowRange.offset(0, statusColumnOffset).getValue(); //The text value we need to evaluate.
if (status == 'Completed') {
rowRange.setBackgroundColor("#99CC99");
} else if (status == 'In Progress') {
rowRange.setBackgroundColor("#FFDD88");
} else if (status == 'Not Started') {
rowRange.setBackgroundColor("#CC6666");
}
}
}

JavaFX table contours of the table

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*/;
}

I am working on a tableview tutorial and I don't understand some errors

I am new to Java and am working on a tutorial for the TableView and I don't understand some of the problems with the code.
I have the error message :
the type of getColumns() is erroneous where S is a type-variable S
extends Object declared in class TableView
Thanks for any suggestions or help
Roger
table = new TableView<>();
data = getInitialTableData();
table.setItems(data);
TableColumn titleCol = new TableColumn("Title");
titleCol.setCellValueFactory(new PropertyValueFactory<Book, String>("title"));
TableColumn authorCol = new TableColumn("Author");
authorCol.setCellValueFactory(new PropertyValueFactory<Book, String>("author"));
table.getColumns().setAll(titleCol, authorCol);
table.getSelectionModel().selectedIndexProperty().addListener(new RowSelectChangeListener());
You need to specify the type of items you are trying to fill the TableView with, see the example in the documentation
TableView<Person> table = new TableView<Person>();
Follow through this tutorial from Oracle.
Your code has to be like:
TableColumn<Book,String> titleCol = new TableColumn<Book,String>("Title"); titleCol.setCellValueFactory(new PropertyValueFactory<Book, String>("title"));
Best Improvement:
TableColumn<Book,String> titleCol = new TableColumn<>("Title"); titleCol.setCellValueFactory(new PropertyValueFactory<>("title"));

tableviewercolumn setwidth percentages instead of pixels

the setWidth method for the TableViewerColumn class takes integer types, I would really like to use percentages, is there anyway to do this, or pack the table or something?
Use TableLayout to layout your table and use ColumnWeightData to specify the 'weight' of each column.
For example, two columns with a 60 / 40 weighting:
TableViewer viewer = ....
TableLayout layout = new TableLayout();
TableViewerColumn col1 = new TableViewerColumn(viewer, SWT.LEAD);
layout.setColumnData(col1.getColumn(), new ColumnWeightData(60));
TableViewerColumn col2 = new TableViewerColumn(viewer, SWT.LEAD);
layout.setColumnData(col2.getColumn(), new ColumnWeightData(40));
viewer.getTable().setLayout(layout);
And yes, you can pack the table/tree too.
for (final TreeColumn item : tree.getColumns()) {
item.pack();
}
for (final TableColumn item : table.getColumns()) {
item.pack();
}
Which is quite similar to the CTRL+NUM-PAD '+' Keycode on Windows.

Dynamically add images to JTable cells

I am dynamically adding data to a cell with the following code:
for(int i = 0; i < matchedSlots.size(); i++)
{
String title = matchedSlots.get(i).getTitle();
String director = matchedSlots.get(i).getDirector();
int rating = matchedSlots.get(i).getRating();
int runTime = matchedSlots.get(i).getRunningTime();
DefaultTableModel tm = (DefaultTableModel) searchResults.getModel();
tm.addRow(new Object[] {title,director,rating,runTime});
}
what do I need to add to the above to be able to add an image in the first cell of each row
By default JTable can render Images. You just need to override getColumnClass() in the TableModel and return Icon.class for 1st column.
Look at Renderers and Editors for more details.
ImageIcon image = new ImageIcon("image.gif");
...
tm.addRow(new Object[] {image,title,director,rating,runTime});
You may need to change your table model to account for the new column if you haven't already.
This short article should help you with the image renderer: http://mdsaputra.wordpress.com/2011/06/13/swing-hack-show-image-in-jtable/

Categories