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.
Related
Title itself sounds strange but i wanted to see if there's actual way of improving performances of "setText" method of regular Java JTextField.
I have app that's used for calculating some scientific data. Results of the calculation are being presented in Swing window that contains tabs with total of 1815 different JTextField controls. I do need JTextField because results should be editable.
I noticed that a lot of "processing results" time goes to internal workings of "setText" method. Looking at JTextComponent source code i assume it's because of underlying "AbstractDocument".
As i would like to improve speed of presenting calculated results to my user, is there a way to make JTextField perform faster (most notably it's "setText" method)?
Those fields are grouped into separate frames in order to be more visual appealing and distinguishable.
Seriously. A user will not be able to view 1815 components at a single time an observe there behaviour especially if the value of all of them are changing at once.
Maybe a single table can't be used for all the text fields, but I'm sure you can structure some of the data into tables.
I do need JTextField because results should be editable.
I suspect some processing is related to the text field generating events whenever the text is changed. For example DocumentEvents and UndoableEditEvents.
So, maybe use a JLabel to display the data and then to edit you double click and use a popup JTextField to enter the data into the label.
creating a custom JTextField that's not using AbstractDocument but plain String?
Instead of using a PlainDocument as the implemtation of the AbstractDocument you could create your own simpler Document implementation without the full overhead of the PlainDocument.
I have a java program which keeps records of databases. I have made use of JComboBox for adding data to my db system. I have to initialize more than 10000 string into my JComboBox. I have used keylistener to make my program auto search elements inside JComboBox.
The problem is that it is taking a lot of time to search a single key. Is there a programming technique to make search faster with keylistener for more than 10000 string elements in JComboBox. Should I have to make use of multithreading to keylistener?
Generally:
Never show so much elements at once into a List,Table,ComboBox etc. It makes the program lags and you spent a lot of memory.Maximum items to be shown per time must be <=300.Basically the comboBox its not so good idea a list or table will fit better.
How?
Every time the list shows 300 items,the user can use next button to load the next 300 from the database or previous button to load the 300 previous items.
About Search:
On every key pressed by user you search into database table you have for the 300 or less best fitting the result and then you add them into the List and ComboBox removing the previous items first.
More about search:
If you want you can retrieve all the items matching to search and use pagination for search results.
I have a large set of data from which the user has to select one. I'm thinking of a way to implement it (of course, in a GUI). I have a few ideas. But just thought of posting here as there may be better alternatives..
Say, user has to select a name from a large set of user base. If I simply put a text field for user to enter the name, then there can be issues like entering same name in different formats, misspelling etc...
I see two options here
Using a combo box
Using a list (Actually i'm thinking of something like a tool tip. As I cant show the whole list always due to space issues)
But combo box won't be much user friendly i guess. As the user will have to scroll around the whole list to select an entry. If the number of entries are too large, this will be
Which means, now I'm left only one option. A popping up list, which will change the content according the text user is entering in the text field. So he can type first few letters and the list will show all the entries starting from the entered text. Got my point, right?
Are there any other better to achieve this kind of need?
If I'm going to implement above, what will be the best way to follow. I'm thinking of extending the JTextField to add required functionality. Well, I'll put some method to set the popup list entries. And I'll add some actionListner to watch the text field, and control the popup list accordingly...
Autocomplete is what you are probably looking for. Google for "java swing jcombobox autocomplete" and limit results for the last couple of years to get relevant results. There will be a lot of examples and ideas on how to implement this with custom code.
I believe there is also some custom libraries like "swingx" that provide at least partial or full implementations to save time.
http://swingx.java.net/
They have released code as recently as the beginning of this years so it appears active and might have what you need.
You could take a look at SwingLab's autocomplete feature, it allows you to attach it to a JCombBox, JList or JTextComponent
use AutoComplete JComboBox/JTextField
based on Standard Java Classes
no issue with larger sets of data
no issue with Focus, BackSpace Key, Caret
for better performance to required sort the array before use
simple workaround for setStrict(true/false), restrict input to array
I've been trying to freshen up on my Java knowledge and I've been building a small GUI program and I've been running into a bit of a problem.
Basically, I have a JList which I'm currently populating with strings from an object from one of my classes which implement AbstractListModel which we can call my ItemList class. It contains an ArrayList of objects of the type Item which implements Serializable.
But, what I'd like to do is rather than populate my JList with a bunch of strings I'd like to populate it with some kind of string + JTextField combination so I can see one property of each Item object while also being able to update another property by changing the JTextField.
Now, what I'm looking for is the simplest possible way of doing this, and I am assuming there is a (relatively) simple way to do this since it's such a common thing to want to do in a GUI application (although I wouldn't put it past Java and Swing to make it convoluted and complicated).
So, what is the correct way of doing this?
No need to ever use String objects. Instead:
Put Item objects in the JList.
Add a ListCellRenderer to the list, to show the Item object the most user friendly way.
When the user selects an item, show the details in a different place (I'm thinking a panel with 2 columns of labels and text fields, and two rows - one for each attribute, and perhaps a button to Save)
The edit controls would best be encapsulated in a panel that can then hidden when not required, & put in a variety of places, e.g.
Below the list
In the main part of the GUI
displayed in a JOptionPane or a (modal or not) JDialog
Here is an example of placing the 'view/edit panel' (the file details) below the selection component (the table).
I've got a blocking problem with the sorting functionality of a JTable; this made stall the development of the spare time open source project for 4 months now. Hope to be pointed into the right direction here.
Context: I'm working on extending the functionality of the ps3mediaserver to add a media library with pms-mlx. The UI of the media server has been done using swing.
Problem: When clicking on a column header in the JTable, a seemingly random column gets sorted instead of the one having been clicked.
Current implementation: Here's the description of the different components and classes being used for the implementation:
ETable: As alternate row colours aren't supported by default in the JTable, I've switched to the ETable extending the JTable. Source comes from here
FileDisplayTable: This is the class creating the table. In the init() method, the sorting is being enabled with 'table.setAutoCreateRowSorter(true);'
FileDisplayTableCellRenderer: Exists to always align cell content on the left
FileDisplayTableColumnModel: Does some mapping between internal types and column names
FileDisplayTableAdapter: This class implements com.jgoodies.binding.adapter.AbstractTableAdapter to map the objects with the table columns.
Possible solutions:
Preferably, I'd like to keep the current implementation and figure out how to correct the sorting, but I doubt someone can help me out with that!? Additionally their are some bits of code I had to add because of strange behaviours; they're commented in the code
The alternate option would be to change the JTable for another control altogether. I've made some research but didn't find the solution I was hoping for. The constraints are that
it must be embeddable in a swing UI
preferably it should support data bindings
support alternate row colours
row sorting
At some point it will be possible to open an editing dialogue, where the content of the row has to be retrieved, can be edited and when saved the row has to be updated.
Before reworking the entire thing I'd like to be sure the component will be able to handle all I want to do with it.
I'm more used to create GUIs using .NET in Visual Studio. It's quite different and a lot more difficult to do the same with swing. Please show me I'm wrong :)
[edit] If someone is willing to reproduce the problem, either get the source or the binaries, launch the application, navigate to the media library tab. In the Genral section import some videos by adding some video files. Go to the library section, click on apply to refresh the list and try to sort the table.
It may be useful to know that JTable columns can be dragged by the user. As a result, the view (JTable or a subclass) and model (an implementation of TableModel) may have different column numbers. Similarly, a RowSorter may affect the order or number of rows in the view as compared to the model. The related conversion methods are mentioned in How to Use Tables: Sorting and Filtering. In particular: "When using a sorter, always remember to translate cell coordinates."
Addendum: As an alternative, consider org.netbeans.swing.etable.ETable or it's subclass org.netbeans.swing.outline.Outline, depicted here.