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.
Related
I am creating a new label which I want to position precisely 130px next to an existing one. I am not familiar with layouts and would rather absolutely position my labels. My code is as follows:
playerLabels[index] = new javax.swing.JLabel();
playerLabels[index].setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/image001".png")));
playerLabels[index].setBounds(playerLabels[index - 1].getX() + 130, playerLabels[index - 1].getY(), playerLabels[index - 1].getWidth(), playerLabels[index - 1].getHeight());
panel.add(playerLabels[index]);
playerLabels[index].setVisible(true);
I am not getting any errors, but even though my labels are created. They do not appear at the desired position.
Does anybody know what I can do?
I agree that it's usually better to use a LayoutManager. That said, here's an article Oracle posted that describes how to do this:
Doing Without a Layout Manager (Absolute Positioning)
The code that you have posted will not depict the issue. Please give a sample code that can replicate your problem.
To address your problem, if you set the position of element manually (setBounds), you need to first set the container layout null and secondly you need to set the size of panel where you are adding those jlabel.
1.
this.setLayout(null);
2.
panel.setSize(900,900); // whatever the size you need.
I'm writing a Java Desktop utility in Java swing and have a minimal GUI part in it, most of work is done on server side i.e. backend. So, I don't want to spend a lot of time on GUI part, learning different controls and widgets. The problem is that Swing has two controls for (to me)same tasks i.e. dropdown menu and they are JComboBox and JSpinner I don't know the difference and I don't want any limitation that would hinder me from completing my task after I've selected one.
I've to use dropdown to display List<String> returned from DataBase and it can have as many as thousands of values. To keep user from scrolling I'll be taking starting alphabet as input or some category limitation will be there so, it may be that I'll be using specific values to be displayed from List<String>. i want my program to be as efficient as it can be and spend least time on front end as there are a lot of operations on backend.
Any Help will be highly appreciated
you issue talking about AutoComplete JComboBox / JTextField
with some effort could be aplied (AutoComplete JTextField) to JSpinner too
I've to use dropdown to display List returned from DataBase and it can have as many as thousands of values.
all above mentioned JComponents are based on premature array, maybe need to convert java.util.List to String[] or Vector (depends of your code logics)
none of GUI are designated to helt the thousands of values, have look at Paginations for Databases engine
above mentioned AutoComplete JComboBox / JTextField works without any issue up-to 2k rows on todays PCs
for searching or selections from largiest arrays you have look at Stepped JComboBox (about two or more JComboBoxes)
1.st for reduced selection from [0-9, A-Z]
2.nd for searching in records started with A (for example)
redirect Database events to the background tasks and to use SwingWorker or Runnable#Thread
The key difference lies in the model: a SpinnerModel implementation creates a sequence of values, while a ComboBoxModel does not. If objects in a SpinnerModel do not have a suitable natural order, you'll need to impose one.
As practical matter, "thousands of values" will benefit from an auxiliary approach, as suggested in #mKorbel's answer.
JComboBox suits your requirement. JComboBox is suitable for displaying a list of values. JSpinner is used when you want to do some functionality like incerement/decrement on the Spinner's text field.
This Oracle tutorial explains about JSpinner and its similarities to JComboBox. There is a demo app also.
I want to implement a date chooser using a JSlider. The user should be able to use the slider to freely choose between two previously known dates. I've seen examples like this one:
But I want to do the same, using only one slider. The minimal distance between two points (tick) should be one day. Any hints how to implement that?
If you want to have a slider with min = 1.1.2012 and max = 10.1.2012 just create a slider with min = 0 and max = number of days in between, then add the selected number to 1.1.2012.
I assume 10.1.2012 means January 10th, thus your slider would have min = 0 and max = 9. Then set the labels accordingly.
I can't to image how to do that with one JSlider, because there you'd have bunch of days, there are some workarounds for Double/RangeSlider, but I think better and easiest would be implements JSpinner with SpinnerDateModel, or best options is look for Custom Java Calendar or DatePicker
EDIT (#Robin)
First to answer your question: you can just use a JSlider, use the number of days between your start and end date to determine the range, and use custom labels (by using for example the setLabelTable method)
Now for user-friendliness, avoid this since
Nobody is familiar with this concept. Every site/application nowadays uses a textfield, most of the time in combination with a calendar widget. That is what users expect, not a slider
It will be hard to get all dates as labels on the slider due to the limited width. This means that a user have to interpolate / count to select his correct date
If you stick to the slider approach, at least consider to add a textfield as well. Even a non-editable text field which shows the currently selected date would be a huge improvement over a slider (see point 2)
Hey,.. i wanna show pictures with names onit in a jList, i know it get's also in a JPanel but i'm now using a jList, doesn't matter..
My question is why does the jlist don't fit the images only in 2 horizontal 'cells' and then go one row down?
sry my english is bad and i don't know how to describe it better, but look on the picture, why does the jlist dont set the e.g. 3rd picture right next to the 2nd?
JList.HORIZONTAL_WRAP works correctly in the ListDialog JWS demo, as described in Initializing a List. I suspect a layout problem, but you might compare your code to the examples found there.
If you use HORIZONTAL_WRAP you can adjust the number of columns with setVisibleRowCount. If you want them automatically fitted to the width of the list, use 0 or something negative.
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.