Blog

JList that can be selected by keyboard entry

The default behavior of a JList is that when you use the keyboard to press the first character of a list item, the selection will jump to the first item starting with that letter. Additional presses of the same letter will cycle through all the items starting with the same letter.

For a small list, this functionality works great. For a larger list with hundreds of items however, you may not want to press a single key thirty times to get to the item you are looking for.

So I did a little research about how to make the list work more like windows, where if you type quickly, it will navigate to the item in the list that starts with the prefix selected. So you could type the first few characters of the item to jump much closer to or exactly on the item you are trying to reach.

It turns out this is not difficult but it requires a bit of boilerplate code. My example code here is a modified version of some example code released under the GNU license.

One of the keys to making this work is you need to define how long between key-presses should the system interpret the new keypress as a starting a new search vs. adding additional characters to the original search. CHAR_DELTA in this example code defines how long the system will wait between keypresses before presuming you’re starting a new search, here we are saying you have to type at least one key per second (1000ms) to add additional letters to your search. m_key is needed to track what key sequence has been typed so far, and m_time tracks the time of the last key-press in order to identify whether the threshold of time has elapsed to make this a new search.

     //elsewhere: jList1 = new javax.swing.JList();

    private static int CHAR_DELTA = 1000; 
    private String m_key; 
    private long m_time;

    private void keyPressHandler(java.awt.event.KeyEvent evt) {
        char ch = evt.getKeyChar();

        //ignore searches for non alpha-numeric characters
        if (!Character.isLetterOrDigit(ch)) {
            return;
        }

        // reset string if too much time has elapsed
        if (m_time+CHAR_DELTA < System.currentTimeMillis()) {
            m_key = "";
        }

        m_time = System.currentTimeMillis();
        m_key += Character.toLowerCase(ch);

        // Iterate through items in the list until a matching prefix is found.
        // This technique is fine for small lists, however, doing a linear
        // search over a very large list with additional string manipulation
        // (eg: toLowerCase) within the tight loop would be quite slow.
        // In that case, pre-processing the case-conversions, and storing the
        // strings in a more search-efficient data structure such as a Trie
        // or a Ternary Search Tree would lead to much faster find.
        for (int i=0; i < jList1.getModel().getSize(); i++) {
            String str = ((String)jList1.getModel().getElementAt(i)).toLowerCase();
            if (str.startsWith(m_key)) {
                jList1.setSelectedIndex(i);     //change selected item in list
                jList1.ensureIndexIsVisible(i); //change listbox scroll-position
                break;
            }

        }
    }

Interview Puzzle Anecdote

Today at work E. was doing more interviews…and then sometime thereafter–I’m not sure exactly what prompted it–but shortly thereafter he bet F. he couldn’t solve this interview question correctly in 15 minutes. And by correct, I don’t mean, works for most cases, but doesn’t fall apart on certain rare edge cases. Unlike a lot of interview questions generally speaking, this one is actually quite relevant to the type of stuff I do at work all day, and conceptually, its pretty simple–kind of deceptively simple. So right after F. turns in his answer, someone else walks by and asks what we’re all talking about and pretty soon half of everyone sitting nearby is discussing the question “oh that’s easy” one person says hearing the problem, course, that person not having actually tried to write out the correct solution completely…It was kind of entertaining

Thoughts about Twitter

“What can be said in 140 characters is either trivial or abridged; in the first case it would be better not to say it at all, and in the second case it would be better to give it the space it deserves.”

http://www.techcrunch.com/2009/08/17/why-i-dont-use-twitter/

Work’s going good

Monday they rearranged the office. My new desk is, now that all the dust has settled, probably in a nicer location than my old one. Plus, I got just enough more space for an extra bookshelf for all those heavy programming books to go on.

I finished the assignment I was working on earlier this week (a bug where songs were being cut off by 5-10 seconds one out of every 3-5 times roughly)…and now I’m on to writing new code to re-skin the app…kind of fun.

Things I’ve learned about Interviews…

Always always have a copy of your resume in front of you during a phone interview. When they suddenly surprise you with a rather expected first question like “tell me about what you did at your last job” and you momentarily you blank in panic, you can stop and take a breath while you glance at your resume and remember the “better” words you carefully chose about how to articulate it.

Always have questions prepared to ask the interviewer. They will ALWAYS ask you if you have questions. And from everything I’ve read and heard, “no I don’t have questions” is never a good answer. You need questions to show them you are evaluating the fit of the job/company to your needs. I’ve found useful questions for initial/screening interviews are usually along the lines of “what would I be actually be doing at this job”. eg: Continue reading Things I’ve learned about Interviews…

River of News?

