Vanavah Edwards
asked on
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
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();
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();
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
two-dimensional array or something.
It is just more strighforward.
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);
ASKER
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.
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 ?
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 ?
ASKER
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.
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
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
ASKER
Right You know the solution. How can i do that?
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
ASKER
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.
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);
ASKER
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.
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);
ASKER
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.
Don't know what you mean by RIGHT TRIM.
Whay is there a zigzag ? The last item may have different length ?
ASKER
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.
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)lin e += " ";
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
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)lin
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
ASKER
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);
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
If nothing cannot be done about the zig zag it is not that bad, I will close now.
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?
Did you see a printout from
System.out.println(line);
operator?
How does it look like?
ASKER
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
COMBOBOX-ZIGZAG.pdf
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
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
ASKER
I tried
cbStockNo.setFont(Font.MON OSPACED);
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.
cbStockNo.setFont(Font.MON
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.
ASKER
cbStockno above is the JComboBox component declared as
cbStockNo= new JComboBox(v1);
cbStockNo= new JComboBox(v1);
Yes, I guess you need to use Cellrenderer to change font in combobox or something like that
So, yes, btetter leave it.
So, yes, btetter leave it.
if you see that they are in some way different, please, explain