Frage

As the question says how to make code template or short cut by selecting some code in java editor in netbeans and make hint appear to fast wrap with editor fold

War es hilfreich?

Lösung

netbeans options image

create with the below code reference

 // <editor-fold defaultstate="collapsed" desc="${comment}">
 ${selection}${cursor}// </editor-fold>

When you got large source files it can sometimes be helpful to create foldings for certain sections of your code. NetBeans got a nifty functionality for this using non-intrusive XML code (similar to Visual Studio).

To create a custom code folding section simply insert the following before the content to fold:

//<editor-fold defaultstate="collapsed" desc="My custom code folding">

and the following after the content to fold:

//</editor-fold>

So for example, if you had a JSF backing bean where you want to create a custom code folding for your properties, action listeners, and action handlers you could do as follows:

package backingbeans;

import javax.faces.model.*;
import javax.faces.event.*;

public class MyBackingBean {

    private String prop1 = "";

    public MyBackingBean() {
    }

    //<editor-fold defaultstate="collapsed" desc="Properties">
    public String getProp1() {
        return this.prop1;
    }

    public void setProp1(String prop1) {
        this.prop1 = prop1;
    }
   //</editor-fold>


   //<editor-fold defaultstate="collapsed" desc="Action listeners">
    public void myFirstListener(ActionEvent event) {
        ... do something ...
    }

   public void mySecondListener(ActionEvent event) {
        ... do something else ...
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Action handlers">
    public String myFirstActionHandler() {
        ... do something ...
        return "OUTCOME1";
    }

    public String mySecondActionHandler() {
        ... do something else ...
        return "OUTCOME2";
    }
    //</editor-fold>
}

This will create three custom folders that are collapsed by default when the file is opened.

If you like this, I’d suggest setting up a coding template for inserting the XML code as it can otherwise be tricky to remember. This is done by following these steps:

Click Tools -> Options

Select “Editor” from the top options

Select the “Code Templates” tab

Select “Java” in the Language dropdown

Click the “New” button

Enter the shorthand for inserting the template, for example I use ‘efold’

Click the “OK” button

The template has been created, now enter the following code into the Expanded Text textbox:

//<editor-fold defaultstate="collapsed" desc="${cursor}">

//</editor-fold>

Click the “OK” button.

You can try out the template by opening a Java source file, place the cursor where you want to insert the folding and type efold followed by the tab key. It will now have inserted the complete editor-fold and place the cursor ready for you to enter a description for the folding.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top