JavaFX8: Strings are not displayed in TableView - tableview

I have the following problem: I try to populate a tableview in JavaFX8 with an array. So, I try to add the array as a row to the tableview. I run this code in the Controller of my FXML file, when enter is pressed. This is the code:
String[] words = {"ace", "boom", "crew", "dog", "eon"};
List<String> tableViewRow = Arrays.asList(words);
ObservableList<String> row = FXCollections.observableList(tableViewRow); //observableArrayList also doesn't work
transactionOverview.getItems().add(row);
transactionOverview is my tableview, and the String[] is just a placeholder for my actual String[]. I tried to create my tableview in multiple ways:
public TableView<ObservableList<String>> transactionOverview;
public TableView<ObservableList> transactionOverview;
public TableView transactionOverview;
None of them works.
The problem is that tableview gets an extra row, which I can select, but there are now string values visible in the tableview. I don't know if they are added.
My code is based on Javafx 2.2 - Dynamic table view - table data (answer from Jitendra Pareek), and I have chosen for this solution because I don't want to use an extra class to populate my tableview.
Any help is appreciated!

Since (according to your comments) you have a fixed number of columns, I would strongly recommend creating a model class to hold the items in each row of the table. You can then follow the standard patterns and it should work readily.
However you manage a TableView, you must provide a cell value factory for each column. This is essentially a function that specifies how to get the value for a cell from the item in the row. If your use a model class that uses JavaFX properties, then you can use a PropertyValueFactory (though Java 8 lambda expressions make that pretty much redundant). Otherwise, you need to implement a callback.
If you really want to use a list structure to hold the data for each row, and assuming your table and table columns are all defined in the FXML file, you would do something like this in your controller class:
#FXML
private TableView<ObservableList<String>> transactionOverview ;
// ...
public void initialize() {
for (int i=0; i < transactionOverview.getColumns().size(); i++) {
TableColumn<ObservableList<String>, String> col = transactionOverview.getColumns().get(i);
final int colIndex = i ;
col.setCellValueFactory( (CellDataFeatures cellData) -> {
ObservableList<String> rowData = cellData.getValue();
return new ReadOnlyStringWrapper(rowData.get(colIndex));
});
}
// ...
}

Related

How to set and/or retrieve default cell padding in iText 7

