[JAVA programming] a little note on JList

Source: Internet
Author: User

This writing Java lesson of the big homework, first had to admire the Dictionary.txt file of the great, ah, a variety of size, hyphen, and various phrases of different circumstances in their own test when the winning, I should be happy ...

In fact, the request is not high, is probably a dictionary query software, provides a background dictionary, as long as the completion of the file I/O, string processing can be.

The following is actually a number of algorithmic problems, find the words, since the orderly (but from some point of view, '-' the value is smaller than A-Z, but the order in the dictionary is not so ah ~ ~ so I violently carried out a quicksort ...) Decisive O (Logn). However, in view of the abnormal requirements of the function of Lenovo, I would be counseling, can only slightly modify the next quicksort ah ... But Java string is also pretty powerful, comparetoignorecase, and what split is simply a welfare ah ~ ~ Also from this shot '-' the problem .....

On the problem of error correction, it is brazen to choose a Levenshtein Distance (editing distance) calculation Ah, but also control the accuracy, pleasure ...

The following gives the function of Levenshtein distance calculation ....

1     Private Final intdistance (string x, string y) {2         intm =x.length ();3         intn =y.length ();4         int[] T =New int[M + 1] [n + 1];5T[0][0] = 0;6          for(intj = 0; J < N; J + +) {7T[0][j + 1] = T[0][j] +ins (y, j);8         }9          for(inti = 0; I < m; i++) {TenT[i + 1][0] = t[i][0] +del (x, i); One              for(intj = 0; J < N; J + +) { AT[i + 1][j + 1] = min (T[i][j] + sub (x, I, Y, j), T[i][j + 1] +del (x, i), -T[i + 1][j] +ins (y, j)); -             } the         } -         returnT[m][n]; -     } -      +     Private intSub (String x,intXi, String y,intYi) { -           returnX.charat (xi) = = Y.charat (yi)? 0:1; +     } A     Private intINS (String x,intXI) { at         return1; -     } -     Private intDel (String x,intXI) { -           return1; -     } -     Private intMinintAintBintc) { in         returnMath.min (Math.min (A, B), c); -}

Having said so much, I'll talk about the subject, JList ...

Question 1:jlist's Listeners

First ate a listselectionlistener of the loss ah, the result because of this GUI in the other listeners conflict, then abandoned, instead of MouseListener and KeyListener (only up and down), This was solved ... Tear eyes ...

Question 2: Dynamic jlist

Because jlist to update in real-time, so the default jlist seems to do not, this time Google (the recent school dormitory can directly on AH ~ cool!!) ), with a defaultlistmodel, you are able to add and delete items in the JList ~ ~

Question 3:jlist on the effect

For the sake of beauty, I want to jlist on the selected items of the obvious, that is, bold, font size increase one number, but the jlist itself does not seem to set SelectedItem function alone, this time will be jlist in the project selected as JLabel sub-class, Instead of a simple string ...

The inside is still very complex ... The first thing to define is a class of item, to define what to put in, and to feel that string is possible ...

1 /** 2 * Mylistitem.java3  * */4  Public classMylistitem {5 String text;6     7      PublicMylistitem (String text) {8          This. text=text;9     }Ten      One      PublicString getString () { A         returntext; -     } -      the      Public voidsetString (String s) { -text=s; -     }  -}

This time you can use the Cellrenderer to achieve the format of the set ~ ~

1  Public classMycellrendererextendsJLabelImplementsListcellrenderer {2     3     Private Static FinalFont font1 =NewFont ("Palatino Linotype", font.plain,13);4     Private Static FinalFont Font2 =NewFont ("Palatino Linotype", font.bold+font.italic,14);5     Private Static FinalColor Color1 =NewColor (176,196,222);6     //for the JLabel default to is transparent, so setopaque true to set the background color7     //http://huangqiqing123.iteye.com/blog/16782088      PublicMycellrenderer () {9          This. Setopaque (true);Ten     } One      A      PublicComponent getlistcellrenderercomponent (JList list, - Object value, -             intIndex, the             BooleanisSelected, -             BooleanCellhasfocus) { -Mylistitem myitem=(Mylistitem) value; -              This. SetText (Myitem.getstring ()); +             if(isSelected) { -                  This. SetFont (Font2); +                  This. SetBackground (color1); A             } at             Else { -                  This. SetFont (font1); -                  This. SetBackground (color.white); -             } -                  -             return  This;  in           }     -}

OK ~ This time, this definition of our jlist is OK ~ so the choice of the item will have some special effect ~ ~ ~

1 Private New Jlist<mylistitem>(); 2 Private Final New Defaultlistmodel<mylistitem>3 jltwordlist.setcellrenderer (new Mycellrenderer ( ));

, it's good to see ~ ~

In fact, I have a small egg, ah, because to achieve if the user input a wrong word, as far as possible according to the word in the dictionary error correction ah, this time I added a doge, not to argue!! I don't know if I'm going to be killed.

[JAVA Programming] a little note about JList

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.