javaFX Storing number format in label or spinner - java

I have a tableview, in one of his columns i calculate the subTotal:
Column "subTotal" = "Qty * price", after calculating the subtotals, i calculate the sum of all subtotals.
Maybe using:
- a label,So How i can store and retrieve the sum from a label as needed.
- Spinner, So How to bind the sum to the Spinner's valueProperty.
Here is the first option using a label:
DoubleBinding doubleBinding = Bindings.createDoubleBinding(() -> tableView.getItems().stream().
collect(Collectors.summingDouble(LineCommand::getSubTotal)), tableView.getItems());
lblTotalHt.textProperty().bind(Bindings.format("%3.2f", doubleBinding));
The problem how i can retreive the stored sum ?
the second option:
How to perform this using a spinner
fldTotalHt.valueProperty().....Bind(toTheSum);

For your first question: "How can I retrieve the stored sum?", you can get this from the binding with
double sum = doubleBinding.getValue();
For the second question, you can do
Spinner<Double> spinner = new Spinner(min, max, doubleBinding.getValue());
spinner.getValueFactory().valueProperty().bind(doubleBinding.asObject());
(asObject() effectively converts the ObservableValue<Number> to an ObservableValue<Double>).
But note that binding a spinner's value is pretty pointless. A bound value cannot be set, for obvious reasons, so the user would not be allowed to change the value of the spinner - this means it would have no more functionality than a label. It's really not clear why you would want to display this value in a spinner, since it really doesn't make any sense for the user to be able to change a value which is computed from other values. Why not just use a label?

If fldTotalHt is a Spinner, then you can access its value property like this
fldTotalHt.getValueFactory().valueProperty().bind(sumProperty)
About a label. I don't quite understand, what you really what. As I understood, you just can't get label's contents. But that's simply label.getText() and it's strange if you know about properties and bindings, but don't know about that.

Related

How to write value to different textfield every time i click a button?

I am having trouble overcoming this and need your assistance
picture
Every time i click a stop button there is a random number generated in the field above. What I want to do is save each number in the field on the left. Names of the textfields are textField_0, textField_1, textField_2 and so on.
izvucen = rnd.nextInt(31) + 1;
randomField.setText(Integer.toString(izvucen));
textField_i.setText(Integer.toString(izvucen)); // critical line, need to replace "i" with something
i++;
You could put all text fields in a list when they are created and access them via indexing
txtFields.get(i).setText(...)
Or put all fields in a map (Map<Integer, TextField>) and access specific ones via get()
mapOfTextFields.get(i).setText()

How to change JTable column width depending on data?

How would I resize the column for it to just fit the largest string in that column. For example in one column I might have a word like "Monday" and I want this column to be shorter than the "Wednesday" column.
The data changes often as it shows the next 3 days, so I would want Java to automatically change it depending on the input data rather than me having to manually set the column width.
My JTable is within a JScrollPane if that changes anything
Hope that makes sense. Please point me in the right direction.

String Tokenizer.nextElement for JComboBox

