Eclipse and curly braces - java

Is there a quick way to make Eclipse put curly brace on the next line (by itself) on a block of code?

Yes, edit your active profile (Java...Code Style...Formatter), and change the brace positions to the next line. Also, in Java..Editor..Typing, you can have it automatically insert your braces at the correct position.

For pre-written block of code, first do the settings as suggested by Don and then select that piece of code and right click Source Code->Format and the formatting would take place as per the settings done in the preferences.

Current versions of eclipse have a more convenient way of doing this.
Go to Preferences->Java->Code Style->Formatter
Click on edit, and on the new opened window, go to the Braces tab. Here you can choose how different types of blocks of code organize their curly braces.
Hope this helps any who google this, and find this post. (Like myself)
Regards to all!

The simplest and global way:
Go to Window -> Preferences.
Then in search put: "brace".
Select -> Java -> Code Style-> Formatter
After opening new window go to brace tab and change it as you prefer.

Coming from C# background I couldn't come to grips with built in Java formatting of Eclipse.
With some effort I managed to get most of what Visual Studio used to offer me. Here is an XML version of it. You can import the file..
Preferences->Java->Code Style->Formatter->Import->Apply
Additionally see arguments on which approach is better on our sister site..

In addition to the methods of changing the settings and ctrl-shift-f to apply, these settings can be done on a per project basis. Simply right-click on your project, choose properties, Java Code Style, and enable the checkbox for "enable project specific settings."
I find this invaluable for use between projects I work on to configure it to match a particular client style, along with other settings to try to match code formatting of existing code. This way, you won't upset other people but can still use your own style on other code bases. :)

Related

Why does Android Studio throw away my Code Style settings?

