O'Reilly logo

Learning Java, 4th Edition by Patrick Niemeyer, Daniel Leuck

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Eclipse Features

Eclipse has too many interesting features to really do them justice here, but we’ll mention a few to show you why people love this IDE.

Coding Shortcuts

Of course, you’ve noticed all the color coding and meta-information present when viewing Java source code in Eclipse. You can customize all of this behavior to suit your taste through the Eclipse Preferences Java Editor Syntax Coloring panel. Many other options can be set through the Eclipse Preferences Java Code Style and Editor panes.

What may not be immediately obvious is that editing is also enhanced. When typing, try pressing Ctrl-Space to attempt autocompletion or have Eclipse present you with relevant options. For example, instead of typing JButton, try typing JBu plus Ctrl-Space. Eclipse completes the name for you. If the portion of the name you type is ambiguous, a drop-down list appears, similar to that shown in Figure A-5.

Eclipse also provides abbreviations for common items. Try typing sysout and pressing Crtl-Space. Eclipse expands this to System.out.println() and places the cursor in the parentheses automatically. If you type a quotation mark, Eclipse closes the quotation for you. Note the green bar that it places after the closing quote. Pressing Tab takes you to that point so that you don’t have to use the arrow keys. Pressing Tab again takes you to the next green bar spot, which is the end of the line.

Using completion in Eclipse

Figure A-5. Using completion in Eclipse

Autocorrection

Eclipse can offer fixes for simple problems in your code when it detects them. To see suggested fixes, click on the red X next to a problem line. Eclipse presents a drop-down menu of possible fixes for the problem. Selecting an option shows you the code changes that Eclipse will make before you choose it.

For example, try changing the name of one of the JButtons in our Calculator to JBButton. Now, click the red X and a screen similar to Figure A-6 appears. Eclipse offers several possible corrections; the best one is to fix the misspelling and change it back to JButton. Of course, if we’d really meant to refer to a new kind of button, we could choose the option to create the new class right there and Eclipse would help us do that by creating a skeleton for us.

Autocorrection in Eclipse

Figure A-6. Autocorrection in Eclipse

Refactoring

Eclipse offers a number of tools under the collective title refactoring. These include the ability to rename and move members, automatically tracking down references to them and changing them throughout the project. More advanced options allow you to do things like create an interface for your class by copying all of its public methods or add a factory pattern to your code to encapsulate all object creation for a type. You can even encapsulate access to a variable, changing the code to use an accessor method instead. These tools can save you a lot of typing (or retyping, as the case may be).

As an example, let’s look at our Calculator again. Click on the addGB() method in the outline or select the method definition yourself. We use this method a lot, so let’s give it a better name. Select Refactor Rename and change the name to addToGridBag. If you want to see what it’s going to do in advance, press the preview button to get a diff view of the code. By selecting OK, Eclipse changes the name and all references to the method in your project (in this case that would include all of the source directories for all chapters). You can also refactor methods in order to change the method signature and add or remove arguments.

Diffing Files

Eclipse provides the ability to quickly diff two files. Select two files simultaneously in the Package Explorer. Right-click one of the files to display a context menu and select Compare With Each Other. Eclipse opens a dual-source view editor pane that shows the differences between the files graphically. You can even resolve conflicts by copying differences from one file to the other with the arrows at the top of the pane. For example, open ch15/webapp-xml/all-web.xml and ch15/webapp-xml/filters-web.xml and view the differences. You can also review changes made to a file using the Compare With Local History option that lets you compare the current state of the file with previous saves in the project. These are very useful tools.

Organizing Imports

Eclipse can tidy up the import statements in your source code. Selecting Source Organize Imports causes Eclipse to turn package imports into single class imports. Eclipse automatically determines exactly which classes from each package are used and breaks the package imports into individual imports. This makes the code a little more explicit, but some people prefer package imports when too many classes are used.

Formatting Source Code

Eclipse can autoformat your source code using the Source Format option. This applies standard Java conventions and indentation. The style is highly customizable; choose Window Preferences Java Code Style Formatter to see the available options.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required