Difference in JComboBox and JSpinner
Asked Answered
F

3

8

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

Farflung answered 10/9, 2012 at 13:23 Comment(1)
From the JSpinner tag wiki: " Although combo boxes provide similar functionality, spinners are sometimes preferred because they don't require a drop down list that can obscure important data."Lavonna
D
4

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

Dennis answered 10/9, 2012 at 13:34 Comment(0)
D
4

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.

Diffusivity answered 10/9, 2012 at 16:28 Comment(0)
I
2

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.

Ingra answered 10/9, 2012 at 13:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.