“BTW, this is also why the river of news approach doesn’t work for everyone. If you’re subscribed solely to feeds from popular blogs and web sites, a river of news is ideal – you get a flow of information that you can scan for stuff that looks interesting. If you miss a story, no big deal – it’ll float by later if it’s important. But if you’re also subscribed to feeds critical to your business, this approach is unacceptable. You don’t want a flow of uncategorized, unprioritized information that combines articles from Boing Boing with items from your company’s internal feeds. You need separation between the stuff you read for fun and the stuff you read because it’s critical to your job.”
http://nick.typepad.com/blog/2009/05/a-listers-are-late-to-the-stream.html

Sometimes its the Little Decisions…

Its funny sometimes to think back how some of the seemingly trivial decisions make a big difference in the path your life takes.

Like, one that stands out, for example, senior year of college, when I was at the spring career fair applying for summer jobs, and I have two versions of my resume, one tailored for defense contractors and one generic resume for other industries. There was that moment when I reached into my folder of resumes and decided to hand the recruiter my defense resume… That particular company actually had two divisions, one doing defense contracts and the other in the transportation industry. In fact, I was slightly intrigued by their transportation group from my prior experience doing an internship for another company in the transportation industry. But I was running low on generic resumes, and had a good number of booths left to visit. So I pulled out a copy of the defense resume. Spur of the moment decision. That innocuous choice ended up shaping the first four years of my career after I got the interview for that company’s defense division, and henceforth accepted the position I interviewed for. In the end, I was happy with where I’d ended up, but it’s funny to think how such a small decision could have caused me to end up somewhere completely different.

Job Seeking Advice

Assorted job seeking advice from SDForum JavaSig attendees:

  • At your experience level (5 years), you should start to “sound professional”, the more you can articulate yourself professionally, the more hire-able you will be.
  • Use job agents on major job boards to track statistics on major programming languages and libraries to identify which ones are most in demand. The libraries worth the time or money to learn would be the ones that are increasing in demand
  • Take a class on networking to gain practice and skill at how to do it…and remember its about relationships, not passing out business cards
  • Be able to confidently pitch what you’ve done…the companies are not looking to hire the team you were part of, but you specifically
  • In your elevator pitch, make sure you’re sharing the business results of what you’ve done, not just that you’re a programmer (what you do)
  • You can’t just sell that you can program, that’s kind of like saying you can type, kind of necessary, but not the selling point. The selling point is higher level, the other stages of the process…Architecting level. how did you make sure your code was bug-free? What goals were you programming for? (eg: performance, ease of programming, etc). How did what you did help the company make money, etc…
  • Be BOLD and be PREPARED (information and tips helps with the prepared part, but you still have to do the bold part yourself, things like contacting people and showing up at events like these and networking, etc)
  • Networking opportunities: SWE, TBP, SD Forum Women’s group, IEEE Women’s Group, Well-known relatives, alumni association, etc. SDForum tends to get a lot of the engineers with an Entrepreneurial spirit
  • Take advantage of UC Alumni resources–check out what your UC Alumni Association membership perks are that you can use at local UCs (Berkeley, SC, but esp. Berkeley)
  • Seriously, you need to know people inside the companies you want to work for, that’s how most people get hired
  • Establish credibility. Later on this might look like being a presenter at a forum like this or publishing books (what some of the others do to lend credibility to their consulting work), now could get involved in the steering committee for a group like this that finds presenters and coordinates the catering and stuff
  • Stuck in an unemployment rut? One way to gain specific relevant experience in the software industry is to volunteer for a startup (like an internship). Why? Two reasons: (1) Gain experience at a specific technology they work with (2) to get a reference. Most startups don’t have a lot of money so unpaid interns = something they’ll probably be willing to do.
  • Talk to former coworkers to get help with how to articulate the business results of what your previous project was doing
  • Been on board asinking ship? Be prepared to overcome the “cootie factor” (the project got cancelled/downsized/etc? it must have cooties)–be able to articulate why your project mattered or would have made a business impact

Laws that protected the weakest in society

“The laws outlined in Leviticus 19 and their equivalents in Deuteronomy have a whole range of community laws that are about protecting the weakest in society and restraining the most powerful.” -Ash Barker (Making Poverty Personal)

Verses about Trust

Incapable Trust: “When I consider that I am unable to trust, I have to also honestly consider what it is that I am trusting in.”

“Do not trust in a friend; do not put your confidence in a companion” -Micah 7:6
“He who is of a proud heart stirs up strife, But he who trusts in the Lord will be prospered. He who trusts in his own heart is a fool, But whoever walks wisely will be delivered.” -Proverbs 28:25-26

“Trust in the Lord with all your heart, And lean not on your own understanding; In all our ways acknowledge Him, And He shall direct your paths.” -Proverbs 3:5

“Trust in the Lord, and do good; Dwell in the land and feed on His faithfulness.” – Psalms 37:3

“Let us hold fast the confession of our hope without wavering for He who promised is faithful.” -Hebrews 10:23