简体   繁体   中英

Other ways to add a parameter to initComponents [java-netbeans]

I have a huge problem with my GUI java project in netbeans. It is well-known that the code compiled by netbeans is read-only and I need an other way to add a parameter to the initComponents method, besides calling a myInitComponents method, identical to initComponents, and calling it in the constructor. Now I have this:

public class MainFrame {

    public MainFrame() {
       DefaultStyledDocument doc = new DefaultStyledDocument();
       myInitComponents(doc);
    }

    myInitComponents (DefaultStyledDocument doc) {
       //components
       textModel = new javax.swing.JTextPane(doc);
       //components
    }

    initComponents () {
      //components
    }

In this way it works, but every time I change something within the frame, I have to copy and pase all the new code of initComponents inside myInitComponent. Moreover, this is a very awful way to do that. Is there any other way to add that parameter? Any help is appreciated!

You can add a parameter to the MainFrame constructor, place it in a field, use custom creation code in the properties table of the GUI builder.

There a couple of free code places to insert in the code of initComponents. Create custom code is such a place;

private final DefaultStyledDocument doc = new DefaultStyledDocument();

And in "custom creation code:"

new JTextPane(doc)

Which can also be used for custom panels etcetera.

I have a huge problem with my GUI java project in netbeans. It is well-known that the code compiled by netbeans is read-only and I need an other way to add a parameter to the initComponents method, besides calling a myInitComponents method, identical to initComponents, and calling it in the constructor

I think you are probably confused between using constructors for initializations and setters for accessing values.

This is as good as asking: if you have a class with attributes like a, b & c, how to create a setter which set all attributes. This is something you should avoid. You could just create an individual setter and getter for each property instead of trying to use an init to set all attributes.

You should be doing this:

class MyClass
{
    private int a;
    private int b;
    private int c;

    public MyClass(){
        init();
    }

    private void init(){
        a = 100;
        b = 200;
        c = 300;
    }

    public int getA(){return a;}  
    public int getB(){return b;}
    public int getC(){return c;}

    public void setA(int a){this.a = a;}
    public void setB(int b){this.a = b;}
    public void setC(int c){this.a = c;}
}

instead of this:

class MyClass
{
    private int a;
    private int b;
    private int c;

    public MyClass(){
        init();
    }

    private void init(){
        a = 100;
        b = 200;
        c = 300;
    }

    public void myInit(int a, int b, int c){
        this.a = a;
        this.b = b;
        this.c = c;
    }
}

this is a very awful way to do that. Is there any other way to add that parameter?

So you asked, if you have one more attribute, say int d . How should I add it to the parameter list of myInit() . So you already start to see the problem with this approach for your class design.

If possible, we try to achieve low coupling and high cohesion in our design. When you dump various unrelated attributes within a single method, you are steering towards low cohesion (a method which is not performing a very specific task).

If you try to use a single method like myInit() and use it as a setter to set multiple fields, it can cause a number of problems.

  • What if the user only wants to set a specific attribute, and not the rest?

So to answer your question, use individual setters for each attribute, unless the attributes are closely related for example:

setLocation(int x, int y);
setBounds(int x, int y, int width, int height);

At last I fixed this in a very simple way. I inserted all the code needed for the DefaultStyleDocument in the initComponents() method by clicking on customize code and adding it as pre-creation code.

public class MainFrame {

    public MainFrame() {

       myInitComponents();
    }

  //delete the myInitComponents() method

    initComponents () {
   //code useful for the DefaultStyledDocument..
    DefaultStyledDocument doc = new DefaultStyledDocument();
      //components
      textModel = new javax.swing.JTextPane(doc);
    }

Hope this could be useful to somebody.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM