0

I have a problem with Netbeans. It has a very nice GUI builder for java swing, but the generated code is not changeable. To solve this issue I tried to first make the GUI in a panel let's call it Asset class which has a JTabbedPane jTabbedPane1; .

Then I extends a new class from it, so i have all the GUI in the asset class and then i can change it, as i wish.

public class AssetHandler extends Asset{

    public AssetHandler(){

        System.out.println("Here is the asset");
        JFrame frame = new JFrame("FrameDemo");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(jTabbedPane1);
        frame.pack();
        frame.setVisible(true);
    }
}

But it complains in the with:

jTabbedPane1 has private access in Asset

Actually, the error does not make sense for me, because i inherited it.

So, how can i use manipulate the generated code ?

Sal-laS
  • 11,016
  • 25
  • 99
  • 169
  • 1
    Okay, here's what you're going to have to do. You're going to put the form editor down and walk away from it, yes, it's great tool, but until you understand how UI's a constructor and learnt some of the techniques employed in making complex, multi hierarchical code, it's going to make you lazy and cause you no end of issues. Instead, start with [How to create GUIs in Swing](https://docs.oracle.com/javase/tutorial/uiswing/) and start making UI's by hand, it will give you a greater appreciation of when a form editor is useful and how you can leverage it – MadProgrammer Mar 21 '17 at 21:27
  • Once you know what you're looking for, you can edit the relevant code generation dialog, for [example](http://stackoverflow.com/a/6236075/230513). See also this [approach](http://stackoverflow.com/a/2561540/230513). – trashgod Mar 21 '17 at 23:42

2 Answers2

1

Subclasses cannot directly access a private variable of the superclass. When you create the Asset class, either declare jTabbedPanel to be protected (instead of private), or add a getTabbedPanel() method to that class.

Vitalii Kotliarenko
  • 2,947
  • 18
  • 26
FredK
  • 4,094
  • 1
  • 9
  • 11
  • it is auto generated as `private` – Sal-laS Mar 21 '17 at 20:59
  • 1
    @Salman If you don't understand Fred's answer, then you have more things you need to learn before embarking on the complex world of graphic user interface programming. Fred is suggesting making a method in your `Asset` class which returns the value of `jTabbedPane1`, it won't effect the generated code – MadProgrammer Mar 21 '17 at 21:29
  • @MadProgrammer i understand his answer, and my answer was proper. Encapsulation of `jTabbedPane1` is automatically setted to `private`, i cannot change it. He siad change `private` to `protected` – Sal-laS Mar 21 '17 at 21:40
  • 1
    BUT you can write a method which returns it! `public JTabbedPane getTabbedPanel() { return jTabbedPane1; }` <- Add this to your `Asset` class! Without more context to how you code is setup, there is nothing more which can be suggested (other than don't use the form editor) – MadProgrammer Mar 21 '17 at 21:42
  • @MadProgrammer i respect your reputation. Thanks for advise, but i know the basic, but this Netbeans editor was a little challenging, so i just thought maybe i am in wrong track. I just thought, maybe netbeans team has set a very simple solution and i cannot find it.But, now i have resolved my issue, and i will make an answer. – Sal-laS Mar 21 '17 at 21:48
  • @Salman In my experience, the Netbeans team expect you to encapsulate the functionality to the class and not expose your UI elements. Netbeans allows you to change the access levels it applies to instance fields (exactly how I don't know of the top of my head) and reuse the base container like a normal class – MadProgrammer Mar 21 '17 at 22:33
1

I have found the answer.

It's impossible to change the Netbeans generated code, or at least i did not found it.

Instead, Netbeans provide you a GUI to add event, and then add the code for any kind of event you need in it.

In other word, when you are working Netbeans GUI builder, you have too divide your works into two different parts

1) Design : This part can be easily done by GUI builder

2)Add Event: (Action perform,Listenner, etc.) You can use the GUI builder to generate the event, and then you can write the code to handle the event. It is the only place, where you can add your own code.

In my opinion the Netbeans is much more better than Eclipse GUI builder plugin for SWING.

Sal-laS
  • 11,016
  • 25
  • 99
  • 169
  • Also worth noting is that you can construct some object for your GUI manually and then add them in: `swingPanel.add(myObject)`. Similarly you can cast GUI objects so that they can be accessed manually: `myButtons[0] = swingButton;` this is useful for iteration – JFreeman Jan 25 '19 at 05:55