Generate JTextField Input Guesses - java

I have a Swing Application with a JTextField that the user is Supposed to Input ID Numbers. The ID Numbers are Stored in a database. During Inquiry from the DB the End user is again required to Input the ID Number so as to Query the DB. I need suggestions as to what to do so that when the user Inputs the First Digits of the ID Number, Guesses appear below the JtextField for the User to Choose from. Is this Possible with Swing and what Is the Best way to Implement it?

http://www.orbital-computer.de/JComboBox/ Maybe this helps. I know I shouldn't post just a link. But I cannot write everything explained there again here.

Try AutoCompleteDecorator in swingx. Check this post.

still not sure,
you can to use AutoCompleted JComboBox / JTextField (AFAIK ther no issues with Document, Focus, Caret and Selection/HightLighter)
(and to combine with a.m. point) to use filtering in JTable (with one Column and/or without JTableHeader) placed in the undecorated JDialog or JWindow(undecorated by default) in case that you want to display popup window with long list of sentences in the scrollabel contents, that could be clickable
use Swing Timer (5 -10 seconds for autohide for popup window)
share (use the same) model for JTable and AutoCompleted JComboBox / JTextField based on Vector or ArrayList,
to check focus lifecycle (nothing better aroung) for popup window as Java Calendar by Kai Toedter (download codesource)

You should write a PickList class which must contain a SQL Query select * from dbtableName where empId %getUserInput()%;

Related

Working with JDateChooser and JTextField

I need a help. I am doing a reservation system and my doubt is:
I have 2 JDateChoosers and JTextFields and one JDateChooser is for the start and one is for the end.
For example the user selects a date from the start JDateChooser and after he entered the number of years in the JTextField automatically the end JDateChooser changes the year. The user can't edit the end JDateChooser.
Select the Date from JDateChooser "start".
Enter the number of years in the JTextField.
Automatically the "end" JDateChooser's value changes.
Look into the mediator design pattern.
Basically you will have one object (the mediatory) which will be responsible for coordinating the state between a number of GUI components. So, when one person enters in something in one of the text boxes, that mediator is a listener on the text box, and will set the other GUI component's state accordingly.

2 column jcombobox

I had quite a lot use for a multicolumn jcombobox, but have not yet found any nor managed to make my own. I have tried several approaches found in web, but they have not worked. Afterwards I read somewhere that those (old) does not work under current Java version.
I have managed to make my own so far, that the combobox has a table as dropdown list and I can select an item with mouse, but the goal is that when the user starts to type into the edit box, drop down list opens and cursor moves based on the text the user has written. It seems that the events from e.g. JTextField editor = (JTextField) comboBox.getEditor().getEditorComponent() does not work.
Has anybody managed to make a two column combobox or have any ideas, how I could get the event when the user starts to type.
You are looking for autocomplete functionality (as I understand the question): it's supported in SwingX - and quite easy to use.
It boils down to implemneting a custom ObjectToStringConverter and configure the comboBox with an autoCompleteDecorator using that converter. Something like:
/**
* A converter which expects an item of an array type and returns
* a string representation of its first value.
*/
public static class ArrayToStringConverter extends ObjectToStringConverter {
#Override
public String getPreferredStringForItem(Object item) {
if (!(item instanceof Object[])) return DEFAULT_IMPLEMENTATION.getPreferredStringForItem(item);
Object[] array = (Object[]) item;
return DEFAULT_IMPLEMENTATION.getPreferredStringForItem(array[0]);
}
}
// usage
// assuming an model with items being arrays
JComboBox combo = new JComboBox(arrayModel);
// the renderer supporting multiple columns, f.i. a table
combo.setCellRenderer(new TabularListRenderer());
AutoCompleteDecorator.decorate(combo, new ArrayToStringConverter());
A complete working example (including the renderer and showing how-to force the width of the popup to be larger than the combo itself) is TableAsListRenderer in my incubator section
BTW: the autocomplete functionality is a standalone module, accessible via maven or manually downloadable from the maven rep at java.net, you would want the swingx-autocomplete-1.6.4.jar (plus the corresponding doc/sources, if interested)
Has anybody managed to make a two column combobox or have any ideas, how I could get the event when the user starts to type.
you can to put JTable to the JComboBox, but by default you can to select only value from entire JTables row, not directly from JTables Cell (required additional workaround, not tried yet)
i still searching for this answer too
here's i try so far..
i create Jpopup and put the Jtable in there..
then i use jlabel not jcombobox,
when user click jlabel, then the popup(Jtable) will show in that jlabel location..
when user select value on jtable,
then the popup will dispose,
then the jlabel will show the result..
for your case, you can use jtextfield not jlabel
EDIT :
heres related question
enter link description here

How to use TextChanged event?

I have a text box in which the user enters a unique number. After he enters a number and press tab or enter. I want to write query to search record matching to that unique number. The number is an integer. It will be passport no, pan card no or etc. If it is available in database, I want to display all information of that user.
I don't want to do that on button click.
I was looking for some TextChanged event but I found that the alternative in Java is the methods in document listener. But I am not getting how to use that. Also in Netbeans in the design view it does not show me any event on text changed. How to use Documentlistener to my JFrame/how to fix this?
Here you can find a tutorial on DocumentListeners and example code as well.

Getting a Swing to read input without a submit button

This is for an assignment so responses should not contain the code written for me.
I have written a program that is essentially an auto-complete program. It takes a word and returns the best matches.
I am trying to write a front end for it in swing(which I have no experience in) and want my front end to do the following: I want the input box to constantly be reading for user input, feeding that value to the other program, and returning the matches immediately in a drop down box, as, say, Google does. I can't seem to find any information on how to do this, all the intro tutorials use a submit button.
Can anyone explain to me how this would be done, or point me to a resource that could explain it? Again, please don't write the code for me, I don't want to unwittingly cheat on my assignment.
If you are using a JTextField, you could register a document listener on it.
If your input box is a JTextField, you can add a DocumentListener (this is a good tutorial) to capture character entries.
I think that no one from answerers ..., I'm only about Don't reinvent the wheel
1) use JTable with one (or two if is about Dictionary) Column and with basic implmentation for Sorting and Filtering (example with filtering from JTextField is in the Tutorial), JTable could be most complex from JComponents and there is everything (quite easilly) possible
2) use AutoComplete JComboBox / JTextField
3) use SwingX Decorator with JXList or JXTable
4) if you needed redirect output to the separate window then use JDialog / JWindow for popup window
One approach could be:
Attach a handler to detect a key press on the text box.
Grab the text from the box, and construct a "lookup" event which is runnable and submit this to some form of service which will dispatch it at some point in the future (hint: ExecutorService, Future)
Save this handle, and if the key press event happens again, cancel the previous and submit a new one.
When the event executes in the future and returns the result, popup a panel which displays the list of items.

jTextField autocomplete from database

I want a jTextField that shows suggestions in a popup as the user types (like google suggests). I want suggestions data to be fetched from a database table. I have been looking at SwingX and GlazedLists but I think they provide jComboBox autocomplete, and jTextField does not show a popup in these libraries.
I want to monitor user input and re-query the database after specific intervals.
Thanks.
I would keep looking into SwingX or GlazedLists, to avoid re-inventing the wheel. But if you are doing it yourself:
Add a KeyListener to the field and show a popup just below the text field whenever the user types. The popup could just be a menu with possible items or maybe even a JList. Make sure your database query can keep up with the typing or put the work on a separate thread.

Categories

Resources