JavaFX TableView only populating some columns - java

I'm trying to populate a TableView in JavaFX, but only one of the columns is being populated with Data. I've been following the oracle documentation and think that my name conventions are correct.
Data Model:
import javafx.beans.property.SimpleIntegerProperty;
public class PeakClassification {
private final SimpleIntegerProperty peakStart;
private final SimpleIntegerProperty peakEnd;
private final SimpleIntegerProperty peakMaxima;
private final SimpleIntegerProperty peakHeight;
private final SimpleIntegerProperty peakWidth;
public PeakClassification(int peakStart, int peakEnd, int peakMaxima, int peakHeight) {
this.peakStart = new SimpleIntegerProperty(peakStart);
this.peakEnd = new SimpleIntegerProperty(peakEnd);
this.peakMaxima = new SimpleIntegerProperty(peakMaxima);
this.peakHeight = new SimpleIntegerProperty(peakHeight);
this.peakWidth = new SimpleIntegerProperty(peakEnd - peakStart);
}
public int getPeakWidth() {
return peakWidth.get();
}
public int getPeakHeight() {
return peakHeight.get();
}
public int getPeakStart() {
return peakStart.get();
}
public int getPeakEnd() {
return peakEnd.get();
}
public int getPeakMaxima() {
return peakMaxima.get();
}
}
Code for creating the table:
//instantiate the table
TableView tableView = new TableView();
//start values
TableColumn startValue = new TableColumn("Start pos");
startValue.setMinWidth(100);
startValue.setCellValueFactory(new PropertyValueFactory<PeakClassification, Integer>("peakStart"));
TableColumn maximumValue = new TableColumn("Max pos");
startValue.setCellValueFactory(new PropertyValueFactory<PeakClassification, Integer>("peakMaxima"));
tableView.setItems(peakClassifications);
tableView.getColumns().addAll(startValue, maximumValue);
I've printed out the peakClassifications list to console to verify that the maximumValue isn't null.
The getter for the peakMaxima field is getPeakMaxima() so it should be able to find it. I've looked at other stackoverflow entries and that seems to be the issue in most of the cases.
Here's a snippet of the result:
Its probably an obvious mistake. Any ideas?
Thanks.

Error is here, you mistankenly used startValue variable again instead of maximumValue
TableColumn maximumValue = new TableColumn("Max pos");
startValue.setCellValueFactory(new PropertyValueFactory<PeakClassification, Integer>("peakMaxima"));

Related

Java - Variable definition of an object