I am trying to write an Android app in Java. In Android Studio, I go to
File->Settings->Editor->Code Style->Java
Then for Project, I modify the curly braces (I can't read code with unaligned curly braces), click apply, and finally click OK.
But then I reopen the settings to find that my settings have been discarded and it has reverted my code style.
How do I prevent it from reformatting to a different style?
I'm not sure if I undestand properly your question but I think that you want to put "Braces" in the next line, isn't it?
You must edit at:
File->Settings->Editor->Code Style->Java
Then,
Select Project or Default -> Wrapping and Braces -> Braces Placement -> NextLine
Let me know if works?
For me, it is working fine:

Netbeans refuses to auto suggest known classes for imports

I just discovered that my NetBeans suddenly stops importing classes that are found in the Java API but can still import classes from my external libraries. Take a look at the image below:
As you can see, List and ArrayList are classes from the java.util package but the usual suggestions for importing them are not shown here, instead it is asking me to create class. I know that I can type import java.util.List; on the top of the code but life was better before! Please I need help thanks.
I had the same problem a couple minutes ago... Terrible. The same problem has been reported here:
Bug 235712
The solution which I've found there was sufficient - simply delete the Netbeans cache...
In order to do this, go to Help ==> About . Then go to the directory with cache and delete all you find in the "index" catalog.
After restarting the NB, everything was cool.
Today I came across the same problem. It happens that it actually is very easy to exclude a class from code completion if you click in the wrong place while working.
To solve it (netbeans 8.2) go to
Tools - Options -> Editor tab
Then select Code Completion Tab
In the drop Down select JAVA (in my case it was Java)
You will find a box bellow titled Package/Classes
In the EXCLUDE tab you probably will find the packages marked to be excluded from code completion. Just remove the ones you want back.
Check under Preferences->Editor->Code Completion to make sure auto complete is on, though by your screen shot I assume that it is.
Also try Preferences->Editor->Formatting and set your Language to Java
In my case, NetBeans was refusing to suggest Scanner. As it turned out, all I had to do was remove java.util.Scanner from Options » Editor » Code Completion » Packages/classes: » Exclude. Then the autocompletion for Scanner returned.

I can't figure out how to disable Netbean's code completion without disabling pop-up of suggested code

How do I disable code completion but keep suggestions enabled? Every time I type object.Method() without passing any parameters I get something similar to object.Method(datatype). Netbeans would complete the code after the first parenthesis, (. When I don't want to pass any parameters, I simply type ) once. I would get something like this thanks to auto complete: object.Method())
The null in between () would be replaced by the ) I typed.
I want to be able to see suggested code without ctrl-space and without having netbeans automatically insert the code for me. In netbeans I can disable the auto insertion of the second bracket } after I type the first {. I don't have an option to disable parentheses from auto inserting. The problem is that if I disable code completion, I also disable the automatic pop-up that gives me related code. Any advice?
If you do not want the IDE to perform the project updates, you can disable the build analyzer as follows:
Right-click the project node in the Projects window and select Properties.
In the Project Properties dialog box, click the Code Assistance category.
Deselect the Use Build Analyzer option.
https://netbeans.org/kb/docs/cnd/HowTos.html
It's been awhile since I used NetBeans (personally I prefer IntelliJ), but I believe you're using the wrong setting. Try this: under Tools>Options>Editor>General, uncheck "Insert Closing Bracket Automatically".
See also this other question: NetBeans curly braces auto-closing

Eclipse code formatter: how to remove its effects?

for a mistake i've made eclipse v. 3.4.2 formatting my sourcecode on a single class file.
That's really confusing, how can i restore my own original text format?
The simplest approach is probably to use Eclipse's Local History. On the context menu for the file, select "Restore from local history." If it's not in the local history, you may need to go back to the previous version in source control... (I assume you're using source control. If you're not, I strongly urge you to do so, right away.)
Of course, another option if you've still got the file open is just to press Ctrl-Z to undo actions - keep doing that until the formatting goes away :)

AutoIndent in Eclipse possible?

I have been wracking my brain trying to figure this out. For the first time I used jEdit the other day and I was pleasantly surprised that it auto indented my code (meaning that I'd put in the following code:
int method () {
_ //<-- and it put me here automatically
I've tried to get the same thing working with eclipse but with no success. I got into the code formatter but I don't see how to make that happen.
Is it possible to do this? Also while I'm here, is there a such thing as a eclipse plugin that will allow you to search the methods and classes of the standard java library?
Thanks
Personally all I use for this is the format options Window->preferences under Java->Code Style ->Formatter.
I once took the time to tweek how I like my code to look like when I work and exported the whole thing. After that I just code without too much bother on what it looks like. When I find the code looks messy by pressing the combination ctrl+shift+f and the whole class becomes pretty again, comments and all.
After a while it pretty much became a reflex...
code code code
ctrl-s, ctrl-b (cause I disable auto build sometimes), ctrl-shift-f
code some more etc...
Once I got used to this I never really cared how it presented the code as i was typing because I knew it would look all pretty as soon as the loop/if/switch/method etc is finished
My clean eclipse install does this by default.
Have you changed any options? Make sure the file you are editing has the .java file extension. The preference options that control the typing automations are under Java -> Editor -> Typing in the Window -> Preferences menu.
Also, I find that the auto-indenting, and most of the other auto-complete functions of eclipse do not function well if the file I am editing has errors in it which prevent compilation. Make sure that your curly-braces are matched correctly, this is the main one that I've noticed blocks auto-indent.
Regarding searching through the standard Java libraries, use the Search -> Java.. menu option, and check the JRE libraries checkbox, then search away. You can also use the Hierarchy view to see how the classes relate. Also, in the Package and Project views you can expand the JRE System Library, and then expand rt.jar which holds pretty much all the standard Java pacakges.
Eclipse has always done this for me by default.
One really cool thing about eclipse is that you can search preference pages. Just right click and go to prefrences. Go to the "Window" menu, and click "Prefrences". Then at the top of the tree view there's a text box that says "type filter text". Replace that with "indent" and it should bring up the page where the indent option is.
Make sure that eclipse recognizes your file as a java file, that you're using the Java distribution, the latest version, etc.
Iv been trying to work around the eclipse indenting and other supposed features for years, and it seems that the bottom line is this ...
It only works for the programming style of the authors, so to use it you need to modify your style to comply.
This would be OK except that the authors of eclipse have some very strange ideas about common shortcut keys.
One horrid example is the search features, eg when did Ctrl+K become "Find Next occurrence" and why doesnt F3 or n work?
That all being said I use eclipse because if you have the time to wait around while it starts up - or never close it - and you can modify everything youve learned about using an editor - why why why - then it will certainly increase your efficiency.
Please note that there is a preference setting for indenting, it can be set for a project, a workspace, or globally, but no matter how you set it eclipse will still chuck tab characters in where you dont want them.
In fact its indent crazy, like it wants to indent everything, even if its already indented.
Like I said Iv been using it for years and it STILL drives me nuts with its random behavior.
Follow these steps for Eclipse:
Select all text: ctrl+A
Correct indentation: ctrl+I
You should check:
Hidden features/tricks for Eclipse?
What is your favorite hot-key in Eclipse?

Categories