简体   繁体   中英

Adding values in a dataset only if a checkbox is checked

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.

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));
}

图片

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM