Couldn’t Think Of a Clever Title

Software Ramblings

Posts Tagged ‘tip’

Java Is Pass By Value

Posted by castever on June 29, 2009

This is something that threw me off when I first encountered it; I should have read the tutorial.

“Primitive arguments, such as an int or a double, are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are gone and any changes to them are lost.”

“Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object’s fields can be changed in the method, if they have the proper access level.”

(reference: The Java Tutorial: Passing Information to a Method or Constructor)

Posted in Java | Tagged: , , | Leave a Comment »

Magic Poi

Posted by castever on May 6, 2009

I just discovered the Apache POI Project.  I needed to export data from a JTable into an Excel spreadsheet and I didn’t want the hassle of exporting as csv and getting warnings from Excel, so a little search gave me POI, and it is simple and easy to use.

Download it and read the documentation that comes with the download (it seems to be a little more updated than the website).  Plenty of examples.

Posted in Java, Software Engineering, tools | Tagged: , , , , | Leave a Comment »

Good Advice

Posted by castever on February 26, 2009

First, this advice didn’t come from me;  I just like it so much that I want everyone to know about it.

I recently began reading the Object Mentor Blog and I came across this post, and it has really stuck a chord with me.  So often I find myself trying to hurry up and get it done, and that usually produces less than good code, which means I have to go back and fix it later or someone else will have to clean up my mess, which is no fun and embarrassing to say the least.  If, however, I take the time to carefully plan out what it is I am creating, use Test Driven Development, and slow down, etc, then the code I have just made will be less likely to break.

We delude ourselves that speed is better.  Not always.

Here are a few things “Uncle Bob” mentions in his blog post to be a professional craftsman:

1) Adopt an attitude of calm

2) Focus on the problem to be solved.

3) Solve the problem step by step without rushing

4) “When you feel the temptation to rush, resist it. Leave the keyboard and walk around. Distract yourself with something else. Do not give in to the call of your addiction.”

I am going to start remembering and implementing these suggestions;  I think I’ll be a better software engineer for it.  Thanks Uncle Bob.

As an update to the above about speed killing, I just read a post by Martin Fowler about Technical Debt.  Sometimes it may be good to deliver fast, poorly designed code that will cause you problems later in order to make an investment in the project, hit deadlines, etc knowing you will have to go back and refactor.

Posted in Best Practices, Software Engineering | Tagged: , , , | 3 Comments »

KeyListener in JComboBox

Posted by castever on December 9, 2008

You’ve seen those auto-complete combo boxes out there. SwingX has one, and it is a good one, but not exactly what I needed.  I didn’t want all of the highlighting and I didn’t want the options in the combo box to be static.  All my suggestions are coming from the database.

So what I discovered and what I want to share is how to listen for key events on an editable jComboBox.   If you did something like this:


jComboBox1.addKeyListener(new KeyListener() { ... }

You would not be notified of KeyEvents.  Why?  you ask.  When you type into an editable JComboBox the key events are not being fired on the JComboBox but rather on the editor of that JComboBox.


jComboBox1.getEditor().getEditorComponent().
addKeyListener(new KeyListener() { ... });

Simple as that!

Posted in Java | Tagged: , , | 1 Comment »