Eclipse open braces on same line? - java

I recently found the AIDE IDE for Android, and love the fact that the editor puts it's braces on the same line when declaring and new code block, like so:
public void foo(){}
Instead of
public void foo(){
}
I think it looks a lot cleaner when you don't need to put much in the method. Is there anyway in Eclipse to do this? I've already looked at the braces settings, but it doesn't seem there's anything in here about this.

You can uncheck the "insert new line in empty method/class/block body" under "New Lines" which is three tabs to the right of "Braces."

Related

How to break {} naturally when I press Enter in Netbeans

I have a habit when writing code:
If I want to write a code block inside a {}, I will type {} first, then move the cursor into between { and }, press Enter and write my code.
In IntelliJ IDE, after I press Enter, IDE auto break and format for {} for me to write code (see this image with green arrow).
With Netbeans, it doesn't work like that (see the image with red screen).
My question is how to setting Netbeans IDE to work like IntelliJ to convenient for me to write code.
I have never written in NetBeans before. But I can understand you - it is so inconvenient and painful.
I have been going through all the combinations and haven't found the one needed.
The options that might repeat this behaviour somehow include:
Typing a { and pressing the enter. Code completion will automatically add the second brace and put you in the right place.
Using the combination cmd+enter and enter.
Then I figured out that we can write a macro:
split-line insert-break
The result:
Unfortunately, the macro is contextless. We can't write the condition "if the caret is between braces do our action otherwise, do the standard one". So, it can be assigned to another hotkey (not enter) to make enter work correctly in its cases.

Intellij Idea: One line if on two lines

So I am new to the Intellij Idea IDE and so far I like it a lot. Just having some trouble getting the code style settings exactly how I like them for Java. I am a fan of not using braces for one line if statements but I like for them to be on two lines:
if(true)
dosomething();
Right now when I run code formatting in Idea, it moves up into one line:
if(true) dosomething();
and I find that very hard to read, but I cannot seem to get the settings just right. Can anyone help me out and tell me what I need to set to keep it on two lines but still no braces? Feel pretty dumb not being able to find it. Thanks!
Deselect Simple blocks in one line in settings -> CodeStyle -> WrappingandBraces, then it should work. It worked for me.
Have you tried Control Statements on one line? Does it for me.
Well this might sound strange, but even I had the option simple blocks in one line unchecked. And I was facing same issue. But then if I left one empty line after the opening brace of method, IntelliJ didn't move entire code to one line.
For better understanding,
Problematic behavior
public void methodA(){
System.out.println("No blank line left");
}
Now when you close and open the file again, IntelliJ does this
public void methodA(){ System.out.println("No blank line left"); }
Correct Behavior
public void methodA(){
System.out.println("left a blank line above");
}
Now when you close and open the file again, it still stays like that
public void methodA(){
System.out.println("left a blank line above");
}
Just to make a note, IntelliJ version is Ultimate 14.1.4
For Webstorm - IntelliJ for Javascript, this option is in Preferences->Editor->Code Style -> Javascript-> Wrapping and Braces.

Eclipse content assist not working in some parts of code

I am using Eclipse Juno and have never had any problems with it, until its content assist stopped working only in some parts of my code. The code below shows what I am talking about:
mWTBatch.setText("Here content assist works");
medCopyBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
mWTBatch.setText(mRTBatch.getText());
mWTExp.setText(mRTExp.getText());
mWTName.setText(mRTName.getText());
mWTQuantity.setText(mRTQuantity.getText());
/* Here content assist is not working */
}
});
mWTBatch.setText("Here it is working again");
That piece of code is just inside one method of my class. The problem is that it works fine inside the methods of the class, but inside objects (new ActionListener(){}) it returns "No default proposals". It has this behavior when I either use "Ctrl+space" or type in "." after the object I want to get suggestions of. I've already searched here for the solution and googled it, but couldn't find a solution. What I have already tried:
Window->Preferences->Editor->Content Assist->Advanced and checked Java Proposals. It didn't work. I've even tried checking it myself, without just using Restore Defaults. I also tried the Java Proposals from the other table. Nothing. At last I tried checking all the fields with Java on them. Nothing worked.
Deleted my workspace folder completely, created a new one and imported my project. As my project is from an SVN repository, I used the project folder inside "trunk" folder, but I didn't copy the content of the folder to the workspace when importing (I need it to be in the repository). Still nothing, it continues with the error.
I reproduced the same situation of the code above into another class of another different project, which was also imported to the workspace, but having its content copied to it. The content assist worked for that one.
I then re-imported the project with the original code from above, but this time copying its content to workspace (which means it had nothing to do anymore with the SVN repository). It didn't work either.
I also checked if the ctrl+space is bind to the content assist on Eclipse settings and it is ok. I also checked if it had nothing to do with advanced key settings of Windows 7 language bar. I had seen on the internet that could be problem, but it was not my case.
I even created a new Java project on the workspace and copied the .java files one by one from the old project, set all the build path manually and in the end it still doesn't work.
For all the first four cases above I also cleaned the projects and closed and reopened them, not forgetting to refresh it after each action. Could anyone tell me how to fix this? I would like to find a solution to this problem, so that others may not be like me on it for 2 days. I think my question is important because I am gathering here many probable solutions found by googling the problem and from StackOverflow, and nothing solved it.
I stumbled into this issue the other day, and after seeing this post thought that I'd have to just live with it.
However, I did find a "hack" around it:
mWTBatch.setText("Here content assist works");
medCopyBtn.addActionListener( //<---------------------- Problem arises because we're inside a function declaration ...
new ActionListener()
{ // <--------------------------------------------- ... yet we're trying to write a function
public void actionPerformed(ActionEvent e)
{
/* Here content assist is not working */
}
}
);
mWTBatch.setText("Here it is working again");
From the content assistants point of view, this is just plain wrong, so we need to give it a little help:
mWTBatch.setText("Here content assist works");
medCopyBtn.addActionListener(
new ActionListener()
// <--------------------------------------------- CURLY BRACKET MISSING
public void actionPerformed(ActionEvent e)
{
/* Here content assist IS WORKING */
}
}
);
mWTBatch.setText("Here it is still working");
This will obviously give you an error upon compilation time, but it gives you full access to the content assist for the rest of the function declaration.
Also, it doesn't matter which curly bracket you remove from inside the function declaration, so long as it's an opening curly bracket.
One other point, if you only remove the opening curly bracket like I did above, then eclipse will NOT add in another closing curly bracket automatically for most cases (because by the time you enter the new opening curly bracket, you've equalised the opening vr closing of curly brackets). You can get around this by deleting the closing curly bracket as well, but then you have to remember to put two curly brackets back.
Hope that helps the 1165 views this question has garnered over the past year =)
Do a search for "Change display language" on the start menu. 'Change keyboards' > Look to see if you have any non English keyboards under 'General' > 'Installed services'. If you have more than just English then pressing Ctrl+Spacebar is changing your focus from Eclipse to the language selector on the taskbar. Remove any other keyboard languages from the list if that is your problem.