I'm using
int TxtAge = Integer.parseInt(tfAge.getText().trim());
to get value from my textfield and search it in database.
Then, I'm using Integer age = Integer.parseInt(stringTokenizer.nextElement().toString()); to go to next attributes in my database.
I have no problem using those codes for textfield but when I'm using the JComboBox the result won't display. How to use the StringTokenizer.nextElement() for JComboBox? Is is the same with TextField?
String sex=(String) stringTokenizer.nextElement();
I tried this code but still failed :(
You seem to have left out the relevant portions of your code, i.e. how you are handling setting/getting items in the JComboBox. Whether you read these values from a database, a file or have them hardcoded is irrelevant to the question.
Since you do ask whether it is the same as with a JTextField, I can at least answer this; it is not the same. The question indicates that you're quite new to Swing. You would probably benefit from working through a basic Swing tutorial, just to get a grip on how to work with these basic GUI elements. For JComboBox, check out Oracles own How to Use Combo Boxes.
Anyways, when working with JComboBox, you will need to first populate it with the values that users can choose from and set the currently selected value. Retrieving the currently selected value is just a simple method call.
Further, you have the possibility of making a combobox editable. This means that the user can edit the text in the combo box to something that was not pre-populated. By default, this option is turned off.
I'll provide some examples.
Initialize:
JComboBox sexComboBox = new JComboBox();
sexComboBox.addItem("Not selected");
sexComboBox.addItem("Male");
sexComboBox.addItem("Female");
sexComboBox.addItem("Do not want to disclose");
By default, the first item you added is selected. To select another one, you need to add one of the following lines:
sexComboBox.setSelectedIndex(1); // zero-based index, "Male" is selected item
sexComboBox.setSelectedItem("Female"); // sets the selected item to "Female"
To enable user to edit the contents to something that was not pre-defined, just add the line:
sexComboBox.setEditable(true);
To retrieve the currently selected value:
String selectedItem = (String) sexComboBox.getSelectedItem();

Create a two dimensional array of different data types (Date and double)

I am trying to make a program to do calculations of doubling time based on multiple readings at different points in time.
I am using a Netbeans JFrame with a JTextfield and a DateChooser with an add button. I intend to add the values in the JTextfield and the DateChooser to a two dimensional array of the data type [Date,double] each time the add button is clicked.
Once all the data is entered I have another button that then plots a graph based on these data points and calculates the doubling time and generates a report.
I am facing the following problems.
I will not know the number of elements I will have it can be 2 it can be 20 it will depend on the observations, I am unsure of how I can set this up in the context of a JTextField, JButton and DateChooser
Any help will be appreciated.
I will not know the number of elements I will have it can be 2 it can
be 20 it will depend on the observations, I am unsure of how I can set
this up in the context of a JTextField, JButton and DateChooser
I wouldn't. It would be eaiser to manage with a JTable.
This way you won't need to care how many observations need to be made, they can simply keep adding new rows as they want.
I would also make a simple Object that contains the Date and double value, maybe called Observation, which can then simply be managed by the JTable's model and if required, transferred to something like a List or array.
This links the Date and value together in a obvious manner which is not easily disconnected - IMHO
Instead of using an Array, use a List. That way you can use List.add() and you don't need to worry about how many elements are going to be in that list.
Elements in a list can be access similar to an array by using List.get(index) to get the objects or a more generic for( element : List ) {}.
You can have complex user type object with 2 fields and then have array of that object.

Java Swing - Problem in JSpinner

I am developing a Java Desktop Application and designing the GUI with the help of Netbeans Swing GUI builder.
I want to use a JSpinner in my app. I have dragged and dropped it to a JPanel. Now, I want to set its two properties:
First, It should display numbers in the range of 1 to 50. Neither less than 1 nor greater than 50. How can I set that range?
Second, when I try to get the value of it by spinner.getValue() it returns an Object. As my spinner's data type is Integer, would it be better to downcast the Object into Integer or Is there any other way to get that numeric value?
Create a SpinnerNumberModel, this should solve all your problems.
SpinnerNumberModel model =
new SpinnerNumberModel(int initialValue, int minValue, int maxValue, int step)
For further information I recommend reading How to Use Spinners
From here, the way to do this in NetBeans:
Create the JSpinner, as you have done.
Right click on it and select "Customize Code"
Set the initialization to be a spinner with a SpinnerNumberModel.
int myInt = (Integer)mySpinner.getValue();
Java has autoboxing for primitive data types, so the above code will get your spinner value as an integer, as long as you use the SpinnerNumberModel as suggested by Ham.
Ham is correct on your first question (how to limit the range of 1 to 50). For the second question, yes, you can simply cast it. Most (if not all) swing components return an Object for their value (the only notable exception being text fields).
Read the section from the Swing tutorial on "How to Use Spinners". And don't forget to check out the rest of the table of contents for Swing basics.

Categories