When you create a table in iText 7 using the Table and Cell classes, the table cells come with some padding built in by default. As far as I can tell by looking at a generated document, it appears to be about 2 PDF units.
Is there any way I can retrieve this value for use in calculations? Also, is there any way I can change this default, so that I can set my own padding to be used in all cells in all tables, instead of having to set it individually on every cell?
Please take a look at the iText 7: Building Blocks tutorial.
In the Before we start section, we see that every building block is derived from a class named ElementPropertyContainer. This class is a container of properties.
In the case of the Cell class, there is a set of properties that define the padding. You can get these properties the generic way (using a method of the AbstractElement class) like this:
System.out.println(cell.getProperty(Property.PADDING_LEFT));
System.out.println(cell.getProperty(Property.PADDING_RIGHT));
System.out.println(cell.getProperty(Property.PADDING_TOP));
System.out.println(cell.getProperty(Property.PADDING_BOTTOM));
But why make it difficult if you can also simply use convenience methods that is available in the BlockElement class:
System.out.println(cell.getPaddingLeft());
System.out.println(cell.getPaddingRight());
System.out.println(cell.getPaddingTop());
System.out.println(cell.getPaddingBottom());
As you can see in the tutorial, the Cell class is a subclass of the BlockElement class. The BlockElement is a subclass of the AbstractElement class. The AbstractElement class is a subclass of the ElementPropertyContainer class.
If you want to change the padding (or the margin if you are so inclined), please read chapter 5 of that tutorial. It has an example, named CellMarginPadding:
public void createPdf(String dest) throws IOException {
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
Document document = new Document(pdf);
Table table = new Table(new float[]{2, 1, 1});
table.setBackgroundColor(Color.ORANGE);
table.setWidthPercent(80);
table.setHorizontalAlignment(HorizontalAlignment.CENTER);
table.addCell(
new Cell(1, 3).add("Cell with colspan 3")
.setPadding(10).setMargin(5).setBackgroundColor(Color.GREEN));
table.addCell(new Cell(2, 1).add("Cell with rowspan 2")
.setMarginTop(5).setMarginBottom(5).setPaddingLeft(30)
.setFontColor(Color.WHITE).setBackgroundColor(Color.BLUE));
table.addCell(new Cell().add("row 1; cell 1")
.setFontColor(Color.WHITE).setBackgroundColor(Color.RED));
table.addCell(new Cell().add("row 1; cell 2"));
table.addCell(new Cell().add("row 2; cell 1").setMargin(10)
.setFontColor(Color.WHITE).setBackgroundColor(Color.RED));
table.addCell(new Cell().add("row 2; cell 2").setPadding(10)
.setFontColor(Color.WHITE).setBackgroundColor(Color.RED));
document.add(table);
document.close();
}
This is what it looks like:
I'm sorry if it hurts the eyes a bit, but using those colors seemed like the best way to explain the difference between the margin and the padding to me.
Most of the properties are inherited. For instance: if you set the font for a Div, that font will be the default font for all the elements added to that Div. There are some exceptions though. The padding is one of them. This is how the default values for the properties specific to the Cell class were defined:
#Override
public <T1> T1 getDefaultProperty(int property) {
switch (property) {
case Property.BORDER:
return (T1) (Object) DEFAULT_BORDER;
case Property.PADDING_BOTTOM:
case Property.PADDING_LEFT:
case Property.PADDING_RIGHT:
case Property.PADDING_TOP:
return (T1) (Object) 2f;
default:
return super.<T1>getDefaultProperty(property);
}
}
As you can see, there is no padding value for the complete cell; the padding consists of four values that incidentally are identical by default.
If you don't like to define a padding different from the default for each Cell, just create a subclass of Cell and call it MyCustomCell. Make it custom in the sense that it uses the padding of your choice by overriding the getDefaultProperty() class.
In the tutorial, you'll find an example of a subclass that draws cells with borders that have rounded corners so that we don't have to set declare a renderer every time we want to introduce rounder corners.
I am the original author of that documentation. I hope you find it useful to answer these and other questions about the Cell and other objects in iText 7.
I did this in C# per #Bruno Lowagie override route to set a default as no padding and no border:
public class BorderlessCell : Cell
{
public BorderlessCell(int rowSpan, int colSpan) : base(rowSpan, colSpan) { }
public BorderlessCell() : base() { }
public override T1 GetDefaultProperty<T1>(int property)
{
switch (property)
{
case Property.BORDER:
return (T1)(Object)(Border.NO_BORDER);
case Property.PADDING_BOTTOM:
case Property.PADDING_LEFT:
case Property.PADDING_RIGHT:
case Property.PADDING_TOP:
return (T1)(Object)(0);
default:
return base.GetDefaultProperty<T1>(property);
}
}
}
What works for me was editing the WidthPercentage, for example:
table.setWidthPercentage(100)

How to reorder columns in datatable with drag and drop Primefaces

I use primefaces version 5.2 and jsf version 2.2.6.
I have a datatable and I want to reorder the columns with drag and drop. Besides I have also the column toggle functionality, with which I can hide columns. The reordering and hiding of columns are saved into the database.
For me it is no problem if on specific positions are hidden columns, I can drop the dragged column after or before the position of the hidden ones.
I use the following ajax event:
<p:ajax event="colReorder" listener="#{transactionsPage.onColumnReorder}"/>
and the method signature looks like this:
public void onColumnReorder(AjaxBehaviorEvent event)
If I could have used ReorderEvent to get the fromIndex and toIndex, it would have been very easy to deal with this situation, but unfortunately this event can be used only for dragging and dropping rows and not columns.
Is there a way to find out these indexes? Even only the fromIndex could be enough.
I managed to find a solution, maybe it can be helpful for someone in the future.
Because I didn't find indexes for fromIndex and toIndex for reordering the columns(there are only such indexes for reordering rows, which work with the ReorderEvent), I get the datatable from the AjaxBehaviorEvent and from that I get the columns and overwrite these indexes of the reordered columns in the database columns and then I can restore the view later with the reordered columns.
The code for triggering the event from the xhtml page looks like this:
<p:ajax event="colReorder" listener="#{transactionsPage.onColumnReorder}"/>
The code for the managed bean looks like this:
public void onColumnReorder(AjaxBehaviorEvent event) {
DataTable table = (DataTable) event.getSource();
List<ViewAttributes> allViewAttributesList = loadAllViewAttributes();
for (int i = 0; i < table.getColumns().size(); i++) {
UIComponent colComponentDestination = (UIComponent) table.getColumns().get(i);
//allViewAttributes is the list of columns from the database, in which I set the new indexes
for (int j = 0; j < allViewAttributesList.size(); j++) {
ViewAttributes viewAttrSource = allViewAttributesList.get(j);
if (viewAttrSource.getColumnName().equals(colComponentDestination.getId()) && i != viewAttrSource.getPosition()) {
viewAttrSource.setPosition(new Long(i));
//save column with new index in the database
getServiceManager().getViewService().getViewDao().update(viewAttrSource);
}
}
}
}

