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.
Related
So I have been asked to help my friend with a small java project, I have a decent enough idea about programming from using VBA but not enough to help him, Im not too clued up on Java
Basically, he has a form with a text box, 2 buttons and an output field
The text box is for entering a value - when he clicks one button, it has to store the value within the text box to an array - and the other button is to output the max value within the array - I would appreciate any help with this if possible
Many thanks,
Sean
To me it sounds like homework. Since you didn't bother posting any code you have written yourself I will outline what I would do. You could use the getText() from a JTextField and store the output in a String. Then you could split that string by white space, provided it only contains integers. After this I would call the Integer.parseInt() method while iterating over the array. Be careful as the input in the box must be only integers, otherwise your program will throw exceptions.
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.
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.
I am trying to make a properties frame just like the one in netBeans (or Visual Studio). My problem is that I don't know exactly how to design it. First I thought I'll make it with JTable (2 columns, multiple rows) but then I realised that on the second column I will have different types of values (booleans, String, color choosers, etc.), but I think that JTable allows only 1 type of data to be placed in a column.
I would like someone to tell me "JTable allows multiple data types on the same column" and show me how to do it, or tell me a different approach to the problem.
You can perfectly tell a JTable to have a column that contains Object, this way you will be able to put whatever ou want in.
BUT.
You'll then have to implement a very good TableCellRenderer/TableCellEditor pair in order to display whatever the cell contains.
Another option would be to use a Grid or GridBag layout inside of a JScrollPane, then dynamically populate the cells of the grid with different editors depending on the data type of the property.
If you can use external libraries, the JGoodies FormLayout is really suited to create such dialogs. Just take a look at the screenshots in their demo.
There is also a rather good PDF available containing with some examples and explanations.
I want to restrict the spinner to go from 0 to 59.
How can I do this?
Use the same general process I described in my answer to your similar question about combo boxes. This will be a little more straight forward if your numbers are fixed:
Edit the model property for the spinner in the properties editor.
Select Number for the Mode type
Edit the fields as necessary
Enjoy!
Give your JSpinner a SpinnerNumberModel and construct the SpinnerNumberModel with parameters that fulfill your criteria. e.g.,
// if the initial value will be 30, then this will set the spinner to
// initialize at 30, have a range from 0 to 59, and a step size of 1
SpinnerNumberModel spinnerNumberModel = new SpinnerNumberModel(30, 0, 60, 1);
spinner.setModel(spinnerNumberModel);
Also, on a side note, I strongly urge you to learn to code Swing without the use of NetBeans-generated code as this will help you immensely in understanding what Swing is doing under the hood and how to best code in Swing with or without NetBeans generation.
A note. Net beans will not let you edit this code yourself. However if you go to the model and choose number you can specify the values that you want.