How to do a double or tribple column JComboBox to be use with autocompletion

I tried using arraylist and finally array to generate a triple column JComboBox with autocompletion and I could not get the data into the JComboBox.
Snippet ID=8243634 contains the code for the Array that I tried.
Snippet ID=8243635 contains the code for the Arraylist that i tried at first.
Which is best?  Should itbe done from arraylist, vectors or an array.  How can it be done and then use the data to populate a JComboBox
//				    ArrayList<ArrayList<String>> inventoryListSD = new ArrayList<ArrayList<String>>();
					Statement st = conn.createStatement();
			    	ResultSet rsData = st.executeQuery("SELECT STOCKNO, DESCRIP, BALANCE FROM CELL_STOCK");
			    	int numberOfRows = 0;
			    	while (rsData.next()) {
				    	numberOfRows++;
			    	}
			    	System.out.println("Total Rows = "+numberOfRows);
			    	int r = -1;
			    	inventorySD = new String [numberOfRows][3];
			    	rsData = st.executeQuery("SELECT STOCKNO, DESCRIP, BALANCE FROM CELL_STOCK");
				    while (rsData.next()) {
				    	r++;
				    	inventorySD[r][1] = rsData.getString("STOCKNO");
				    	inventorySD[r][2] = rsData.getString("DESCRIP");
			    	}
		    		System.out.println(inventorySD);
			        conn.close();

Open in new window

ArrayList<ArrayList<String>> inventoryListSD = new ArrayList<ArrayList<String>>();
			Statement st = conn.createStatement();
	    	ResultSet rsData = st.executeQuery("SELECT STOCKNO, DESCRIP FROM CELL_STOCK");
	    	int j =-1;
		    while (rsData.next()) {
		    	j++;
			    ArrayList<String> inventoryCOL = new ArrayList<String>();
		    	inventoryCOL.add(rsData.getString("STOCKNO"));
		    	inventoryCOL.add(rsData.getString("DESCRIP"));
		    	inventoryCOL.add(rsData.getString("BALANCE"));
		    	inventoryListSD.add(inventoryCOL);
		    	}
		    noOfItemsSD = new String [inventoryListSD.size()]; //Get count for array
		    System.out.println("noOfItemsSD - "+noOfItemsSD);
//		    System.out.println(inventoryListSD.get(11));
		    for(int i=1; i<=j; i++) {
			    inventorySD = toSinventoryListSD.toArray(j);
		    }
	        conn.close();

Open in new window

Vanavah EdwardsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
JComboBox has constructor which takes Vector;
So the easies is to use vector
0
for_yanCommented:
You can also use array of objects, burt as usual, arrays are less convenient than vectors as you have to know the size from the beginning and it is not so easy to change contents later
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
for_yanCommented:
As to the double or triple columns of JComboBox'es -I don't see differences between one or several comboboxes right away;
if you see that they are in some way different, please, explain
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

for_yanCommented:
If you have two or three JComboBoxes better use two or three vectors, rather than
two-dimensional array or something.
It is just more strighforward.
0
for_yanCommented:
Something like that should work:

Vector<String> v1 = new Vector<String>();
Vector<String> v2 = new Vector<String>();

rsData = st.executeQuery("SELECT STOCKNO, DESCRIP, BALANCE FROM CELL_STOCK");
				    while (rsData.next()) {
				    	r++;
				    	v1.addElement(rsData.getString("STOCKNO"));
				    	v2.addElement(rsData.getString("DESCRIP"));
			    	}
JComboBox cb1 = new JComboBox(v1);
JComboBox cb2 = new JComboBox(v2);

Open in new window

0
Vanavah EdwardsAuthor Commented:
I like your method.  But I wanted one JComboBox with difference columns so that when the user want to make a selection and startS to type say the description of the items the autocompletion would enable them to see the stock no at the same type because many sometime they are almost idential descriptions but difference stock nos.
0
for_yanCommented:
I'm not sure I understand fully, maybe you want to make one combobox and combine
both pieces into one string and add all of them so that when the user types sufficiently slong string it will offer the choice to them ?
0
Vanavah EdwardsAuthor Commented:
That sounds like it.  I want them to see both the stockno and the description all on one line as if was a table but with JCombox properties like drop down list with autocompletion.  
0
for_yanCommented:
then rather make one JComboBox and generate all combination and add them to one vector
Whenever the user types more and more of the text, it will narrow down the selection.
It will probably be even more convenient to the user then slecet from two comboboxes
0
Vanavah EdwardsAuthor Commented:
Right  You know the solution.  How can i do that?
0
for_yanCommented:

So, explain to me once again what you need - what are the options - you are selecting
stock name (or hwat is STOCKNO), stock description and balance (what is balance) and waht options
do you want to give to the users ?
Why did you have initial idea to have two or thre comboboxes?
Explain the general situation, we'll then think about how to do it better
0
Vanavah EdwardsAuthor Commented:
I am pulling into the vector you suggested from a stock table 3 fields namely STOCKNO, DESCRIP, and BALANCE.  I want to show these 3 fields in the JComboBox.  I saw it in a program on the net and thought it was a great idea because the user can see all 3 fields and and the autocomplete properties.  But I have an open mind.  If you can think of a better way please let me know.  You are the expert.
0
for_yanCommented:
But you cannot select them independently, so I'd suggest that you add them into one string, something like that:

