javafx label text color dynamic bindings - javafx

I have a label labelSign which is showing the +ve or -ve sign.
and I have another label labelValue and the text color of the label is depending on the text of labelSign.
This code is ok if there are two kinds of colors:
labelValue.textFillProperty().bind(Bindings.when(labelSign.textProperty().isEqualsTo("+ve")).then(Color.GREEN).otherwise(Color.RED));
How to handle if there are 3 cases of labelSign: +ve, -ve and empty and painting the text of labelValue as BLACK if the labelSign is empty?

Use Bindings.createObjectBinding to create a binding with the text property as dependency.
private static Color textToColor(String text) {
...
}
labelValue.textFillProperty().bind(Bindings.createObjectBinding(() -> textToColor(labelSign.getText()), labelSign.textProperty());
This allows you to use an arbitrary algorithm to determine the color based on the text. An update happens everytime one of the dependencies (in this case the text property of the Label) is updated.
On the other hand you can set an arbitrary text color without changing the displayed result, if the text is empty (= empty string)...

Related

How to increase row height in vaadin grid accordingly to data in the cell

I am consuming data from the list and some list may contain multiple line data, so how that can be reflected in the vaadin grid at runtime.
This is the code
public Grid GridBasic() {
grid.removeAllColumns();
grid.addColumn(TopicConsumedDetails::getPartitionNumber).setHeader("PARTITION ");
grid.addColumn(TopicConsumedDetails::getOffset).setHeader("OFFSET");
grid.addColumn(TopicConsumedDetails::getMessage).setHeader("MESSAGE");
grid.setItems(details);
grid.addThemeVariants(GridVariant.LUMO_WRAP_CELL_CONTENT);
grid.getColumns().forEach( col -> col.setAutoWidth(true));
return grid;
}
This just displays all the data in a single line and it requires scrolling left to right.
Vaadin Version :23.3.1
Use the built-in "wrap cell content" variant: https://vaadin.com/docs/latest/components/grid/#wrap-cell-content
grid.addThemeVariants(GridVariant.LUMO_WRAP_CELL_CONTENT);
As per the previous answer, I think that using this is the correct approach:
grid.addThemeVariants(GridVariant.LUMO_WRAP_CELL_CONTENT);
However, you are overriding this setting by calling
grid.getColumns().forEach( col -> col.setAutoWidth(true));
According to the documentation, this automatically sets the column width based on the contents, leading to the right scroll problem.
If you remove this call, you should get the proper wrapping behavior. I was able to reproduce the problem and then see a result like this once I removed the auto width setting:
Alternatively, if you have sensible line breaks in the content you want to wrap, you can do that--but it won't happen automatically, as mentioned by #Rolf in a comment above. That is because the line breaks in the text are basically just whitespace and aren't respected as such by the HTML. So in order to do that, one option is to add an "Html" component column. You can then replace your text's line breaks with <br/> tags, which you could do with a regular expression. It would look like this:
String myColumnText = colText.replaceAll(....); //Some regex to match on your particular line breaks
grid.addComponentColumn(item -> new Html("<p>" + myColumnText +"</p>");
The <p> tags (or some wrapper tags) are required as the Html component requires a wrapper tag.
Note that (1) using this approach means that you won't get the automatic wrapping behavior any more so the line breaks in your source need to be sensible and (2) you have to be certain you trust the incoming content or it is otherwise sanitized, as this kind of rendering-text-as-html opens up some security holes with untrusted content.

How Can I do not display the XRRichText ,if there is no data?

I am using the XRRichText.visible=off if there is no data, but still it throwing some spaces in report. I do not want those spaces if there is no data.
Just want to display none & no spaces . How can I do this?
The upper spaces are just XRRichText.
Set the property ProcessNullValues for the labels with the issue as ‘Suppress and Shrink’.
The purpose of this property value is: If a control receives a null value, it is not printed (without adding blank space in its place).
The property has two more values:
Leave – A control is always printed.
Suppress - If a control receives a null value, a blank space is printed instead.

Multiline title on a JavaFx Chart

Is there some means - short of extending the JavaFX charting base class(es) - to specify a multi-line title string?
As seen in the screenshot the title is the only element 'wanting' more space.
I had seen some references to using a newline '\n' and even a pipe character '|' in the string but they apparently do not work for the title.
I just threw this in a sample I had and it worked.
chart.setTitle("really long title...........\n.............and some more ");
Label l = (Label)chart.lookup(".chart-title");
l.setWrapText(true);
The \n sets the break point if I don't want it at the limit.
As you can see it's just a Label, the hard part is getting it.
You can also use a css file with the selector. I think it's already centered.
.chart-title{
-fx-wrap-text : true;
-fx-alignment : center;
}

How to Convert Qcolor value to hex value?

I have implemented a QColor dialog box which opens on certain condition. To get the selected color after final selection I use the method selectedColor() which returns the value in QColor. When I print that value, it's like this:
<PyQt4.QtGui.QColor object at 0x01DD7880>
I want color value in hex value like this: #DFDFDF (for grey). If it's not hex, correct me.
Is there any function to convert that?
Any suggestions welcome.
You need to print selectedColor().name() to print the actual color value in hex. See the QColor Documentation
To amplify a bit, maybe confuse, maybe clarify... (For Python newbies)
color = QColorDialog.getColor(pWidget.textBackgroundColor(), pWidget, 'Get Text Highlighting Color')
The above will return a QColor using the QColorDialog, for those of us who don't want to be stuck with named colors like 'Blue', 'red', green etc.
fg = color.name()
In this case I am converting the QColor to a string HEX for use in a style sheet.
Widget.setStyleSheet('background-color: ' + bg + ';color: ' + fg)
This is how such a converted value can be used in a style sheet.
Note how to concatenate more than one stylesheet attribute. Also, side note, sometimes changing one attribute cancels previous changes to others.

How to edit multiline text in cell of QTableWidget?

How to edit cell manually like in Excel:
Alt + Enter -> line break (true break, not simply line break symbol on the same line)
Enter -> exit from edit mode?
EDIT:
Text in one cell in view mode:
Line1
Line2
Same text in edit mode:
Line1 Line2
Besides, I cannot write newline in cell, I can insert it only by copying.
A easy solution is to subclass QTableWidget and reimplement the keyPressEvent function.
You just need to change the behaviour in the cases you want and call the parent in the ones you don't want to handle.
Have a look at keyPressEvent(QKeyEvent *) method documentation in QWidget doc.

Resources