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

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.

Related

InteliJ IDEA switch equals parts

Is there an option to do the following in InteliJ IDEA automatically? Or some macro or something?
From:
a.equals(b)
To
b.equals(a)
There are times when there is a NPE in the first operand and so it breaks the flow. Like when comparing some string from an object to an enum value. Clean code says it should be like enum.equals(value) and I want to do this at the press of keyboard shortcut.
If you put your cursor anywhere within a.equals(b) (even if it's after the closing parenthesis) and then press Alt-Enter, then you'll get a menu which includes "Flip '.equals'", which does exactly what you want.
An alternative way to get to that action is to press Ctrl-Shift-A to open the action search menu and enter any part of the name such as "flip".

How to make NetBeans automatically format indenting when pasting into a loop/if statement?

When I write my code in NetBeans, there are times when I want to add/remove if statements and loops. However, every time I want to add/remove a statement but keep the content, I have to fix all the indenting for the contained code. Likewise, every time I try to paste code into a statement, the indenting is all messed up and I have to fix it.
Is there any way to highlight the code and fix indentation?
I'm aware that Eclipse has an option to fix all formatting upon saving. I'm not looking for something that changes formatting upon saving, rather only something I can click on the moment I want the formatting fixed. I put an example of what I'm talking about below.
if (condition == true){
//pasted code
//pasted code
//pasted code
}
or
//other code
//removed if statement
//original code that is spaced too far right
//original code that is spaced too far right
//removed }
//other code
Try to paste using Ctrl + Shift + V.
If it worked, look at the netbeans options to configure the shortcut you want.
To change shortcuts go to : Tools > Options > Keymap
Here is a table of defaults shortcuts in netbeans : https://netbeans.org/project_downloads/usersguide/shortcuts-80.pdf
It might be useful, you can notice the "Ctrl-Shift-V Paste formatted" shortcut.

how to set a specific position though command line?

String cmd = "start calc.exe";
Process process = Runtime.getRuntime().exec(codeString);
I can call calculator out, but I wish to specify a accurate position like (200,300).
how can I rewrite my cmd String?
I know that java.awt.window can set a window or frame to the specific position.
Is there any method I can use to fill frame or window with my process?
There is no clean pure java solution because JDK does not provide API that can control non-java windows. So, if you want to can use JNI/JNA.
But I can suggest you a patch that will typically work.
Windows OS allows moving windows using keyboard. Try the following manually:
Win+R
type calc and press enter
press alt+space
press M
press enter
now use arrows to move the window. Press ESC to exit this mode.
All these actions can be implemented using java.awt.Robot.
So, you can run calculator and then immediately move its window where you want.
Well, this is not clear solution, but very simple one.
Expected Problems:
Alt+space is mapped to other, custom application
Other window that started together with calc overlaps it.
User will see that window is created somewhere and then quickly moved.
So, everything depends on how important all this for you. This solution is good as an exercise or demo but bad for real commercial application.

Using Intellij to select block between parentheses () or brackets [] or curly brackets {} either with keyboard or mouse

I've started working with IntelliJ and I really like it, but there are a few features which I miss compared with Eclipse. One of which is selecting blocks between {}, (), or [] or jumping between the opening/closing of a block. For example, in eclipse if you double click just after an opening parentheses it will select everything up to the matching closing parentheses as in:
method(item1, method2(itemA), item3,
item4, item5);
Where if you double clicked after the opening parentheses method(|, then it would select everything up to the closing parentheses, right after item5. I have discovered that IntelliJ will select method bodies when you double click, but not regions inside of parentheses and not for class bodies.
Also, in eclipse, you can jump between the end and beginning of a block by pressing Ctrl+Shift+P just after the opening/closing of the block. In IntelliJ (using eclipse key mapping), Ctrl+Shift+P simply selects everything up to the method's closing curly brace '}'. I've discovered that Ctrl+Shift+} works the way that I expect but only for curly braces {} and it also selects everything between the block be it a method or class rather than just moving the cursor.
I can tell that IntelliJ is fairly sophisticated and customizable, but I can't figure out how to duplicate this feature from Eclipse. Any assistance would be appreciated in getting this functionality to work.
Thanks in advance!
Craig
One of which is selecting blocks between {}, (), or [] or jumping between the opening/closing of a block.
Let's assume you have cursor on itemA.
Ctrl+W -> selected:
itemA
Ctrl+W -> selected:
method2(itemA)
Ctrl+W -> selected:
item1, method2(itemA), item3,
item4, item5
Ctrl+W -> selected:
method(item1, method2(itemA), item3,
item4, item5)
And so on. After that it would select whole method, class etc.
Honestly this is the shortcut which I use most frequently. Unfortunately I sometimes try to use that in other applications - each web browser closes tab by pressing Ctrl+W :)
Another thing is:
Ctrl + {
Ctrl + }
It jumps to opening / closing bracket
In Mac, go to start of a block and do:
Command + Shift + Alt + }
or go to end of a block and do:
Command + Shift + Alt + {
In Windows do it as:
Ctrl + Shift + }
and
Ctrl + Shift + {
If you're using the Mac OS X 10.5+ keymap, selecting the enclosing scope is option(alt) + up arrow. Pressing it repeatedly expands the selection by the next enclosing scope.
If you're using the Mac OS X keymap, this action is mapped to command+W.
I've been using Ctrl+W, but this is very annoying as it first selects a word, then an enclosing statement, then another enclosing statement and so on. You have to press Ctrl+W just too much times.
The best way I've figured for any block selection is:
Press Ctrl+{ - this will take you to the beginning of the block
Press Ctrl+Shift+} - this will select the whole block from the beginning to the end.
This way you're able to select a block of any size with any amount of nested blocks with few actions.
I am using a workaround which works pretty well. Just beside the code block you will see minimize/maximize button (which is used for code folding, the minus and the plus sign you see beside the line numbers). You will find it on minus, as the code is unfolded.
Just press on the minus, the whole code block will be minimized, and in one line. Choose that line, copy it, paste it, delete it or whatever you want to do with it :)

NetBeans IDE Code completion shortcut

I have two problems.
First when i type code, netbeans automatically completes the brackets. Now what i need is to move cursor out of brackets to type the semicolon. I have to do it with the right arrow key, but i have seen tutorials in which coders to it faster. Is there some other short key or a way to do it quicker i.e from the keys under my hands?
Second the Netbeans suggestion are quite slow in my machine. Is there a way to make them load faster?
Also, CTRL-; adds a semicolon at the end of the current line, without moving the cursor. The shortcut can be changed: Menu Tools -> Options -> Keymap and search for "Complete line".
There is also in the keymap an option for "Complete line and create new line", which does the same but then puts the cursor at the start of a newly created line below. By default, I had no mapping to it, but I added it as CTRL-SHIFT-;.
Type the closed backet. Netbeans realizes what's going on and does exactly what you describe.

Categories