Vector<String> v1 = new Vector<String>();


rsData = st.executeQuery("SELECT STOCKNO, DESCRIP, BALANCE FROM CELL_STOCK");
				    while (rsData.next()) {
				    	r++;
				    	v1.addElement(rsData.getString("STOCKNO") + ";  " + rsData.getString("DESCRIP")) + ";  " + rsData.getFloat("BALANCE") );
				    	
			    	}
JComboBox cb1 = new JComboBox(v1);

Open in new window

0
Vanavah EdwardsAuthor Commented:
That worked.  But because the STOCKNO field has varyiing length numbers, it is now zig zap and not even and fixed length spaced out.  How can I solve this zig zag effect.
0
for_yanCommented:
try this way - make sure that numbers 20 and 40 a for sure longer than the longest possible one column or two olumns respectively

Vector<String> v1 = new Vector<String>();


rsData = st.executeQuery("SELECT STOCKNO, DESCRIP, BALANCE FROM CELL_STOCK");
				    while (rsData.next()) {

				    	r++;
                                                                                String line = "";
                                                                                line += rsData.getString("STOCKNO");
                                                                               while(line.length() < 20)line +=" ";
                                                                                line +=  rsData.getString("DESCRIP");
                                                                                    while(line.length() < 40)line +=" ";
                                                                                      line +=  rsData.getFloat("BALANCE");

				    	v1.addElement(line);
				    	
			    	}
JComboBox cb1 = new JComboBox(v1);

Open in new window

0
Vanavah EdwardsAuthor Commented:
That work much, much better and the autocompletion is working fine.  I made sure about the longest items.   But there is still a little zig zag.  Can you RIGHT TRIM first while adding the spaces.  How is that done.
0
for_yanCommented:

Don't know what you mean by RIGHT TRIM.
Whay is there a zigzag ? The last item may have different length ?
0
Vanavah EdwardsAuthor Commented:
These items are not more than 10 characters.  Okay if you add on 20 spaces and the field only allow for say 10 spaces you will have 30 spaces and the item in combo box will be too wide.   Can you substring of lets say 10 or ? spaces in the second loop to ensure that it is even.
0
for_yanCommented:
Don't understan what you are saying.
If first filed is say 3-10 chras long.
then you add this filed and then
while(line.length() < 15)line += " ";
will make the lngth 15 for all cases.
if the second one is again 3-10, then you add the field you'll have max 25
and
while(line.length()<30)line += " ";
will make two items aways end with 30th symbol
then you just add the third item - and the last chrcacter posistion will be different vbecausof the length of the third item.

If this is not this way, paste and show me how it becomes different


0
Vanavah EdwardsAuthor Commented:
Attach is the code I added.  But It goes sometimes zig zag..  If an I items is 4 characters short, the diescription would go inward 4 characters and vice versa.
int r=0;
    while (rsData.next()) {
    	r++;
    	String line = "";
         line += rsData.getString("STOCKNO");
               while(line.length() < 15)line +=" ";
        		line +=  rsData.getString("DESCRIP");
			while(line.length() < 30)line +=" ";
	v1.addElement(line);

Open in new window

0
for_yanCommented:
If you have only two items, then the langth of the second item will be varaible, but the second column should strt at the same place,
and you don't need this line :
while(line.length() < 30)line +=" ";


Add printout like below, and post what it prints out
int r=0;
    while (rsData.next()) {
    	r++;
    	String line = "";
         line += rsData.getString("STOCKNO");
               while(line.length() < 15)line +=" ";
        		line +=  rsData.getString("DESCRIP");
			//while(line.length() < 30)line +=" ";
       System.out.println(line);
	v1.addElement(line);

Open in new window

0
Vanavah EdwardsAuthor Commented:
If nothing cannot be done about the zig zag it is not that bad,  I will close now.
0
for_yanCommented:
But it is still hard to understand what youi mean by zig-zag.
Did you see a printout from
    System.out.println(line);
operator?
How does it look like?
0
Vanavah EdwardsAuthor Commented:
When you do a System.out.,println(v1) it looks perfectly even between each column.  However, in the combobox it is different.  See attached images.  One is V! done with STOCKNO first and the second V2 is done with DESCRIP first.
COMBOBOX-ZIGZAG.pdf
0
for_yanCommented:
I see, this is now understandable.
This because in the output on printout the font is monospaced - the smae width for each character
and on combobox ellement it is proprotional - different characters have different widths.
We can try to play with fonts on combobox, but I'm not sure  we can find monospaced fonts
for combobox , and think it looks OK and it I'd think it is not worth it to play with combobox fonts
0
Vanavah EdwardsAuthor Commented:
I tried
cbStockNo.setFont(Font.MONOSPACED);
But i am getting an error -->>
The method setFont(Font) in the type JComponent is not applicable for the arguments (String)
So I will leave it if nothing cannot be done and only use the V! as that look near even.
0
Vanavah EdwardsAuthor Commented:
cbStockno above is the JComboBox component declared as

cbStockNo= new JComboBox(v1);
0
for_yanCommented:
Yes, I guess you need to use Cellrenderer to change font in combobox or something like that
So, yes, btetter leave it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.