0

I work on Java FX.
I have a tableview in which I created a "select checkbox" column. The goal is when a user clicks on a checkbox, an alert message appears.
To test it, I first tried to display a System.out.println message.
The problem are :

  • when I only select one checkbox, I have a System.out.println message that writes all the checkboxes :
com.calculatrice.app.model.Person@34752060
com.calculatrice.app.model.Person@654bd7bc
com.calculatrice.app.model.Person@74bbebe0
com.calculatrice.app.model.Person@2e23be4f
com.calculatrice.app.model.Person@3348edcb
com.calculatrice.app.model.Person@8052a29
com.calculatrice.app.model.Person@23ca3422
com.calculatrice.app.model.Person@102837d2
com.calculatrice.app.model.Person@647ab6a9
  • when I select nothing, I have the same System.out.println message

How could I do to have a message with item(s), corresponding to the checkbox(es) selected ?

Here is my snippet :

  private void selectCheckBox(ActionEvent ae) {
         // personTable is the Tableview ; Person is the class where the getter is declared
        for(Person p : personTable.getItems()){
            if (p.getSelect().isSelected()){
                System.out.println (p + " is selected");
            }
        }
}
Julien
  • 45
  • 1
  • 3
  • 15
  • 1
    I think overriding the `toString` method will help solve this problem. Well, I guess it's not really a problem because it's expected behavior. – SedJ601 Oct 25 '19 at 16:31
  • Ok, thanks for your answer. Could I ask you how to make it ? – Julien Oct 25 '19 at 16:34
  • 4
    Please provide a [mre] that demonstrates the problem. – Slaw Oct 25 '19 at 17:40
  • 1
    I don’t know what your implementation is, but if the selected field on Person is not a full BooleanProperty and you aren’t using [CheckboxTableCell](https://openjfx.io/javadoc/12/javafx.controls/javafx/scene/control/cell/CheckBoxTableCell.html), you probably should be. You may also have other issues in the code we cannot see. – jewelsea Oct 25 '19 at 18:24
  • Possible duplicate of [JavaFX: CheckBoxTableCell get ActionEvent when user check a checkBox](https://stackoverflow.com/questions/28671132/javafx-checkboxtablecell-get-actionevent-when-user-check-a-checkbox) – SedJ601 Oct 25 '19 at 19:17

1 Answers1

1

You could label the checkboxes then use getLabel to get their name.

        if (p.getSelect().isSelected()){
            System.out.println (p.getLabel() + " is selected");
        }

Alternatively, if you don't want visible labels, you can use setName and getName.

Yserbius
  • 1,375
  • 12
  • 18
  • Thank you for your answer. The problem is that I still see the same behaviour. When I select one checkbox, the message returns the labels of every checkbox. – Julien Oct 25 '19 at 16:36
  • i'm just guessing but the problem could be in the internal implementation of the Person class – SDIDSA Oct 25 '19 at 16:44
  • Also if you are using bindings, make sure you bind and unbind them at appropriate times. When I started with javafx I would end up with 100 things bound to one control. – SephB Oct 25 '19 at 23:50