Bind 2 tableviews and a lineChart together in JavaFX2

My requirement is to use 2 tables and 1 chart to visualize my data set. Each data element contains its (unique) name and a bunch of data belonging to it. The first table will show the name of every dataset I have and the second table will show the data belonging to the dataset (row) being selected in the first table. The second table and the chart will show the same data and both belong to the dataset (row) being selected in the first table. I have achieved half of this behavior (linking both table) now by using the code below.
The problem I currently have now is: I can't figure out the way to let the chart display the same data as the second table. My idea is to set the chart data in the ChangeListener, but the problem is the data model of the table is likely to not suitable with the chart. I have the readingData field as ObservableList in the TableDataModel class which is the type that the chart accept but it is an ObservableList of ReadingData not XYChart.Data. Is there any way I can use the XYChart.Data in the ReadData class?
My main class:
ObservableList<TableDataModel> tableData = FXCollections.observableArrayList();
// Other code omitted
/* Create the first table */
TableView<TableDataModel> myTable = new TableView<TableDataModel>();
TableColumn nameColumn = new TableColumn("Name");
nameColumn.setCellValueFactory(new PropertyValueFactory<TableDataModel, String>("name"));
// Other column omitted
myTable.setItems(tableData);
myTable.getColumns().addAll(nameColumn, ...);
// When user select on any row update the second table items
myTable.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TableDataModel>()
{
#Override
public void changed(ObservableValue<? extends TableDataModel> arg0, TableDataModel arg1, TableDataModel arg2)
{
dataTable.setItems(arg2.readingData);
}
});
/* The second table */
TableView<ReadData> dataTable = new TableView<ReadData>();
TableColumn valueColumn = new TableColumn("Value");
valueColumn.setCellValueFactory(new PropertyValueFactory<ReadData, Integer>("value"));
// Other column omitted
dataTable.setItems(null);
dataTable.getColumns().addAll(valueColumn, ...);
TableDataModel.java:
private final SimpleStringProperty name;
// Other SimpleStringProperty and its get and set method omitted
public final ObservableList<ReadData> readingData = FXCollections.observableArrayList();
ReadData.java:
// I use XYChart.Data here because I think that this might be useful when I want to show this on the chart
private SimpleObjectProperty<XYChart.Data<Integer, Integer>> value;
// Other property
// Provide this to make below line work
// valueColumn.setCellValueFactory(new PropertyValueFactory<ReadData, Integer>("value"));
public int getValue()
{
return value.get().getYValue();
}
AFAIK you need to put your XYChart.Datas into the XYChart.Series first, then put that series into the chart by chart.getData().add(series). I think you can do this in your myTable selected change listener: Create there a new series (or modify existing, previously created and added one), add all your value.get() values from the ReadData. See the example of LineChart.

Using JQGrid with custom paging in Asp.Net MVC

I am using JQGrid with the Trirand.Web.Mvc class, and trying to figure out how to do custom paging.
I have seen the paging demos here
The problem with these demos is that they bind directly to a linq context object and lets MVC take care of the paging.
// This method is called when the grid requests data. You can choose any method to call
// by setting the JQGrid.DataUrl property
public JsonResult PerformanceLinq_DataRequested()
{
// Get both the grid Model and the data Model
// The data model in our case is an autogenerated linq2sql database based on Northwind.
var gridModel = new OrdersJqGridModel();
var northWindModel = new NorthwindDataContext();
// return the result of the DataBind method, passing the datasource as a parameter
// jqGrid for ASP.NET MVC automatically takes care of paging, sorting, filtering/searching, etc
return gridModel.OrdersGrid.DataBind(northWindModel.OrdersLarges);
}
The data set I want to bind to is quite complex and I am returning it from a stored procedure, which does the paging for me.
So all I have to give JQGrid is the correct size of rows for a specific page of the entire resultset. I can also return the total row count.
So I have my results in a List myListOfObjects.
I can pass this into the DataBind using myListOfObjects.AsQueryable()
The problem is, JQGrid thinks there is only {page size} rows, so does not display any of the paging options.
Is it possible to pass in the total row count?
Other grids, like Teleriks MVC grid allows you to pass in the Total row count, and it displays the paging correctly
Ok, so I've managed to solve this myself. There may be other ways to do it, if so I'd love to hear them!
The JQGrid.DataBind produces an JsonResult object, whose Data value is set to Trirands own object Trirand.Web.Mvc.JsonResponse
It's an internal class to their Trirand.Web.Mvc, so i had to copy its structure which I could see using Visual Studio debugging.
It has:
page - the current page number
records - the total record count
rows - of type Trirand.Web.Mvc.JsonRow (which I need to replicate too)
total - the total number of pages needed
JsonRow looks like:
cell - a string array of your columns
id - your row ID
So my code looked like this:
var jsonList = new List<JSONRow>();
myData.ForEach(x => jsonList.Add(new JSONRow(x)));
var jsonResult = Json (new
{
page = page,
rows = jsonList.ToArray(),
records = totalRows,
total = Math.Round((double)totalRows / rows, MidpointRounding.AwayFromZero)
}, JsonRequestBehavior.AllowGet);
return jsonResult;
My JsonRow looks like this:
public class JSONRow
{
public string[] cell { get; set; }
public string id { get; set; }
public JSONRow(MyObjectType myObject)
{
id = myObject.id;
cell = new string[3];
cell[0] = myObject.Col1;
cell[1] = myObject.Col2?? "";
cell[2] = myObject.Col3?? "";
}
}

