Eclipse folding with java - 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.

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.

Comment out methods using the Outline view in Eclipse

I was wondering if there exists some sort of plugin for Eclipse to add onto the Outline view that will comment out blocks of methods.
This would be useful for me during unit testing and I want to retest specific failing methods while leaving alone the ones I know will pass.
My current workaround is simply using
/*
unit test code
*/
You can select blocks then do CTRL+/ and Ctrl+Shift+/
Ctrl+/ comments all selected lines each with //
Ctrl+Shift+/ Comments out all selected lines with /* */
Doing it again uncomments
To select an entire method from within a method (may require multiple chains), press Alt + Shift + Up. To traverse methods, Ctrl + Shift + Up or Down.
Depending on where you are in the method, it will select the following things in ascending order (approximately).
parent object
individual statement or method call
full line
enclosing brackets
enclosing if/else, for, while loop structures
method declarations
class declarations
Then, using the comment shortcut provided in the other answer, you can quickly navigate and comment.

Any way to delay PaintComponent?

I've read through lots of the threads on paintComponent here, most of which making the point that it either is never or almost never necessary (or possible) to choose when paintComponent is called.
In my program, however, sometimes (only sometimes) paintComponent gets called before some of the objects it needs to paint have finished initializing or even sometimes before they've been created, triggering warnings- JOptionPane pop-ups, which surprisingly do not show any of the text they were hard-coded to display in their "message" area. I've read in other places that it's something to do with the EDT, and I've looked into some parts of that but I'm just getting confused. If the main purpose of the EDT is to update the gui, and by default pretty much everything will run in the EDT, then could I tell the program to run all the initialization and update functions in a different thread(s), which I somehow forcibly make run before the EDT runs?
What I'd ideally like to have happen is for paintComponent to wait until a certain point in my code to be run (after a bunch of update functions, regardless of what happens to the screen. After it does get called, it is followed by a pause in which relatively little is going on ( I had been using Thread.sleep() inside a while loop ) and which lasts until the user clicks something - at which point all the necessary functions are run again, followed by paintComponent afterwards, then the sleep() while loop, etc.
From what I understand, I think what I want isn't really possible, so my question is: Do you guys have any ideas of how to get around this?
EDIT:
So essentially the program is a college course planner, intended to make it easier for someone to plan out by semester all the courses they have to take before graduation, move those courses around (if possible), and see how all the courses are connected (prerequisites and such). When the program starts, it loads the list of necessary courses from a text file, then loads info about each course from a bunch of individual text files, and arranges them according to their prerequisites. Courses with no prerequisites go in the first semester, courses whose prerequisites have all been added to the first semester get added to the second, and so on until all the courses have been added. When paintComponent runs, it calls a function that assume all of each course's prerequisites exist on the schedule, and if it finds otherwise, it throws an error and displays a JOptionPane message box. When this happens during a normal run of the program (like if I manually add a course before adding its prerequisites), that all works and displays correctly. But sometimes that message box pops up when only some of the courses have been loaded (meaning control is still in the main constructor) and when it does so, the actual string message doesn't show up - only the actual pane, title and ok button do. Heres the line where I display the error box, so you can know that I'm not trying to display a string variable which has the potential of being empty.
JOptionPane.showMessageDialog(this,
"Course couldn't be loaded, partially >loaded\ncourses have been removed.",
"Error",
JOptionPane.OK_OPTION);
It is the "Course couldn't...been removed." part that doesn't get displayed. This is the only JOptionPane I display with the title "Error".
This post mentioned what sounds like the same thing happening, except I'm not using any of the things that poster had to fix. So possibly it's irrelevant but I'll add it just in case. JOptionPane.showMessageDialog() shows but without any message?
But to step back a bit, because that box popped up before all the courses had been added, it means that paintComponent was somehow called in the middle of the relevant JPanel's constructor, before a bunch of things had been initialized. I added a bunch of println() statements to make sure that that is true. Is it normal for that to happen, and if so, is there a way to fix it without simply using Andrew Thompson's advice?
After thinking though it a bit, I think that because the project is 3200 lines long and relies to a huge extent on text files, I'm really not sure how to ( or if I can) make a SSCCE for it..
If any specific pieces would be helpful I'll gladly add those but if this problem isn't clearly some standard issue I'm getting wrong, then I'll just add that flag and keep looking for bugs.
Thanks for your help
Declare a flag as a class attribute. Check it in the paint method. Change it at the end of the initialization.
class XandYandZ extends JComponent {
boolean initializationFinished = false;
public XandYandZ() {
// long initialisation..
initializationFinished = true;
}
public void paintComponent(Graphics g) {
if (!initializationFinished) return;
// .. paint ..

making comment a entire method at one time

sometimes it happens that we need to make comment a entire method in Java and reactive, more than once in same code in same time to test something. so is there any way in eclipse by which a method can be commented from its start or end, without going to the end and start for making comment?
First Drag Mouse and Select the Method then
Press Left(cntrl+shift+/) to comment
Press Left(cntrl+shift+\) to uncomment
Position on the method name and use Alt+Shift+Up twice to select the entire method. Then use Ctrl+/ to comment the selected text.

Is there any way to set breakpoints on all methods of a class?

Is there any way to set breakpoints on all methods of a given class?
I have a huge (2300 lines) legacy class and I need to set breakpoints on all method calls to understand how this mess works.
You can follow the steps below:
Run -> View breakpoints -> Add -> Java Method Breakpoints
Class pattern -> full reference of your class (e.g., mypackage.MyClass)
Method Name -> * (i.e., asterisk wild card)
I have discovered workaround :
1. I have set "Toggle Brakepoint" hotkey to Alt+Numpad 0.
2. After that you can click on first method
3. Use "Toggle Brakepoint"
4. Alt+Down - goto Next Method. ( Alt+Up - goto Previous Method. )
5. Repeat 3 step.
This is similar to Sergey Senkov's answer, but without hotkeys.
In the structure view, click on the first method. Repeat the following for each method:
Context Menu Key
M to toggle the method breakpoint.
Down
There is a plugin for idea:
Simple Toggle All Method Breakpoint.
It allows you to breakpoint all methods / clear all method's breakpoints in one click from context menu on class in Project view.
As Andrey Lavrukhin suggested, there is Simple Toggle All Method Breakpoint, install it through Settings -> Plugins. Works perfectly.
The only way you'll be able to do what you want is to set method breakpoints on each and every method with the class in question. You need to click on the left hand gutter next to the method - a little red circle with 4 dots will appear and you may get a warning saying method level breakpoints can impact performance. You can then further configure the breakpoint (by rightclicking on it, or select shift+F8) and set it so that it breaks on entry, exit or both
I'm afraid theres no way to do this in a single step/setting.

Categories