0

I am building one really big application. The situation is this: I have many tables which I can fill with data from my Java app.

I have groups and for each group there is button in my main JFrame (Menu) and a table in my database:

Example: Table A is storing information for group A

I have one button that is called "New" and it should add new group - so this means to create new table and also to add new button in MainFrame. This New Button is opening frame called NewGroupFrame for example. Is it possible to add button to my MainMenu frame from this NewGroupFrame?

Basically: Is it possible to add components from one frame to another?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • a component can only belong to one container. You can add it to another, but that will remove it from where it was if it was – ControlAltDel Mar 19 '19 at 14:52
  • i think you didn't understand my question. I want to create entire new button and to be added to the other frame. I don't want 2 frames to share 1 component. – Borislav Stefanov Mar 19 '19 at 14:54
  • 2
    `I want to create entire new button and to be added to the other frame.` ok, go ahead and do it. The component doesn't know it will be added to a different frame. All you need is the reference to the panel where you want to add the component. Also, you should not be using a second JFrame. If you need a child window, then you should be using a JDialog. An application will generally only ever have a single parent JFrame. – camickr Mar 19 '19 at 14:58
  • Exactly. How to get there? How the reference should look? Also your suggestion of JDialog got my attention. Would you give an example if it is possible. – Borislav Stefanov Mar 19 '19 at 14:59
  • 1
    Your question contains no code at all, which makes me take a guess: you are using the Netbeans GUI designer till this point and you don't know much at all about actually programming using the Swing API yet. Am I right? – Gimby Mar 19 '19 at 15:03
  • Absolutely correct – Borislav Stefanov Mar 19 '19 at 15:06
  • 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) The answers provide many alternatives. In this case, it seems a `JTabbedPane` with the content of the new tables as new tabs might be suitable. But there are many options, and you know the situation best. 2) *"How to get there?"* Tip: Add @camickr (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Mar 19 '19 at 15:24
  • @BorislavStefanov well... that will have to change. – Gimby Mar 19 '19 at 16:06
  • 1
    Thank you all. Not sure how to proceed after reading @AndrewThompson's link and the way I was thinking to proceed is bad. – Borislav Stefanov Mar 20 '19 at 06:02
  • "Not sure how to proceed" use the common strategy: break big problem into smaller ones. First you can add the functionality need functionality to the `JFrame` class. For example `public void addButton(String tableName)` method that allows dynamically adding a button. – c0der Mar 20 '19 at 09:56
  • @Gimby Would you give me more information why I should switch NetBeans GUI with Eclipse Swing for example? As per this: https://stackoverflow.com/questions/2717889/eclipse-or-netbeans-for-swing-based-application it is OK to use NetBeans, even more - it is preferable. – Borislav Stefanov Apr 01 '19 at 11:38

0 Answers0