Home > NetBeans > Customize code formatting in NetBeans

Customize code formatting in NetBeans

Sometime recently, (perhaps 6.9?) NetBeans changed the default code formatting algorithms.  I prefer to write my if/else statements as follows:

if (cond1) {
else {
I found that, every time I put else on a new line by itself, the else was automatically unindented, along with the closed bracket.

Indentation problems

After unsuccessfully searching online, I eventually stumbled onto some settings that control this behavior.  By default, NetBeans wants to format your if/else statements as follows:

if (cond1) {
} else { // 'else' is on the same line as the closing brace

If you want to have a new line after your closing brace, as I do, you need to change some settings.

Go into Preferences -> Editor -> Formatting.  Change the language to Java, and the Category to alignment.

New line settings panel

Make sure the “else” checkbox is checked in the “new lines” section.  Customize the rest of it to suit your coding style.

Categories: NetBeans Tags: , ,
  1. Ravi Jain
    December 9, 2013 at 8:46 am

    Good article , helped me a lot.

  2. Sajjad Hossain
    August 13, 2014 at 9:48 pm

    Thanks for sharing. It helped me.

  3. Thomas Miller
    August 27, 2015 at 7:49 pm

    Did not find “Preferences” menu…

    • Nicholas Dunn
      August 28, 2015 at 6:50 am

      Maybe that’s just Mac. Try tools -> Options. It’s been awhile since I’ve used NetBeans and I’m not sure On Thu, Aug 27, 2015 at 7:49 PM Developmentality wrote:


  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: