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.
Related
Just as the title suggests, I built a Java application that displays the results of students. Everything is working fine, however I was only able to display a set number of rows at a time applying some conditions, by using the setMxRows() method of the PreparedStatement object.
. However, I wish to load all the results sequentially and anytime the Jscrollpane is visible, meaning the results have overflown the available space on the JTable, I would get the id of the last row that activated the Jscrollpane and then reload the results where id is less than this particular id. Please how do I achieve this or is there a better way to get around this. I appreciate your answers in advance
I have a program that I created for work. This program takes an uploaded file, reads it, and puts the data into a JList in the GUI. The GUI actually has two lists and the user is able to move items between the left and right list by highlighting them like usual with a JList and then hitting an arrow to move the items. The lists are multiple-interval selection.
One small addition I would like to add is some type of counter that shows the user how many items they have selected before they actually move them between lists. This would need to be dynamic so if the user holds control down and begins clicking the counter will continue to update the number of highlighted items.
As the lists are often quite large and a user might need to move an odd number of transactions between the lists (Think 300 transactions in left list and the user needs to move exactly 50) it would be beneficial to have this counter.
Can anyone think of how this could be done? I'm not sure how to add an action listener to just clicking on the items. Please also let me know if I need to elaborate any more.
Generally my question is can I create an action listener just for when a user clicks a item in a JList that updates a counter for the current selected indices? Also it would need to change when they no longer have selected an indice.
Register a ListSelectionListener with your JList.
The listener could simply query how many rows are selected and update the number in the panel to that. Perhaps use getSelectedValues().size().
http://docs.oracle.com/javase/8/docs/api/javax/swing/JList.html#addListSelectionListener-javax.swing.event.ListSelectionListener-
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 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 got stuck with a performance problem while writing my program and I need your help! :)
I'm using a JTable to view test results taken from a vector I made and it has 4 columns in it. When I click on a row the details from a saved txt file of that test are shown in a child window. Also, when I click on the columns header the event sends the vector to a function that sorts it according to the pressed column. Every time a new value needs to be entered the sorting function is called again.
My program works fine with a small number of rows. But, when I enter say, 150 rows, every time I enter a new row the Table flicks (the sort probably takes a lot of time), but I have to keep the vector synchronized with the jable because of the "push to view the result" option.
I would really appreciate some help with this.
thanks
You shouldn't have to do any sorting yourself. JTable supports sorting natively, and has the convertRowIndexToModel and convertRowIndexToView methods to go from the view index to the model index and vice-versa.
See http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting.
Use JTable's internal sorter (DefaultRowSorter). Do not re-create the vector which holds the data - use Vector's add() method to add new records. In many years of Java GUI development I haven't seen a single case where I had to keep records in the TableModel sorted. Make sure getColumnClass() returns a proper type, so the default sorter knows how to sort the column, and that is all.