My issue is the following : I read a .csv file and convert it into an ArrayList. In order to put this ArrayList in a javafx TableView, I need to create an object, go through ObservableList<Object> data = FXCollections.observableArrayList() and populate it with as much as new Object() as I need. The point is that in my .csv, the number of columns may differ in each run of my program, so the object definition does. I don't know how to define an object which definition is a function of the length of my ArrayList and the attributes aren't always the same (and I can't find any clue on google).
To illustrate, imagine the following code :
public class SettingData extends Application {
public void start(Stage stage) {
TableView<FileData> table = new TableView<FileData>();
final ObservableList<FileData> data = FXCollections.observableArrayList(
new FileData("file1", "D:\\myFiles\\file1.txt", "25 MB", "12/01/2017"),
new FileData("file2", "D:\\myFiles\\file2.txt", "30 MB", "01/11/2019"),
new FileData("file3", "D:\\myFiles\\file3.txt", "50 MB", "12/04/2017"),
new FileData("file4", "D:\\myFiles\\file4.txt", "75 MB", "25/09/2018")
);
TableColumn fileNameCol = new TableColumn("File Name");
fileNameCol.setCellValueFactory(new PropertyValueFactory<>("fileName"));
TableColumn pathCol = new TableColumn("Path");
pathCol.setCellValueFactory(new PropertyValueFactory("path"));
TableColumn sizeCol = new TableColumn("Size");
sizeCol.setCellValueFactory(new PropertyValueFactory("size"));
TableColumn dateCol = new TableColumn("Date Modified");
dateCol.setCellValueFactory(new PropertyValueFactory("dateModified"));
ObservableList<String> list = FXCollections.observableArrayList();
table.setItems(data);
table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
table.getColumns().addAll(fileNameCol, pathCol, sizeCol, dateCol);
Assuming that we define FileData like this :
public class FileData {
SimpleStringProperty fileName;
SimpleStringProperty path;
SimpleStringProperty size;
SimpleStringProperty dateModified;
FileData(String fileName, String path, String size, String dateModified) {
this.fileName = new SimpleStringProperty(fileName);
this.path = new SimpleStringProperty(path);
this.size = new SimpleStringProperty(size);
this.dateModified = new SimpleStringProperty(dateModified);
}
public String getFileName(){
return fileName.get();
}
public void setFileName(String fname){
fileName.set(fname);
}
public String getPath(){
return path.get();
}
public void setPath(String fpath){
path.set(fpath);
}
public String getSize(){
return size.get();
}
public void setSize(String fsize){
size.set(fsize);
}
public String getDateModified(){
return dateModified.get();
}
public void setModified(String fmodified){
dateModified.set(fmodified);
}
}
How may implement my code to handle the fact that once I may not have size columns, or another time I'll have a author column. (In fact, the file I'm working with carries around 80 to 110 columns, that's why I need to have a "modular" definition of my object).
It's my first post on any forum ever, I'm not familiar with your usages, sorry for that.
Thanks for your answers.

JavaFx ViewList shares values thru different instances

Hi StackOverflow people,
First question here, I'm stuck on this code and cannot move forward, tried different approaches but cannot figure out why this is happening.
The code is intended to be a few lists each one represents a day of the week, and each of the list has all the possible time. Now, everytime I ran the code each list, even when not update, is using the last date available. For the sake of the example, remove almost all the lists and leave only 2.
The update on the date is being done on this line, t1.setFecha(lunesDate.plusDays(i));, but if for instance, I remove this line on one of the lists, the list is getting the date updated, even if this is happening on another list, with another variable!! It is like the JVM is considering all the lists to be the same... Makes no sense for me...
Can anyone point where is the issue on the code?
Class Turno.class
import java.time.LocalDate;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
public class Turno {
private static final int LIBRE = 1;
private static final int RESERVADO = 2;
private static final int CUMPLIDO = 3;
private static final int CERRADO = 4;
private SimpleIntegerProperty id = new SimpleIntegerProperty();
private SimpleObjectProperty<LocalDate> fecha = new SimpleObjectProperty<LocalDate>();
private SimpleIntegerProperty idhorario = new SimpleIntegerProperty();
private SimpleStringProperty horario = new SimpleStringProperty();
private SimpleIntegerProperty estado = new SimpleIntegerProperty();
private SimpleIntegerProperty idProfesional = new SimpleIntegerProperty();
private SimpleStringProperty profesional = new SimpleStringProperty();;
private SimpleIntegerProperty idPaciente = new SimpleIntegerProperty();
private SimpleStringProperty paciente = new SimpleStringProperty();;
private SimpleStringProperty observaciones = new SimpleStringProperty();;
public Turno(int id, LocalDate d, int idh, String h, int e, int idpro, String pro, int idpac, String pac,
String o) {
this.setId(id);
this.setFecha(d);
this.setIdHorario(idh);
this.setHorario(h);
this.setEstado(e);
this.setIdProfesional(idpro);
this.setProfesional(pro);
this.setIdPaciente(idpac);
this.setPaciente(pac);
this.setObservaciones(o);
}
public Turno() {
}
// ID
public final SimpleIntegerProperty idProperty() {
return this.id;
}
public final int getId() {
return this.idProperty().get();
}
public final void setId(final int i) {
this.idProperty().set(i);
}
/* Bunch of getter and setters for properties, just like the one above */
Class TestTurno.class
public class TestTurnos extends Application {
private static Turno turnoSeleccionado = null;
ScrollPane scrollPane = new ScrollPane();
HBox listas = new HBox();
VBox vBoxL = new VBox();
VBox vBoxM = new VBox();
ListView<Turno> listViewTurnosL = new ListView<>();
ListView<Turno> listViewTurnosM = new ListView<>();
List<Turno> listaHorarios = new ArrayList<>();
List<Turno> listaTurnos = new ArrayList<>();
public static void Main(String[] args) {
launch(args);
}
#Override
public void start(Stage escenario) throws Exception {
// Here we get the current Monday date, in order to prepare for the current week
LocalDate lunesDate = null;
LocalDate diaSemana = null;
LocalDate diaHoy = LocalDate.now();
int d = diaHoy.getDayOfWeek().getValue();
lunesDate = diaHoy.minusDays(d - 1);
// Give the schedules to each day list
listViewTurnosL.setItems(FXCollections.observableList(listaHorarios));
listViewTurnosM.setItems(FXCollections.observableList(listaHorarios));
// Then we look for more data on the DB,
for (int i = 0; i < 2; i++) {
// Database magic happens here, we filled the listaTurnos, not relevant
// We make the lists
//
//!!! Here is where the glitch appears, debugging shows that it only gets into the switch on the right conditions,
// but it keeps on updating the date on any of the lists, even when it is updating another list
switch (i) {
case 0: {
for (Turno t1 : listViewTurnosL.getItems()) {
t1.setFecha(lunesDate.plusDays(i));
}
// Value of t1.getFecha() is 1
break;
}
case 1: {
for (Turno t2 : listViewTurnosM.getItems()) {
t2.setFecha(lunesDate.plusDays(i));
}
// Value of t1.getFecha() is 2 !!!!!
// Value of t2.getFecha() is 2
break;
}
}
}
vBoxL.getChildren().addAll(listViewTurnosL);
vBoxM.getChildren().addAll(listViewTurnosM);
listas.getChildren().addAll(vBoxL, vBoxM);
scrollPane.setContent(listas);
Scene escena = new Scene(scrollPane, 800, 800);
escenario.setScene(escena);
escenario.show();
}
}
Consider how you are creating your lists:
listViewTurnosL.setItems(FXCollections.observableList(listaHorarios));
listViewTurnosM.setItems(FXCollections.observableList(listaHorarios));
The documentation for the factory method you are using reads:
Constructs an ObservableList that is backed by the specified list.
That is - the base list provided is kept as the backing (storing) list. Since both ObservableList instances share the same original ArrayList, it is no wonder they share the content.
You may want to use the factory method FXCollections.ObservableArrayList which creates a new ObservableList (the backing list is created internally, or is the list itself).
If you really need the non-observable list instances, you should probably use two different ones if the lists are not to be equal.
If I look at your code I see the following things:
listViewTurnosL.setItems(FXCollections.observableList(listaHorarios));
listViewTurnosM.setItems(FXCollections.observableList(listaHorarios));
This means to that both listViewTurnosX contain exactly the same element references.
case 0: {
for (Turno t1 : listViewTurnosL.getItems()) {
t1.setFecha(lunesDate.plusDays(i));
}
// Value of t1.getFecha() is 1
break;
}
case 1: {
for (Turno t2 : listViewTurnosM.getItems()) {
t2.setFecha(lunesDate.plusDays(i));
}
It doesn't matter which list is iterated, both contain the same elements, so in both cases the same properties get updated.

How to implement editable grid with 2 related dropdown in wicket?

I need to show the data from DB in table format with edit, save , cancel, delete button, its called editable grid in wicket.
In Table row after click on edit button data should be shown on 2 dropdown choices and select data from 1st dropdown then 2nd dropdown data model should be changed based on selection of first dropdown.
I have gone through the https://github.com/wicketstuff/core/tree/master/editable-grid-parent. But its showing only editable grid with 1 dropdown in wicket and i want 2 dropdown. Please help on this.
UPDATE :
I have used this
private List<AbstractEditablePropertyColumn<Person, String>> getColumns()
{
List<AbstractEditablePropertyColumn<Person, String>> columns = new ArrayList<AbstractEditablePropertyColumn<Person, String>>();
stateDropDownPropertyColumn = new AbstractEditablePropertyColumn<Person, String>(new PropertyModel<String>(this, "selectedMake"), "state")
{
private static final long serialVersionUID = 1L;
public EditableCellPanel<Person> getEditableCellPanel(String componentId)
{
return getStateDDCellpanel(componentId,this);
}
};
cityDropDownPropertyColumn = new AbstractEditablePropertyColumn<Person, String>(new Model<String>("CarModel"), "city"){
private static final long serialVersionUID = 1L;
#Override
public EditableCellPanel<Person> getEditableCellPanel(String componentId) {
// TODO Auto-generated method stub
return getCityDDCellpanel(componentId,this);
}};
columns.add(stateDropDownPropertyColumn);
columns.add(cityDropDownPropertyColumn);
return columns;
}
private EditableRequiredDropDownCellPanel<Person, String> getStateDDCellpanel(String componentId,
AbstractEditablePropertyColumn<Person, String> DropDownPropertyColumn){
this.stateComponentID = componentId;
this.stateDropDownPropertyColumn = DropDownPropertyColumn;
stateDropDownCellPanel = new EditableRequiredDropDownCellPanel<Person, String>(stateComponentID, stateDropDownPropertyColumn, stateChoices);
return stateDropDownCellPanel;
}
private EditableRequiredDropDownCellPanel<Person, String> getCityDDCellpanel(String componentId,
AbstractEditablePropertyColumn<Person, String> DropDownPropertyColumn){
this.cityComponentID = componentId;
this.cityDropDownPropertyColumn = DropDownPropertyColumn;
cityDropDownCellPanel = new EditableRequiredDropDownCellPanel<Person, String>(cityComponentID, cityDropDownPropertyColumn, cityChoices);
cityDropDownCellPanel.setOutputMarkupId(true);
cityDropDownCellPanel.setOutputMarkupPlaceholderTag(true);
return cityDropDownCellPanel;
}
I have not any idea about this where i need to put up behaviours of dropdown cell.
Probably almost direct answer to question according to dynamic list (first is fixed, second id depended, dynamically computed):
http://examples7x.wicket.apache.org/ajax/choice
To be with SO rules not to include pure links, excerpt from official example Apache licensed
(prepared to be shorter - not compile)
Model allows You prepare changed data, and AJAX event forces refresh - this is core of idea. Event (ajax behaviour) connected to first 'makers', and behaviour forces to refresh 'models' . This is typical wicket+ajax pattern.
/**
* Linked select boxes example
*
* #author Igor Vaynberg (ivaynberg)
*/
public class ChoicePage extends BasePage
{
private String selectedMake;
private final Map<String, List<String>> modelsMap = new HashMap<>(); // map:company->model
/**
* #return Currently selected make
*/
public String getSelectedMake()
{
return selectedMake;
}
/**
* #param selectedMake
* The make that is currently selected
*/
public void setSelectedMake(String selectedMake)
{
this.selectedMake = selectedMake;
}
/**
* Constructor.
*/
public ChoicePage()
{
modelsMap.put("AUDI", Arrays.asList("A4", "A6", "TT"));
modelsMap.put("CADILLAC", Arrays.asList("CTS", "DTS", "ESCALADE", "SRX", "DEVILLE"));
modelsMap.put("FORD", Arrays.asList("CROWN", "ESCAPE", "EXPEDITION", "EXPLORER", "F-150"));
IModel<List<String>> makeChoices = new AbstractReadOnlyModel<List<String>>()
{
#Override
public List<String> getObject()
{
return new ArrayList<>(modelsMap.keySet());
}
};
IModel<List<String>> modelChoices = new AbstractReadOnlyModel<List<String>>()
{
#Override
public List<String> getObject()
{
List<String> models = modelsMap.get(selectedMake);
if (models == null)
{
models = Collections.emptyList();
}
return models;
}
};
Form<?> form = new Form("form");
add(form);
final DropDownChoice<String> makes = new DropDownChoice<>("makes",
new PropertyModel<String>(this, "selectedMake"), makeChoices);
final DropDownChoice<String> models = new DropDownChoice<>("models",
new Model<String>(), modelChoices);
models.setOutputMarkupId(true);
form.add(makes);
form.add(models);
...
makes.add(new AjaxFormComponentUpdatingBehavior("change")
{
#Override
protected void onUpdate(AjaxRequestTarget target)
{
target.add(models);
}
});
}
}
UPDATE after comment. Lets back to previous github code, must replace fixed list by model. How??? Derive or copy, add contructor with IModel ... Wicket has very good object design.
In similar way we add column types not known to (not planned) grid author.
BTW Your comment 'its not working' is very broad. Sorry, I can help if I can, but dont make full project for You. Hope You will enjoy with coding.
package org.wicketstuff.egrid.column;
import java.util.List;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.FormComponent;
/**
*
* #author Nadeem Mohammad
*
*/
public class EditableRequiredDropDownCellPanel<T, S> extends EditableCellPanel
{
private static final long serialVersionUID = 1L;
public EditableRequiredDropDownCellPanel(final String id, final PropertyColumn<T, S> column, #SuppressWarnings("rawtypes") final List choices)
{
super(id);
#SuppressWarnings("unchecked")
DropDownChoice<T> field = new DropDownChoice<T>("dropdown", choices); <--- **** Here should be model ****
field.setLabel(column.getDisplayModel());
add(field);
}
#Override
public FormComponent<?> getEditableComponent()
{
return (FormComponent<?>) get("dropdown");
}
}
then with changed class return here:
columns.add(new AbstractEditablePropertyColumn<Person, String>(new Model<String>("Age"), "age")
{
private static final long serialVersionUID = 1L;
public EditableCellPanel getEditableCellPanel(String componentId)
{
return new ***** EditableRequiredDropDownCellPanel ***** <Person, String>(componentId, this, Arrays.asList("10","11","12","13","14","15"));
}
});
add Beahviours too

JavaFX TableView click sorters not working?

Why is my TableColumn.setSortable() showing the sort graphic on the table header when I double-click on it, but it is not actually doing any sort at all? I would imagine it naturally knows how to sort Numbers? Do I have to set an explicit comparator even for types that have a natural sort behavior?
public class PenaltyDashboardManager {
private final TableView<Penalty> penaltyTable = new TableView<Penalty>();
/* ... */
private void initializeTable() {
penaltyTable.setItems(Penalty.getPenaltyManager().getPenalties());
penaltyTable.setEditable(true);
TableColumn<Penalty,Number> penaltyId = new TableColumn<>("ID");
penaltyId.setCellValueFactory(c -> c.getValue().getPenaltyIdProperty());
penaltyId.setEditable(true);
penaltyId.setSortable(true);
/* ... */
penaltyTable.getColumns.add(penaltyId);
}
}
UPDATE
Very odd. I tried to create a simple example to demonstate the sorting not working. But this simple column of integers is sorting just fine :/
public final class TableSortTest extends Application {
private static final ObservableList<NumericCombo> values = FXCollections.observableList(
IntStream.range(1, 100).mapToObj(i -> new NumericCombo()).collect(Collectors.toList()));
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Collections.shuffle(values);
TableView<NumericCombo> tableView = new TableView<>();
tableView.setItems(values);
TableColumn<NumericCombo,Number> combo1 = new TableColumn<>("COMBO 1");
combo1.setCellValueFactory(c -> new ReadOnlyObjectWrapper<>(c.getValue().combo1));
TableColumn<NumericCombo,Number> combo2 = new TableColumn<>("COMBO 2");
combo2.setCellValueFactory(c -> c.getValue().combo2);
TableColumn<NumericCombo,Number> combo3 = new TableColumn<>("COMBO 3");
combo3.setCellValueFactory(c -> c.getValue().combo3);
tableView.getColumns().addAll(combo1,combo2,combo3);
Group root = new Group(tableView);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
private static final class NumericCombo {
private static final Random rand = new Random();
private final int combo1;
private final IntegerProperty combo2;
private final IntegerProperty combo3;
private NumericCombo() {
combo1 = rand.nextInt((10000 - 0) + 1);
combo2 = new SimpleIntegerProperty(rand.nextInt((10000 - 0) + 1));
combo3 = new SimpleIntegerProperty(rand.nextInt((10000 - 0) + 1));
}
}
}
I found the issue! I was using my own implementation of ObservableList, called ObservableImmutableList. It wraps the ObservableList interface around a Guava ImmutableList. Since the ImmutableList is not modifiable, it cannot be sorted... even in a TableView.
This transitions to another issue I'm struggling to figure out. How do I sort my ObservableImmutableList? So I posted another question.

JavaFX 8 - How to bind TextField text property to TableView integer property

Let's say I have a situation like this: I have a TableView (tableAuthors) with two TableColumns (Id and Name).
This is the AuthorProps POJO which is used by TableView:
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
public class AuthorProps {
private final SimpleIntegerProperty authorsId;
private final SimpleStringProperty authorsName;
public AuthorProps(int authorsId, String authorsName) {
this.authorsId = new SimpleIntegerProperty(authorsId);
this.authorsName = new SimpleStringProperty( authorsName);
}
public int getAuthorsId() {
return authorsId.get();
}
public SimpleIntegerProperty authorsIdProperty() {
return authorsId;
}
public void setAuthorsId(int authorsId) {
this.authorsId.set(authorsId);
}
public String getAuthorsName() {
return authorsName.get();
}
public SimpleStringProperty authorsNameProperty() {
return authorsName;
}
public void setAuthorsName(String authorsName) {
this.authorsName.set(authorsName);
}
}
And let's say I have two TextFields (txtId and txtName). Now, I would like to bind values from table cells to TextFields.
tableAuthors.getSelectionModel()
.selectedItemProperty()
.addListener((observableValue, authorProps, authorProps2) -> {
//This works:
txtName.textProperty().bindBidirectional(authorProps2.authorsNameProperty());
//This doesn't work:
txtId.textProperty().bindBidirectional(authorProps2.authorsIdProperty());
});
I can bind Name TableColumn to txtName TextField because authorsNameProperty is a SimpleStringProperty, but I can't bind Id TableColumn to txtId TextField because authorsIdProperty is a SimpleIntegerProperty. My question is: How can I bind txtId to Id TableColumn?
P.S. I can provide working example if it's necessary.
Try:
txtId.textProperty().bindBidirectional(authorProps2.authorsIdProperty(), new NumberStringConverter());

Categories