I guess you want to construct your query depending on the selected JCheckBox
es.
The below code snippet works, if:
- You created a
JCheckBox[] checkBoxes
field that contains all the checkboxes with languages. - The text of all those
JCheckBox
is exactly theString
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 String
s in the query and as text of the checkboxes you could e.g. store those String
s 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 Stream
s 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