0

I have a loop who adds values in a dataset in 3 series:

for (int j = 0; j < gcArrayList.size(); j++) {
    if (j % 3 == 0) {
        datasetBar.addValue(Float.parseFloat((String) gcArrayList.get(j)),series1, gcDateList.get(j / 3));
    } else if (j % 3 == 1) {
        datasetBar.addValue(Float.parseFloat((String) gcArrayList.get(j)),series2, gcDateList.get(j / 3));
    } else {
        datasetBar.addValue(Float.parseFloat((String) gcArrayList.get(j)),series3, gcDateList.get(j / 3));
    }       
}

Then I want those dataset to add values in each series only if their checkbox is checked( I have 3 checkboxes for "First array", "Second array" , "Third array").

    array1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JCheckBox ch = (JCheckBox) e.getSource();

            if(ch.isSelected()) {

            } else {

            }
        }
    });

Also the checkboxes are in a different class than the method who adds values in datasets.

I don't know how to make the connection between the ActionListener of the checkbox and the datasetBar.addValue().

I tried something like:

if(j % 3 ==0 && array1.isSelected()) ...

I knew it won't work but I think the solution is somewhere in that area... Can anybody help me?

Thanks.

Edgar Rokjān
  • 17,245
  • 4
  • 40
  • 67
DCalin
  • 67
  • 7
  • One approach is shown [here](http://stackoverflow.com/a/13919878/230513). – trashgod Dec 16 '15 at 19:09
  • Thanks. I was looking at that approach but didn't found out how to apply it to my problem. Can you explain me, please, how that can help me? I don't know how to make the 'addValue' function listen to the checkbox. – DCalin Dec 16 '15 at 20:24

1 Answers1

1

I don't know how to make the addValue() function listen to the check box.

This example stores the state of each check box the TableModel associated with a JTable. Alternatively, use a List<Boolean> as your model. Each check box should have an instance of an ActionListener that updates the corresponding list element. Use the check box's putClientProperty() method to keep track of the list index for each check box. As shown in this PropertyChangeDemo, you can add a PropertyChangeListener to the enclosing Container of the components you want to observe and fire an appropriate PropertyChangeEvent in a check box's ActionListener.

Rather than adding values to each series, this related example extends AbstractAction to allow a JCheckBox to toggle the visibility of individual series.

@Override
public void actionPerformed(ActionEvent e) {
    renderer.setSeriesVisible(i, !renderer.getSeriesVisible(i));
}

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045