Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in JComboBox and JSpinner

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

like image 812
moCap Avatar asked Sep 10 '12 13:09

moCap


People also ask

Which method is used to add items to JComboBox?

Creates a JComboBox with a default data model. The default data model is an empty list of objects. Use addItem to add items.

What is a JComboBox?

JComboBox is a part of Java Swing package. JComboBox inherits JComponent class . JComboBox shows a popup menu that shows a list and the user can select a option from that specified list . JComboBox can be editable or read- only depending on the choice of the programmer .


2 Answers

  • 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

like image 114
mKorbel Avatar answered Sep 23 '22 15:09

mKorbel


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.

like image 35
trashgod Avatar answered Sep 19 '22 15:09

trashgod