java - JavaFX TableView with highlighted text -
i want highlight parts of text displayed in javafx
tableview
. till i'm using text
objects in textflow
objects. highlight specific parts in text using tags cutting text in parts (javafx.scene.text
objects) highlight or not highlight next code.
col3.setcellvaluefactory(new propertyvaluefactory<regexmatch, string>("text")); col3.setcellfactory(new callback<tablecolumn, tablecell>() { @override public tablecell call(tablecolumn param) { tablecell cell = new tablecell() { @override protected void updateitem(object text, boolean empty) { if (text != null && text instanceof string) { string str = (string) text; textflow flow = new textflow(); if (txtsearchfield.gettext().length() > 3 && str.contains(highlight_start)) { // highlight flow.getchildren().clear(); while (str.contains(highlight_start)) { // first part text starttext = new text(str.substring(0, str.indexof(highlight_start))); starttext.setwrappingwidth(double.max_value); flow.getchildren().add(starttext); str = str.substring(str.indexof(highlight_start) + highlight_start.length(), str.length()); // part highlight text highlightedtext = new text(str.substring(0, str.indexof(highlight_end))); highlightedtext.setstyle("-fx-text-background-color: yellow;"); highlightedtext.setfill(color.blue); highlightedtext.setwrappingwidth(double.max_value); flow.getchildren().add(highlightedtext); // lastly part str = str.substring(str.indexof(highlight_end) + highlight_end.length(), str.length()); if (!str.contains(highlight_start)) { text endtext = new text(str); endtext.setwrappingwidth(double.max_value); flow.getchildren().add(endtext); } } }else if (txtsearchfield.gettext().length() < 1) { // remove former highlightings , show simple text str = str.replaceall(highlight_start, ""); str = str.replaceall(highlight_end, ""); flow.getchildren().clear(); text textmodule = new text(str); textmodule.setwrappingwidth(double.max_value); flow.getchildren().add(textmodule); } else { // show simple text flow.getchildren().clear(); text textmodule = new text(str); textmodule.setwrappingwidth(double.max_value); flow.getchildren().add(textmodule); } flow.setprefheight(bigicons ? big_size : small_size); setgraphic(flow); } } }; homecoming cell; } });
unfortunately background highlighting not work , have unusual linebreaks shown in picture. text not contain linebreaks.
(sorry image quality, real screenshot :))
any help appreciated.
solution @eckig suggested, using multiple labels
in hbox
idea, because each 'label' can have own background color , can utilize much labels
in line in hbox
needed:
col3.setcellvaluefactory(new propertyvaluefactory<regexmatch, string("excerptlinetable")); col3.setcellfactory(new callback<tablecolumn, tablecell>() { @override public tablecell call(tablecolumn param) { tablecell cell = new tablecell() { @override protected void updateitem(object text, boolean empty) { if (text != null && text instanceof string) { hbox hbox = new hbox(); string str = (string) text; if (txtsearchfield.gettext().length() > 3 && str.contains(highlight_start)) { // highlight hbox.getchildren().clear(); while (str.contains(highlight_start)) { // first part label label = new label(str.substring(0, str.indexof(highlight_start))); hbox.getchildren().add(label); str = str.substring(str.indexof(highlight_start) + highlight_start.length(), str.length()); // part highlight label label2 = new label(str.substring(0, str.indexof(highlight_end))); label2.setstyle("-fx-background-color: blue;"); hbox.getchildren().add(label2); // lastly part str = str.substring(str.indexof(highlight_end) + highlight_end.length(), str.length()); if (!str.contains(highlight_start)) { label label3 = new label(str); hbox.getchildren().add(label3); } } } else if (txtsearchfield.gettext().length() < 1) { // remove former highlightings , show simple text str = str.replaceall(highlight_start, ""); str = str.replaceall(highlight_end, ""); hbox.getchildren().clear(); label label = new label(str); hbox.getchildren().add(label); } else { // show simple text hbox.getchildren().clear(); label label = new label(str); hbox.getchildren().add(label); } setgraphic(hbox); } } }; homecoming cell; } });
after double reading problem think can resume follows:
a rather long text in tablecolumn the user should able filter text each text-fragment matches current search term highlighted.now have 2 options:
create hbox , add together labels containg text fragments. label extends part , may have background color. use textflow , add together texts text fragments. there can "only" alter foreground color.ah , before forget: disable textflow text wrapping must not phone call textmodule.setwrappingwidth(double.max_value);
instead textmodule.setprefwidth(double.max_value);
and hint: seek recycle much possible. recreating whole textflow on each update not thought (instead store fellow member variable in cell).
java javafx tableview textflow
No comments:
Post a Comment