Bold Text Parts in ControlsFX Notification - javafx

I'm trying to write bold text inside the body of a Notification.
Notifications.create()
.title("My Title")
.text("This is <bold>text</bold>")
.showInformation();
As far as I can tell there's only the .text("my text") method to set the text of a Notification.
However I'd like to use a TextFlow in there to modify certain parts of a text.
I've also tried to use CSS for this but that only lets me change to overall appearances of the Notification as setting specific text through CSS is explicitly not supported.
My question now is: Is there a way to style parts of a text inside a Notification differently?

Got it working now.
The solution is to use .graphic(Node node). If you still want to display an Image at the left side you need to put everything in a Pane and add some padding.
I yoinked the original image from here.
BorderPane borderPane = new BorderPane();
borderPane.setLeft(new ImageView(Controller.class.getResource("/dialog-information.png").toExternalForm()));
TextFlow textFlow = new TextFlow();
textFlow.setPadding(new Insets(15, 0, 5, 5));
Text text1 = new Text("This is ");
Text text2 = new Text("bold");
text2.setStyle("-fx-font-weight: bold");
textFlow.getChildren().addAll(text1, text2);
borderPane.setRight(textFlow);
Notifications.create()
.title("My Title")
.graphic(borderPane)
.hideAfter(Duration.seconds(10))
.show();
It is important to use .show() and not .showInformation/Error/Warning() because that would override the BorderPane.
Result:

Related

How to resize QListWidgetItem according to it's content?

I'm adding a QListWidgetItems to a QListWidget. Is there any way to set the size of the QListWidgetItem according to it's content data?
QSize size(50, 20);
QListWidgetItem* newItem1 = new QListWidgetItem();
newItem1->setText("short text");
newItem1->setSizeHint(size);
listWidget->addItem(newItem1); //listWidget is previously created
QListWidgetItem* newItem2 = new QListWidgetItem();
newItem2->setText("this is a very long text");
newItem2->setSizeHint(size);
listWidget->addItem(newItem2);
Text of newItem1 is displayed without any problem. But the newItem2 text is not fully displayed. It only shows few characters and then "..." as text elide. How to show the complete text without the elide? I want to set the size according to the size of item's data without setting any constant numbers.
I simply copy/pasted your code, removed setSizeHint() function call for both items and added some of mine. Here's working code:
QListWidgetItem* newItem1 = new QListWidgetItem();
newItem1->setText("short text");
ui->listWidget->addItem(newItem1);
QListWidgetItem* newItem2 = new QListWidgetItem();
newItem2->setText("this is a very long text");
ui->listWidget->addItem(newItem2);
ui->listWidget->setFixedSize(ui->listWidget->sizeHintForColumn(0) + ui->listWidget->frameWidth() * 2,
ui->listWidget->sizeHintForRow(0) * ui->listWidget->count() + 2 * ui->listWidget->frameWidth());
As you can see, both items are fully displayed.
Actually I have accidentally enabled uniformItemSizes for the QListWidget. When I disable that, QListWidgetItems were automatically resized according to it's content.

JavaFx TextFlow does not resize on GridPane on change of value

I have a TextFlow in a GridPane, when I change its value to a wider one it does not increase its width accordingly on display but truncates. I am a newbie at Java and I've probably missed something simple.
Initial value is built as follows;
TextFlow mhText = new TextFlow();
// Loop to build TextFlow
for(Card c : myGame.me.playerHands.get(0).handCards) {
Text text = new Text(c.cardText(false) + " ");
if(c.redSuit()) {text.setFill(Color.RED);}
mhText.getChildren().add(text);
}
playerGrid.add(mhText, 1, pgRow)
Then later it is rebuilt...
mhText.getChildren().clear();
for(Card c : myHand.handCards) {
Text text = new Text(c.cardText(false) + " ");
if(c.redSuit()) {text.setFill(Color.RED);}
mhText.getChildren().add(text);
}
Result screenshot
You can just see from the screenshot that only a dot from the additional 3rd 'card' is showing, I think due to truncation. I've tried using setPrefWidth and setMinWidth without success.
I can solve the problem by removing and re-adding the TextFlow on the GridPane,
playerGrid.getChildren().remove(mhText);
playerGrid.add(mhText, 1, myHandRow);
but I would be surprised if this is the right way, any advice is welcome.

Add labels dynamically on Groupbox but only half text is displaying

I am trying to add dynamic labels on groupbox, but my text size which I also created dynamically is only showing half of it on group box.
This is my simple code:
private void AddLabels()
{
List<Label> labels = new List<Label>();
groupBox1.Controls.Clear();
Label l = new Label();
l.Font = new Font(new FontFamily(System.Drawing.Text.GenericFontFamilies.Serif), 30, FontStyle.Bold);
l.Text = "Hello World";
l.Parent = groupBox1;
l.BringToFront();
labels.Add(l);
}
As you notice on my code, my Font Size is 30, but when I run this, only "Hello" text is showing and only half of "Hello" is showing. The word "World" is not showing also.
Just in case you encounter the same problem. I already figured it the day I posted my question and I want to answer this just in case someone is looking for an answer.
Just adjust your label font size and everything will work fine.

Lower text in a QMessagebox

I have a QMessagebox with a custom background image. Since there is some stuff on the top side of the background image I want to see, the text of the messagebox should be lowered. Does anybody know how I can do this? I already tried throwing in some white lines using br, so:
popup.setText("<font size =5 color =white ><br>""<br>""<br>""Are you sure you
want to erase the memory</font> ");
but this screws up the background picture. Is there any way I can move the "box" that contains the text to a lower position?
You could try to get the QMessageBox' layout, get the label which holds your text and increment the labels margin. This probably is a hack and might make your project unportable. Construct your QMessageBox, call hack and then exec the box.
void hack(QMessageBox* pMessageBox)
{
QGridLayout* grid = qobject_cast<QGridLayout*>(pMessageBox->layout());
if (grid)
{
QLabel* label = qobject_cast<QLabel*>((grid->itemAtPosition(0,1))->widget());
if (label)
{
label->setMargin(label->margin()+5); // whatever is suitable
}
}
}

calculating Spark TextArea width

I am treating a spark TextArea as text input(by setting heightInLines="1"). The TextArea is part of an mxml component and I want to resize the component when the text is changed.
I haven't been able to use textArea.measureaText(textArea.text) to get line metrics and use it. I get this error "Parameter antiAliasType must be non-null."
Is there any way to get the width of a TextArea which it is going to consume at runtime for a particular string or a particular TextFlow?
A little ugly but works for me:
var uiText:UItextField = new UITextField();
// if you have custom text style on the TextArea then set it the same of this uitextfield
uiText.setTextFormat("bla bla bla");
uiText.text = "This is the text that I want the size for";
var textW:Number = uiText.textWidth;
var textH:Number = uiText.textHeight;
// then we need to consider also the border of the text area and the paddings
// try read the padding values and border sizes using getStyle(...)
myTextArea.width = textW+2*borderW+paddingL+paddingR;
myTextArea.height = textH+2*borderH+paddingT+paddingB;
That should be all

Resources