I cant print a simple JAVA code with BlueJ , works in CMD

I am new to java, and I can't seem to figure out how I can get this simple code to work properly. I understand the code, and I have it typed exactly how the book has it. The code compiles with no errors, but it just doesn't print anything. I think the problem has to do with the main function or something, because when I right click the class in BlueJ and click on void main it will compile correctly.
Anyways, here's the code.
public class Test
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
Am I doing something wrong in BlueJ, or is it supposed to be that way? Thanks!
When you're ready with the code, click on the Compile button.
If everything is fine, you will see a message on the bottom of the screen, saying Class compiled - no syntax errors. Then close the screen and go back to the project palette.
Right click on the class, and pick void main(String[] args). You will be prompted with a little screen, where you can provide some parameters. Don't provide any and just click OK.
If everything is fine, you will see the Terminal Window with the proper output.
There is nothing wrong with the code, I suggest you focus on the BlueJ documentation.
There is nothing wrong with the program. That's a regular "hello world" program.
There is nothing wrong with BlueJ. (Did you seriously think that BlueJ is incapable of running simple programs?)
There is most likely something wrong with the way you are using BlueJ ... but you've provided minimal information on that.
I suggest that you read the BlueJ documentation ... or follow this simple step-by-step example: http://www.cs.utexas.edu/~scottm/cs307/handouts/BlueJProjectInstructions.html
(It is also possible that you've not installed BlueJ correctly, but lets not start proposing wild theories without some evidence to support them ...)

Eclipse folding with java

Working with eclipse in a java collapsed file if i try to write a method between two collapsed methods when I write public and press space automatically the ide collapses the word public inside the method below.
State initial
- public methodA()
- public methodC()
Step 1:
- public methodA()
public
- public methodC()
Eclipse bug
- public methodA()
- public methodC() //Error because public is declared twice!
If I write the method faster the problem doesn't occur. Any form to avoid this weird behaviour without disable code folding?
Thanks in advance
What I do is to unfold the method before where I want to insert the code. Then it doesn't actually fold the new text into the previous (folded) method. But the simplest way is probably to just not fold the code and use the outline view to navigate around the file.
You really have only a few things you can do besides disabling folding. Even though the "public" you just typed gets folded into the following method it is still present in the text and the cursor is still just after the last letter. You can:
Continue typing and the public will become visible, as will the following method that was previously folded. This will through off the formatting, though.
Press Enter. This will move you to the next line and unfold the following method, but it will disentangle the code you're typing from that next method. You're on the next line after the "public" but the formatting stays nice. Just typing a backspace will get the cursor back into the right place and you can continue typing from there.

Categories