How can I display a DoubleProperty on my scene? - java

I want this totalSalesAmountProperty to display the value but even after it is updated, and has an actual value, it still doesn't display. I know this has a value because I system.out the getter method and I get a value. Why would that be happening?
Label lblTotalSales = new Label(String.valueOf(newSale.getTotalSalesAmount1()));

You need to bind the label's text property to the totalSalesAmountProperty:
label.textProperty().bind(totalSalesAmountProperty);
Then the label text will automatically update any time the totalSalesAmountProperty is modified.

try this (what is the return type of getTotalSalesAmount1() ?? Here I consider it double)
Label lblTotalSales = new Label(" " + newSale.getTotalSalesAmount1());

By using
Label lblTotalSales = new Label(String.valueOf(newSale.getTotalSalesAmount1()));
you set the text to the String returned by String.valueOf(newSale.getTotalSalesAmount1()) just before calling the Label constructor, i.e. it yields the same result as
String s = String.valueOf(newSale.getTotalSalesAmount1());
Label lblTotalSales = new Label(s);
Strings are neither mutable nor observable in java and therefore the text is not automatically updated.
To fix this bind the textProperty of the Label to the String version of the DoubleProperty. This will add listeners to the property that will update the text of the Label every time the DoubleProperty changes.
DoubleProperty propertyToShow = ...
Label label = new Label();
label.textProperty().bind(propertyToShow.asString());

Related

How do you edit a repeatedly initialized label?

Let's say I made a swt and a button triggers this line of code:
Label Charname = new Label(shell, SWT.NONE);
Charname.setBounds(250,10+a,500,40);
Charname.setText("Hello");
a=a+40;
I press the button twice, so it makes 2 labels, like so:
hello
hello
If I wanted to .getText the FIRST label, how would I do so? I know these labels are both the same but this is just a example, in what I'm working in these labels are different.
You just need to remember the labels you have created so you can access them again. One way would be to save them in a list in your class.
public class MyClass {
List<Label> labels = new ArrayList<>();
.... other code
Label charname = new Label(shell, SWT.NONE);
...
// Save in the list
labels.add(charname);
.....
// Access old label
int index = ... index of label required
Label oldLabel = labels.get(index);
}

Calculate % of textbox filled in

I am working on a javafx project in which I'm providing a textbox to be filled in.I want to calculate % of textbox filled in..say for eg 100 characters is a limit and 50 are filled in so 50 should be the % value but it should change automatically as i keep typing .I don't know exactly how to do that (specially the Automatic thing). I want to show that % value on progressbar like this :
(Ignore buttons)
Need help! Thank you in advance
You can define yourself a DoubleBinding which is bound to the textProperty and on each change revaluates it's value.
final double max = 100;
TextField text = new TextField();
DoubleBinding percentage = new DoubleBinding() {
{
super.bind(text.textProperty());
}
#Override
protected double computeValue() {
return text.getText().length() / max;
}
};
In the static initializer block of the DoubleBinding you bind the textProperty of your TextField. This will cause the reevaluation of the binding through the computeValue method. Then you can bind it to the textProperty of a Label:
Label lbl = new Label();
lbl.textProperty().bind(percentage.asString());
Of course you can also bind it to other controls than a Label like a ProgressBar or ProgressIndicator:
ProgressBar bar = new ProgressBar();
bar.progressProperty().bind(percentage);
ProgressIndicator indicator = new ProgressIndicator();
indicator.progressProperty().bind(percentage);
This binding then can be used to display the percentage already filled in. You might also take a look at this documentation by Oracle. The type of this binding is a low-level binding.

How to dynamically set a JLabel text

int variable = 100;
label = new JLabel("<html><font color=red>variable</font><html>");
How do I make this display "100" on screen and not "variable"
By building a String with it included:-
label = new JLabel("<html><font color=red>" + variable + "</font><html>");
This will output <html><font color=red>100</font><html> (well, in a label it will be html formatted)
The reason yours didn't work is because almost anything inside quotes is treated as a String so adding a variable in there is just the same as adding the name of the variable.
Additionally
This will also work for objects, not just primitive types like int by calling their toString() method and adding the output from that.
You can use String.format method:
label = new JLabel(String.format("<html><font color=red>%d</font><html>", variable));

using LayoutClickListener to terminary replace components

I have grid layout witch some fields added like that:
private Component userDetailsTab(final User user) {
final GridLayout details = new GridLayout(2, 1);
details.setMargin(true);
details.setSpacing(true);
details.addComponent(createDetailLabel(Messages.User_Name));
final Component username = createDetailValue(user.getName());
details.addComponent(username);
...
I have also Layout click listener which replace labels on text field, it looks like that:
final TextField tf = new TextField();
details.addListener(new LayoutClickListener() {
private static final long serialVersionUID = -7374243623325736476L;
#Override
public void layoutClick(LayoutClickEvent event) {
Component com = event.getChildComponent();
if (event.getChildComponent() instanceof Label) {
Label label = (Label)event.getChildComponent();
details.replaceComponent(com, tf);
tf.setValue(label.getValue());
}
}
});
In future I want to enable click on label, edit it and write changes to database after clicking somewhere else (on different label for example).
Now when I click on 1st label and then on 2nd label, effect is: 1st has value of 2nd and 2nd is text field witch value of 2nd. Why it's going that way? What should i do to after clicking on 1st and then 2nd get 1st label witch value of 1st?
You don't need to swap between Labels and TextFields, you can just use a TextField and style it look like a Label when it's not focused.
When I tried to create click-to-edit labels, it created a ton of extra work for me. I'd discourage it (and do as Patton suggests in the comments).
However, if you're going to insist on trying to create in-place editing, you will want to do the following:
Create a new class that extends a layout (e.g. HorizontalLayout), which can swap out a label for a text field
use LayoutClickListener to removeComponent(myLabel) and addComponent(myTextField)
use BlurListener to swap back to the label
use ValueChangeListener on the text field to copy its value to the label
This is a still a bad idea because:
Users cannot see affordances as easily (they can't tell what's editable)
Users cannot use the keyboard to tab to the field they want to edit
It adds unncessary complexity (maintenance time, etc).
I would recommend, if you want in-place editing, just show the text field, and save the new value with the BlurListener.

Get the size of a Draw2d Label Figure

if I have a class with a private Label = new Label(""); in it and in some method i write:
private void setText(String text)
{
this.label.setText(text);
System.out.println("label size = " + this.label.getSize(0,0));
}
it will always print "label size = Dimension(0,0)". why is this? how can I obtain the size occupied by the label after setting its text? I also tried other solutions (here and method getTextBounds() as suggested in here ) but i either obtain again Dimension(0,0) or a NullPointerException, respectively.
do you have any suggestion? thanx :)
this.label.getPreferredSize() is what you're looking for. It returns the space your label would like to occupy.
But at this point the label doesn't know yet what font to use, hence the NullPointerException. Once your figure tree has been set e.g. as the content of a FigureCanvas the font should be available. Alternatively, you could explicitly set a font before calling getPreferredSize().
To add a rounded rectangle around your label, like you requested in your comment, you could do the following:
RoundedRectangle rr = new RoundedRectangle();
rr.setBorder(new MarginBorder(4));
rr.setLayoutManager(new StackLayout());
rr.add(new Label("label text"));

Categories