[Solved] Parameter pass method two method in java


I guess you want to construct your query depending on the selected JCheckBoxes.

The below code snippet works, if:

  1. You created a JCheckBox[] checkBoxes field that contains all the checkboxes with languages.
  2. The text of all those JCheckBox is exactly the String that should be placed inside the '.

public void search() {
    // join text of all selected JCheckBoxes from checkBoxes array
    String conditions = Arrays.stream(checkBoxes) // create a stream of language checkboxes
            .filter(JCheckBox::isSelected) // restrict stream to selected checkboxes
            .map(JCheckBox::getText) // convert from checkbox to query string
            .collect(Collectors.joining("' or language like '")); // join query strings using a delimiter

    ...

    if (!conditions.isEmpty()) {
        // at least one language selected
        ...

        String query = "select language from language where language like '"+ conditions+"'  ;";

        ...    
    }

    ...
}

If you want to use different Strings in the query and as text of the checkboxes you could e.g. store those Strings in a field HashMap<JCheckBox, String> checkboxToQueryString and use map(checkboxToQueryString::get) instead of map(JCheckBox::getText).

If you use a java version <8 it shouldn’t be too difficult to rewrite the code part involving Streams and method references.

Note that using streams for only 2 checkboxes is a bit of an overkill. You can simplyfy the code as you feel appropriate. The approach shown above works for a arbitrary number of checkboxes however.

solved Parameter pass method two method in java