1

I have made java application in netbeans. In that i have three different frames with a lot of things in it with code. i have to use similar frames in the program. So i want to copy them. but the problem is that if i copy it, the code doesn't gets copied and so i have to manually copy the code and as the labels and buttons names get changed it is tedious to replace them in the code. So i want a solution like, if i make many applications and then integrate them or if, if i can duplicate that in the same program. Note: i have to make tens of copies of the frames, so cant do it manually

I have a main frame, which loads on opening the program. Then opens a menue frame and after that MAIN FRAME, SEE FRAME, BLANK FRAME. All those in capital are to be duplicated. Each of them have a lot of things. for example: blank has 3 buttons, a textfield and a label; Main has a jpanel having 3 labels two buttons and one jpanel in itself which has 3 textfields and 3 labels. See is also like this. Added this on the request of a comment.

See this:

enter image description here

enter image description here

In the program [jFrame] opens first

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • What you want instead, is create instance of that class? Make them reutilizable. By the way what you are asking is just open netbeans mattise navigator and copy from there, then paste in new class and you have the same design. – nachokk Oct 11 '13 at 02:38
  • Could you give a simplified example that would clarify your problem. You could post the code of a simple frame with a couple of widgets then explain your difficulty in reusing it. – Tarik Oct 11 '13 at 02:39
  • @nachokk Hey this is a newbie; please explain.i am not able to understand you –  Oct 11 '13 at 02:41
  • Hey this is a 14 year old newbie. Explain in detail. –  Oct 11 '13 at 02:41
  • 4
    First, are you using swing? Also i don't get how this have +5 – nachokk Oct 11 '13 at 02:43

2 Answers2

2

It's sounds like you are trying to copy the controls from one frame to another, instead you should be copying the class file.

Right click the file in the Project tab and select Copy from the drop down list

enter image description here

Right click the package node you want to copy the class to and select Paste from the drown list

enter image description here

If you want to rename the file as you copy it, you can use Refactor Copy instead, otherwise you will need to rename the copy manually from the Project view

Caveats

I just want to point out this basically goes against the principles of OOP and you should seriously reconsider your design and approach to this probelm.

Instead, you should be starting with a "base" class, which provides the basic functionality, even perhaps making it abstract and extending from this class each time you need to extend it's functionality.

Secondly, you should have a read through The Use of Multiple JFrames: Good or Bad Practice?

Thirdly, JFrame is not the best choice to start with, something like JPanel would be more suitable base component, allowing to place the component onto what ever top level container you wish or place it within or with other components, making the component infinitely more flexible and reusable...

IMHO

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Ok this is a nice solution but the thing is what will open first? when the jar is executed and how to call frames from other java file ???? –  Oct 11 '13 at 02:49
  • @DakshShah That depends on how you configure your application. If you right click the project node and select 'Properties', select `Run` from the `Catagories` tree, you will see a `Main Class:` property. This will be the first thing run. As to how to call the other frames, this is as simple as making an instance of whatever object you want and making it visible. You may be better spent reading through [Creating a GUI with Swing](http://docs.oracle.com/javase/tutorial/uiswing/) then with Netbeans form editor... – MadProgrammer Oct 11 '13 at 02:54
  • I have already coded a lot, in it so i cant change the whole structure –  Oct 11 '13 at 02:56
  • it says my main class is freetts.FreeTTS i dont noe whats that, tell me one thing what if i want to change it to menu jframe? and one more thing, if i copy the java file, will i able to access one's contents from the other file??? –  Oct 11 '13 at 03:00
  • 1- Click the button `...` next to the `Main Class:` property field, select the class you want to act as the main entry point 2- Yes and no. Unless you have a reference to the instance of the other class, you won't be able to access it's details. – MadProgrammer Oct 11 '13 at 03:02
  • I tried what you said, see u want to access something like this: FormTTScopy.SEE.setVisible(true); this isn't a proper code, but i want something like this. –  Oct 11 '13 at 03:04
  • What you want and what you can do aren't always the same. What's `SEE`? – MadProgrammer Oct 11 '13 at 03:08
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/39031/discussion-between-daksh-shah-and-madprogrammer) –  Oct 11 '13 at 03:09
  • i did what you had told, so now i have a FormTTS1.java. Now there is a frame menu in FORMTTS1 as well as FormTTS. So if the main class is in formTTS, i can open it's menu by just menu.setvisible BUT how would i make the menu in FormTTS1 visible from a button in FormTTS –  Oct 11 '13 at 03:13
0

Here is what I would recommend:

  1. Decouple your application as much as possible so you can reuse some of your classes.
  2. Use Ivey or Maven to manage your dependencies this way you can write code once and then integrate it into multiple projects.

I have provided the link below for you :

grepit
  • 21,260
  • 6
  • 105
  • 81
  • i am not able to understand you. Please explain clearly what is all this and further the links you provided are so complex, i mean i cant understand a word you said. i am a 14 year old newbie –  Oct 11 '13 at 03:09