Using DataObjectTypeName in DataObjectSource

The functionality I am trying to use is:
- Create a ObjectDataSource for selection and updating controls on a web page (User Control).
- Use the DataObjectTypeName to have an object created that would send the data to an UpdateMethod.
- Before the values are populated in the DataObjectTypeName’s object, I would like to pre-populate the object so the unused items in the class are not defaulted to zeros and empty strings without me knowing whether the zero or default string was set by the user or by the application.
I cannot find a way to pre-populate the values (this was an issue back in 2006 with framework 2.0). One might ask “Why would anyone need to pre-populate the object?”. The simple answer is: I want to be able to randomly place controls on different User Controls and not have to be concerned with which UpdateMethod needs to handle which fields of an object.
For Example, let’s say I have a class (that reflects a SQL Table) that includes the fields: FirstName, LastName, Address, City, State, Zip. I may want to give the user the option to change the FirstName and LastName and not even see the Address, City, State, Zip (or vice-versa). I do not want to create two UpdateMethods where one handled FirstName and LastName and the other method handles the other fields. I am working with a Class of some 40+ columns from multiple tables and I may want some fields on one screen and not another and decide later to change those fields from one screen to another (which breaks my UpdateMethods without me knowing).
I hope I explained my issue well enough.
Thanks
This is hardly a solution to the problem, but it's my best stab at it.
I have a GridView with its DataSourceID set to an ObjectDataSource.
Whenever a row is updated, I want the property values in the object to be selectively updated - that is - only updated if they appear as columns in the GridView.
I've created the following extension:
public static class GridViewExtensions
{
public static void EnableLimitUpdateToGridViewColumns(this GridView gridView)
{
_gridView = gridView;
if (_gridView.DataSourceObject != null)
{
((ObjectDataSource)_gridView.DataSourceObject)
.Updating += new ObjectDataSourceMethodEventHandler(objectDataSource_Updating);
}
}
private static GridView _gridView;
private static void objectDataSource_Updating(object sender, ObjectDataSourceMethodEventArgs e)
{
var newObject = ((object)e.InputParameters[0]);
var oldObjects = ((ObjectDataSource)_gridView.DataSourceObject).Select().Cast<object>();
Type type = oldObjects.First().GetType();
object oldObject = null;
foreach (var obj in oldObjects)
{
if (type.GetProperty(_gridView.DataKeyNames.First()).GetValue(obj, null).ToString() ==
type.GetProperty(_gridView.DataKeyNames.First()).GetValue(newObject, null).ToString())
{
oldObject = obj;
break;
}
}
if (oldObject == null) return;
var dynamicColumns = _gridView.Columns.OfType<DynamicField>();
foreach (var property in type.GetProperties())
{
if (dynamicColumns.Where(c => c.DataField == property.Name).Count() == 0)
{
property.SetValue(newObject, property.GetValue(oldObject, null), null);
}
}
}
}
And in the Page_Init event of my page, I apply it to the GridView, like so:
protected void Page_Init()
{
GridView1.EnableLimitUpdateToGridViewColumns();
}
This is working well for me at the moment.
You could probably apply similar logic to other controls, e.g. ListView or DetailsView.
I'm currently scratching my head to think of a way this can be done in a rendering-agnostic manner - i.e. without having to know about the rendering control being used.
I hope this ends up as a normal feature of the GridView or ObjectDataSource control rather than having to hack it.

Resources