The following creates a simple window displaying the following fields:
proxyType (Combobox:enum)
proxyHost (Textfield:String)
proxyPort (Textfield:int
proxyExclusions (Textfield:String)
The following Java code displays the content:
public class ProxyDemo extends Application {
private FXForm<Proxy> fxForm;
private StackPane mainPane = new StackPane();
static enum ProxyType {
DIRECT, HTTP, HTTPS, FTP, SOCKS;
}
static class Proxy {
private final ObjectProperty<ProxyType> proxyType = new SimpleObjectProperty<ProxyType>();
private final StringProperty proxyHost = new SimpleStringProperty();
private final IntegerProperty proxyPort = new SimpleIntegerProperty();
private final StringProperty proxyExclusions = new SimpleStringProperty();
public Proxy(ProxyType proxyType, String proxyHost, int proxyPort, String proxyExclusions) {
this.proxyType.set(proxyType);
this.proxyHost.set(proxyHost);
this.proxyPort.set(proxyPort);
this.proxyExclusions.set(proxyExclusions);
}
public ProxyType getProxyType() {
return proxyType.get();
}
public String getProxyHost() {
return proxyHost.get();
}
public int getProxyPort() {
return proxyPort.get();
}
public String getProxyExclusions() {
return proxyExclusions.get();
}
}
#SuppressWarnings("unchecked")
#Override
public void start(Stage primaryStage) throws Exception {
Proxy proxy = new Proxy(ProxyType.DIRECT, "", 0, "");
fxForm = new FXFormBuilder<>().source(proxy).build();
mainPane.getChildren().addAll(fxForm);
Scene root = new Scene(mainPane);
primaryStage.setTitle("Demo");
primaryStage.setScene(root);
primaryStage.show();
}
public static void main(String... args) {
ProxyDemo.launch(args);
}
}
The following is my attempt to lookup the Proxy Type.
fxForm.getScene().lookup("#proxyType");
It is my intention to Disable the fields when the Proxy Type is DIRECT, otherwise enable them.
Is the "proxyType" a Combobox type?
What is the ID of this field that FXForm2 assigns?
How does FXForm2 assign the IDs?
My questions are solved.
1. Is the proxyType a Combobox type?
No, the proxyType is a ChoiceBox.
2. What is the ID of this field that FXForm2 assigns?
#proxyType-form-editor
3. How does FXForm2 assign the IDs?
The interactive control is always suffixed with "-form-editor".
e.g. proxyHost will have an ID of #proxyHost-form-editor.
The associated label will have the ID #proxyHost-label.
References
FXForm2 GitHub - Wiki
https://github.com/dooApp/FXForm2/wiki/Style-your-form-with-css
Related
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.
I want the title of my notes-program to change whenever my list (notes) changes. To achieve this I wanted to bind an IntegerProperty to the size of my list, but it says:
The method bind(ObservableValue<? extends Number>) in the type Property is not applicable for the arguments (int)
Does this mean I should cast the size from int to a Number (tried it but there was another problem) or is there an even easier solution?
public class Notes extends Stage {
ObservableList<String> notes = FXCollections.observableArrayList();
public Notes() {
this.setup();
}
private void setup() {
IntegerProperty size = new SimpleIntegerProperty();
size.bind(this.notes.size());
this.setTitle(String.format("Notes (%d)", size.getValue()));
final Scene scene = new Scene(this.createRootPane());
this.setScene(scene);
}
}
Just bind your title property to the list size’s asString binding:
titleProperty().bind(Bindings.size(notes).asString("Notes (%d)"));
I have this method in my application which is constructing RadioButtons using an helper class: Mode. I would like to call out of the createModesRadios method .getText() for the user's selected RadioButton. Also I would like to save the user's choices in order to remember them in a following use. Is there an easy way to call them and set the choices into the CheckBoxes, into the RadioButtons and into the PrefixSelectionComboBoxs?
I am using a Model Class Configuration.java in order to store the information, because I will need to process some calculations with the user inputs. I would like to store in it also the choices made from the controllers mentioned above.
Part of my Class
public class ConfigurationEditDialogController {
// General Tab
#FXML
private TextField configurationNameField;
#FXML
private TextField commentField;
#FXML
private TextField creationTimeField;
#FXML
private TextField startAddress;
#FXML
private ToggleSwitch triggerEnable;
#FXML
private ToggleSwitch softwareTrigger;
#FXML
private ToggleSwitch ctsEnable;
// Data Acquisition Tab
#FXML
private Pane dataAcquisitionTab;
#FXML
private Button okButton;
private Mode[] modes = new Mode[]{
new Mode("Vertical", 4),
new Mode("Hybrid", 2),
new Mode("Horizontal", 1)
};
private int count = Stream.of(modes).mapToInt(Mode::getCount).max().orElse(0);
private ToggleGroup group = new ToggleGroup();
private IntegerProperty elementCount = new SimpleIntegerProperty();
private ObservableMap<Integer, String> elements = FXCollections.observableHashMap();
CheckBox[] checkBoxes = new CheckBox[count];
private ObservableList<String> options = FXCollections.observableArrayList(
"ciao",
"hello",
"halo");
private Stage dialogStage;
private Configuration configuration;
private boolean okClicked = false;
/**
* Initializes the controller class. This method is automatically called
* after the fxml file has been loaded.
*/
#FXML
private void initialize() {
HBox radioBox = createModesRadios(elementCount, modes);
GridPane grid = new GridPane();
VBox root = new VBox(20, radioBox);
root.setPrefSize(680, 377);
grid.setHgap(40);
grid.setVgap(30);
grid.setAlignment(Pos.CENTER);
elementCount.addListener((o, oldValue, newValue) -> {
// uncheck checkboxes, if too many are checked
updateCheckBoxes(checkBoxes, newValue.intValue(), -1);
});
for (int i = 0; i < count; i++) {
final Integer index = i;
CheckBox checkBox = new CheckBox();
checkBoxes[i] = checkBox;
PrefixSelectionComboBox<String> comboBox = new PrefixSelectionComboBox<>();
comboBox.setItems(options);
comboBox.setPromptText("Select Test Bus " + i);
comboBox.setPrefWidth(400);
comboBox.setVisibleRowCount(options.size() -1);
comboBox.valueProperty().addListener((o, oldValue, newValue) -> {
// modify value in map on value change
elements.put(index, newValue);
});
comboBox.setDisable(true);
checkBox.selectedProperty().addListener((o, oldValue, newValue) -> {
comboBox.setDisable(!newValue);
if (newValue) {
// put the current element in the map
elements.put(index, comboBox.getValue());
// uncheck checkboxes that exceed the required count keeping the current one unmodified
updateCheckBoxes(checkBoxes, elementCount.get(), index);
} else {
elements.remove(index);
}
});
grid.addRow(i, comboBox, checkBox);
}
// enable Ok button iff the number of elements is correct
okButton.disableProperty().bind(elementCount.isNotEqualTo(Bindings.size(elements)));
root.getChildren().add(grid);
dataAcquisitionTab.getChildren().add(root);
}
/**
* Create Radio Buttons with Mode class helper
*
* #param count
* #param modes
* #return
*/
private HBox createModesRadios(IntegerProperty count, Mode... modes) {
ToggleGroup group = new ToggleGroup();
HBox result = new HBox(50);
result.setPadding(new Insets(20, 0, 0, 0));
result.setAlignment(Pos.CENTER);
for (Mode mode : modes) {
RadioButton radio = new RadioButton(mode.getText());
radio.setToggleGroup(group);
radio.setUserData(mode);
result.getChildren().add(radio);
radio =
}
if (modes.length > 0) {
group.selectToggle((Toggle) result.getChildren().get(0));
count.bind(Bindings.createIntegerBinding(() -> ((Mode) group.getSelectedToggle().getUserData()).getCount(), group.selectedToggleProperty()));
} else {
count.set(0);
}
return result;
}
/**
* Method for updating CheckBoxes depending on the selected modek
*
* #param checkBoxes
* #param requiredCount
* #param unmodifiedIndex
*/
private static void updateCheckBoxes(CheckBox[] checkBoxes, int requiredCount, int unmodifiedIndex) {
if (unmodifiedIndex >= 0 && checkBoxes[unmodifiedIndex].isSelected()) {
requiredCount--;
}
int i;
for (i = 0; i < checkBoxes.length && requiredCount > 0; i++) {
if (i != unmodifiedIndex && checkBoxes[i].isSelected()) {
requiredCount--;
}
}
for (; i < checkBoxes.length; i++) {
if (i != unmodifiedIndex) {
checkBoxes[i].setSelected(false);
}
}
}
/**
* Sets the stage of this dialog.
*
* #param dialogStage
*/
public void setDialogStage(Stage dialogStage) {
this.dialogStage = dialogStage;
}
/**
* Sets the configuration to be edited in the dialog.
*
* #param configuration
*/
public void setConfiguration(Configuration configuration) {
this.configuration = configuration;
configurationNameField.setText(configuration.getConfigurationName());
commentField.setText(configuration.getComment());
//Set the comboboxes and the checkboxes and the radiobutton
creationTimeField.setText(LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm")));
}
/**
* Returns true if the user clicked OK, false otherwise.
*
* #return
*/
public boolean isOkClicked() {
return okClicked;
}
/**
* Called when the user clicks ok.
*/
#FXML
private void handleOk() {
if (isInputValid()) {
configuration.setConfigurationName(configurationNameField.getText());
configuration.setComment(commentField.getText());
configuration.setTestBus1(elements.get(0));
configuration.setTestBus2(elements.get(1));
configuration.setTestBus3(elements.get(2));
configuration.setTestBus4(elements.get(3));
configuration.setCreationTime(DateTimeUtil.parse(creationTimeField.getText()));
// Store the information of the sample mode (vertical, hybrid or horizontal).
okClicked = true;
dialogStage.close();
}
}
Mode Helper Class
public class Mode {
private final String text;
private final int count;
public Mode(String text, int count) {
this.text = text;
this.count = count;
}
public String getText() {
return text;
}
public int getCount() {
return count;
}
}
Configuration.java
public class Configuration {
private final StringProperty configurationName;
private final StringProperty comment;
private final StringProperty testBus1;
private final StringProperty testBus2;
private final StringProperty testBus3;
private final StringProperty testBus4;
private final StringProperty triggerSource;
private final StringProperty sampleMode;
private final StringProperty icDesign;
private final StringProperty triggerMode;
private final DoubleProperty acquisitionTime;
private final ObjectProperty<LocalDateTime> creationTime;
/**
* Default constructor.
*/
public Configuration() {
this(null, null);
}
/**
* Constructor with some initial data.
*
* #param configurationName
* #param comment
*/
public Configuration(String configurationName, String comment) { //todo initialize null values
this.configurationName = new SimpleStringProperty(configurationName);
this.comment = new SimpleStringProperty(comment);
// Some initial sample data, just for testing.
this.testBus1 = new SimpleStringProperty("");
this.testBus2 = new SimpleStringProperty("");
this.testBus3 = new SimpleStringProperty("");
this.testBus4 = new SimpleStringProperty("");
this.triggerSource = new SimpleStringProperty("");
this.sampleMode = new SimpleStringProperty("");
this.icDesign = new SimpleStringProperty("Ceres");
this.triggerMode = new SimpleStringProperty("Internal");
this.acquisitionTime = new SimpleDoubleProperty(2346.45);
this.creationTime = new SimpleObjectProperty<>(LocalDateTime.of(2010, 10, 20,
15, 12));
}
/**
* Getters and Setters for the variables of the model class. '-Property' methods for the lambdas
* expressions to populate the table's columns (just a few used at the moment, but everything
* was created with scalability in mind, so more table columns can be added easily)
*/
// If more details in the table are needed just add a column in
// the ConfigurationOverview.fxml and use these methods.
// Configuration Name
public String getConfigurationName() {
return configurationName.get();
}
public void setConfigurationName(String configurationName) {
this.configurationName.set(configurationName);
}
public StringProperty configurationNameProperty() {
return configurationName;
}
// Comment
public String getComment() {
return comment.get();
}
public void setComment (String comment) {
this.comment.set(comment);
}
public StringProperty commentProperty() {
return comment;
}
// Test Bus 1
public String getTestBus1() {
return testBus1.get();
}
public void setTestBus1(String testBus1) {
this.testBus1.set(testBus1);
}
public StringProperty testBus1Property() {
return testBus1;
}
// Test Bus 2
public String getTestBus2() {
return testBus2.get();
}
public void setTestBus2(String testBus2) {
this.testBus2.set(testBus2);
}
public StringProperty testBus2Property() {
return testBus2;
}
// Test Bus 3
public String getTestBus3() {
return testBus3.get();
}
public void setTestBus3(String testBus3) {
this.testBus3.set(testBus3);
}
public StringProperty testBus3Property() {
return testBus3;
}
// Test Bus 4
public String getTestBus4() {
return testBus4.get();
}
public void setTestBus4(String testBus4) {
this.testBus4.set(testBus4);
}
public StringProperty testBus4Property() {
return testBus4;
}
// Trigger Source
public String getTriggerSource(){
return triggerSource.get();
}
public void setTriggerSource (String triggerSource){
this.triggerSource.set(triggerSource);
}
public StringProperty triggerSourceProperty() {
return triggerSource;
}
// Sample Mode
public String getSampleMode() {
return sampleMode.get();
}
public void setSampleMode (String sampleMode){
this.sampleMode.set(sampleMode);
}
public StringProperty sampleModeProperty() {return sampleMode;}
// IC Design
public String getIcDesign() {
return icDesign.get();
}
public void setIcDesign (String icDesign){
this.icDesign.set(icDesign);
}
public StringProperty icDesignProperty() {
return icDesign;
}
// Trigger Mode
public String getTriggerMode() {
return triggerMode.get();
}
public void setTriggerMode (String triggerMode){
this.triggerMode.set(triggerMode);
}
public StringProperty triggerModeProperty() {return triggerMode;}
// Acquisition Time
public double getAcquisitionTime() {
return acquisitionTime.get();
}
public void setAcquisitionTime(double acquisitionTime){
this.acquisitionTime.set(acquisitionTime);
}
public DoubleProperty acquisitionTimeProperty() {
return acquisitionTime;
}
// Last modified - Creation Time
#XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
public LocalDateTime getCreationTime() {
return creationTime.get();
}
public void setCreationTime(LocalDateTime creationTime){
this.creationTime.set(creationTime);
}
public ObjectProperty<LocalDateTime> creationTimeProperty() {
return creationTime;
}
}
You can use the java.util.Properties class to easily read/write settings like this to/from an XML file.
Save To Properties
Properties props = new Properties();
// Set the properties to be saved
props.setProperty("triggerMode", triggerMode.get());
props.setProperty("acquisitionTime", String.valueOf(acquisitionTime.get()));
// Write the file
try {
File configFile = new File("config.xml");
FileOutputStream out = new FileOutputStream(configFile);
props.storeToXML(out,"Configuration");
} catch (IOException e) {
e.printStackTrace();
}
This creates the config.xml file and populates it with all the properties you set using the props.setProperty() method.
Within the props.setProperty() method, you have two parameters. The first is the name of the property, the second is the actual value. The name is important as you will use it to read the appropriate value later.
The above code outputs the following XML file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Configuration</comment>
<entry key="acquisitionTime">12.0</entry>
<entry key="triggerMode">My Trigger Mode</entry>
</properties>
Read from Properties
Reading from the XML file is just as simple. Use the loadFromXML() method:
FileInputStream in;
// Load the settings file
in = new FileInputStream(DataFiles.LOCAL_SETTINGS_FILE);
properties.loadFromXML(in);
From there, you can set your Configuration model object by getting each property:
configuration.setAcquisitionTime(props.getProperty("acquisitionTime", "0.0"));
configuration.setTriggerMode(props.getProperty("triggerMode", "Manual"));
The getProperty() method also takes two parameters. The first is obviously the name of the property we saved earlier. The second is the default value to use if the requested property name does not exist in the XML file.
Pretty simple!
I do recommend updating all the values in your Configuration.java model and saving properties from there (instead of trying to save the properties directly from your textfields, comboboxes, etc).
EDIT
In order to retrieve the values from your controls within the for loop, you need to add them to a list that is accessible from outside that loop.
Create appropriate lists to hold the controls as their created:
List<PrefixSelectionComboBox<String>> comboBoxes = new ArrayList<PrefixSelectionComboBox<String>>();
List<CheckBox> checkBoxes = new ArrayList<>();
At the bottom of your loop, add the new controls to these lists:
comboBoxes.add(comboBox);
checkBoxes.add(checkBox);
grid.addRow(i, comboBox, checkBox);
Now, when you need their values, you can iterate over those lists to extract them:
for (PrefixSelectionComboBox cb : comboBoxes) {
cb.getValue(); // Do with this what you must
}
for (CheckBox cb : checkBoxes) {
cb.isSelected();
}
public class SerialCommunicationController{
#FXML public Label tempReading;
#FXML public Label errorReading;
private final Logger logger = LoggerFactory.getLogger(getClass());
private ArduinoInterfaceControl arduinoInterfaceControl;
private Stage stage;
private String prop;
private String inte;
private String deri;
private boolean lastCommandSuccessful;
public static final String LOCAL_SIMULATOR = "Local Simulator";
private String[] params;
private ArduinoInterfaceControl arduinoInterface;
public SerialCommunicationController() {
}
public SerialCommunicationController(String cmd){
setValues(cmd);
}
public void setValues(String readings){
String[] params = readings.split("\\s+");
// something went wrong, just decode to blank.
if(params.length < 1) {
lastCommandSuccessful = false;
}
else {
tempReading.setText(params[0]);
errorReading.setText(params[1]);
// successful command received.
lastCommandSuccessful = Boolean.valueOf(params[1]);
}
}
}
Hi Guys i am getting NullPointException when trying to do tempReading.setText() and errorReading.setText(). I have defined default values of label text in my fxml file.
It seems that both tempReading and errorReading are null.
Perhaps binding to the FXML is incorrect?
Make sure the fx:id in the FXML file matches the names of your Label fields in the Java class.
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.