IntelliJ, is it possible to “Make controller” as in NetBeans?

此生再无相见时 提交于 2020-06-13 18:49:28

问题


Does anyone with IntelliJ & NetBeans experience know, if IntelliJ has some similar feature as for NetBeans "Make controller" when right clicking the corresponding FXML file?

I think it is pretty important since it updates, if I have added something new (a FXID for example), and if I deleted it and such - and also places it in the correct place by itself.

I know you, in Scene Builder, can go to "View -> Show Sample Controller Skeleton", but this is just not enough automation, especially not from a Jetbrains IDE.

This also means that the following link is not an answer: how to do netbeans'"make controller" in intellij idea

[Menu in Netbeans, showing the "Make controller" feature]


回答1:


Rather than having an action to create the controller, IDEA uses intentions (activate via Alt+Enter or ). And its philosophy would be to keep the controller in sync as you edit the FXML file rather than recreating it to update it with changes.

For example, when you create an FXML file, IDEA offers to create the controller:

First I name it as desired, and then use an Intention to create the corresponding class:

Then as I add things to the FXML, IDEA offers to create the corresponding items in the controller:

Similarly with an fx:id:

And if I use the rename refactor to change the fx:id

it renames the corresponding field name in the Controller to match. So instead of editing it and then recreating the controller to capture the name change, it simply keeps them in sync. But again, the key is to use the rename refactoring and not just "manually" edit the fx:id.

Edit to answer follow-up questions in comments

First a disclaimer... I only dabble in JavaFX to write simple dialogs as front ends to command line scripts. So I'm by no means an expert and may not be aware of a JavaFX related feature in IDEA. But I have been using IDEA for 14 years and typically know what to look for in terms of features and "how do I" questions.

Can you shed some light on using this kind of stuff with Scene Builder, if you know something about it?

Whenever I use a WYSIYWG editor like Scene Builder, I still keep an eye on and cleanup the code it generates. I'm still a bit old school in that way. And I believe IntelliJ IDEA adheres to that philosophy.

I find it really hard to believe IDEA has no way of "mass automatic updating" edits made in Scene Builder

I can tell you as an IDE, IDEA is not big on Wizards or mass code generation. Instead it's more "surgical" in nature. It's "code by intentions" philosophy is to enter/edit your intent, and then use an intention/quick-fix to implement that intention. Thus any code generation it does is small and focused. But... in some cases you can ask it to simultaneously apply an intention to multiple items by opening the intentions side menu (using the right arrow key) and selecting the "Fix All 'issue name' problems in file". Again, given my limited experience with JavaFx work, I may not be aware of something. But I'm not seeing anything where such a thing would be and I'm not seeing anything in the Help pages.

<soapbox> When I first started using IDEA in 2003 I too wondered where all the wizards and mass code generation features that my old IDE provided were. (I previously used Forte for Java, the predecessor to NetBeans.) At first I missed them. Then I started using intentions and refactorings for everything. After a couple of months, I didn't miss the wizards. After a few more months, I came to hate wizards in other IDEs. In the long run, I was far more productive using the intentions & refactorings. And I noticed I was no longer spending a lot of time dealing with compile issues and bugs that I did when I used wizards. </soapbox>

If I assign fx:id's to a lot of buttons and such in Scene Builder, do I then have to manually go into the code view of the fxml file, find all of the fx:id's and for each of them alt+enter to get them into the controller?

Unfortunately for the "create field" intention for missing IDs" there is not a batch mode available. So you can not use the "fix all" option. My educated guess as to why not is that when creating the field you are given the opportunity to select the type for it. For example, with the fx:id for a label, I may want to type the field as Label, Labeled, Control, etc. Granted in most cases you are likely to type it as the actual type it is in your FXML. So you may want to open a feature request asking for a batch mode to be made available for the "Create field" intention that defaults the field to type used in the FXML. Nevertheless, if you get use to using F2 to navigate to the next error, and then using Alt+Enter (or ) to open the quick fix, you'll find you can add the missing fields very quickly.

if I deleted some fx:id's also, then I would have to remember which ones they were, to them manually delete them in the controller?

Again, instead of just "manually" deleting an fx:id, use an IDEA refactoring. When deleting fx:ids (or anything) use the "Safe Delete" refactoring. With your cursor on the id (either in the FXML file or the Controller), type Alt+Delete (or ) to launch "Safe Delete".

If there are no usages, the field is deleted. Otherwise you get a warning:

Click the "View Usages" button. The "Find" toolbar will open up showing the usages.

Update your code to remove their usages. Use the "Rerun Safe Delete" button to monitor your progress. Once all usages are done, hit the "Do Refactor" button to delete the usages. While this may seem more involved than some sort of regenerate the controller option, my question would be how would such a feature deal with the usage of that field in a method within the controller? Would it just not recreate the field leaving a compile error in my controller? How would it "regenerate" all the customization I've made to the controller?

The Safe Delete also keeps your code error free. If I had a dollar for every time I went to delete a something I absolutely knew was not being used anywhere only for the safe delete refactoring to pop up a warning about the place its being used that I totally forgot about.




回答2:


If you use Javaru's solution then created fields will be public and without @FXML annotations.

To change it go to: File | Settings | Editor | Code Style | Java | Code Generation | Default Visibility - choose Escalate or Private, and check "use external annotations" at the bottom.

Second solution: open fxml file in scene builder set the controller class in controller section at left bottom part of window. Go to: view | show sample controller skeleton and copy controller class code.

It is my first post, hope I've helped. :)




回答3:


I can't see create method or create field
I see this



来源:https://stackoverflow.com/questions/47224993/intellij-is-it-possible-to-make-controller-as-in-netbeans

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!