| | |
Searching a Text Field in a JList
Thread Solved |
OK, I have figured out what is going on ... I think ... it is not currCD or else it would not check each element and give the dialog box, that tells me it is moving through the list.
If I put the cdname in the cdnamefield also, it finds that cd and populates the fields as it should.
Why would I have to put it in both places? It seems to be searching the field, not the list ... which is what I think it is set to do, but not exactly what I want to do ....
does that rambling make any sense?
If I put the cdname in the cdnamefield also, it finds that cd and populates the fields as it should.
Why would I have to put it in both places? It seems to be searching the field, not the list ... which is what I think it is set to do, but not exactly what I want to do ....
does that rambling make any sense?
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
I am just flat out busted. I have done everything I can think of, and this is the closest I can get toworking correctly.
If I put the cd name in the search field AND the cdnamefield the search will find it and populate everything like it should.
I do not understand it. The only thing I can think of is that my search is looking in the text field itself, and when a match is made then pulling in that element.
here is the code
I have "tweaked" on the currCD and if lines above to my wits end ... i think therein the problem lies.
Please someone pull me from the depths of despair.
If I put the cd name in the search field AND the cdnamefield the search will find it and populate everything like it should.
I do not understand it. The only thing I can think of is that my search is looking in the text field itself, and when a match is made then pulling in that element.
here is the code
//start at first cd currCD = 0; // compare JTextField f = searchField; final String s = f.getText(); SwingUtilities.invokeLater( new Runnable() { public void run() { try { for(int i = 0; i < listModel.getSize(); i++) { CdwArtist cdEntry = (CdwArtist)listModel.elementAt(i); if(cdNameField.getText().equalsIgnoreCase(s)) { Inventorylist.setSelectedIndex(i); Inventorylist.scrollRectToVisible( Inventorylist.getCellBounds(i,i) ); CdwArtist newCD = (CdwArtist) listModel.get( currCD ); artistField.setText(newCD.getArtist()); cdNameField.setText(newCD.getName()); itemField.setText(String.valueOf(newCD.getItemno())); nstockField.setText(String.valueOf(newCD.getNstock())); priceField.setText(formatter.format(newCD.getPrice())); } else { JOptionPane.showMessageDialog(null,"No CD Match Found","TryAgain",JOptionPane.INFORMATION_MESSAGE); } } } catch(StringIndexOutOfBoundsException sie) {} } });
Please someone pull me from the depths of despair.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
1. Is there any reason for currCD = 0? Do you use it for something?
2.
What is
Just checking few lines bellow it is not!
2.
CdwArtist cdEntry = (CdwArtist)listModel.elementAt(i); will retrive data about CD on position "i" of your list. What is
cdNameField.getText() for? Is it the way you retrieve CD name from cdEntry object?Just checking few lines bellow it is not!
cdNameField.setText(newCD.getName()); Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
See comments in the code (prefixed Ez).
This should be a working version, but I just banged it out in a text editor real quick so any syntax errors are yours to enjoy. I also didn't bother changing a couple of things that would make it slightly more efficient (like holding on to the reference of the found CD instead of getting it again) because I didn't want to induce even more confusion over the mechanics.
This should be a working version, but I just banged it out in a text editor real quick so any syntax errors are yours to enjoy. I also didn't bother changing a couple of things that would make it slightly more efficient (like holding on to the reference of the found CD instead of getting it again) because I didn't want to induce even more confusion over the mechanics.
Java Syntax (Toggle Plain Text)
//start at first cd //currCD = 0; // Ez: removed, this has nothing to do with the search // compare //JTextField f = searchField; // Ez: also removed, superfluous final String searchString = searchField.getText(); SwingUtilities.invokeLater( new Runnable() { public void run() { boolean matchFound=false; // Ez: this is a simple boolean flag for whether match found for(int i = 0; i < listModel.getSize(); i++) { CdwArtist cdEntry = (CdwArtist)listModel.elementAt(i); if(cdEntry.getName().equalsIgnoreCase(searchString)) // Ez: Yes, you were comparing the cdname field against the search field { matchFound=true; currCD = i; break; } } // Ez: All you have to do now is check the match flag if (matchFound) { Inventorylist.setSelectedIndex(i); Inventorylist.scrollRectToVisible( Inventorylist.getCellBounds(i,i) ); CdwArtist newCD = (CdwArtist) listModel.get( currCD ); artistField.setText(newCD.getArtist()); cdNameField.setText(newCD.getName()); itemField.setText(String.valueOf(newCD.getItemno())); nstockField.setText(String.valueOf(newCD.getNstock())); priceField.setText(formatter.format(newCD.getPrice())); } else { // Ez: No match JOptionPane.showMessageDialog(null,"No CD Match Found","TryAgain",JOptionPane.INFORMATION_MESSAGE); } } });
wow. I thought I was so close and I couldn't have been farther off.
My boolean attempts were WAY screwed up!
Changing to cdEntry was something I had played with in one way or another several times, never with any success, but I could have stayed for 100 years the way I was going and would have never even come close on most of it.
You don't know how much I appreciate this. I am feeling a bit dence right now and will probably stay out of here the rest of the night just to go over things and try to improve my future performance. This is all so new, and I think I am just trying too much for not having any more experience than I do ... at the same time, I will never learn if I do not try this stuff.
If it is any consolation, I only have two more methods I am even going to attempt and then I am done until I get back from Mexico in mid August ... so you will done with me for at least that long! :o)
My boolean attempts were WAY screwed up!
Changing to cdEntry was something I had played with in one way or another several times, never with any success, but I could have stayed for 100 years the way I was going and would have never even come close on most of it.
You don't know how much I appreciate this. I am feeling a bit dence right now and will probably stay out of here the rest of the night just to go over things and try to improve my future performance. This is all so new, and I think I am just trying too much for not having any more experience than I do ... at the same time, I will never learn if I do not try this stuff.
If it is any consolation, I only have two more methods I am even going to attempt and then I am done until I get back from Mexico in mid August ... so you will done with me for at least that long! :o)
Last edited by no1zson; Aug 2nd, 2007 at 8:25 pm.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
![]() |
Other Threads in the Java Forum
- Previous Thread: Unicode & Java
- Next Thread: Version output
Views: 5777 | Replies: 34
| Thread Tools | Search this Thread |
Tag cloud for Java
access actionlistener add android applet arguments array arrays binary bluetooth build c++ chat class classes client code combobox compiler component constructor convert converter data database design detection eclipse error event exception file forloop game givemetehcodez graphics gui helpwithhomework homeworkassignment html ide image inheritance input integer j2me java javafx jframe jmf jpanel jtable julia lazy linked linked-list list loop looping main method methods mobile netbeans newbie number object os output pattern pixel printing problem program programming project read recursion remote remove return robot scanner server service set size sms sort sql string swing system test text text-file transfer translate tree web






