Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using two GUI from one source  RSS feed

 
Jack Kenshin
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on an existing project and am wanted to simplify and make usable it. I have two GUI that I want they to feed from just one resource else their some GUI codes. I mean there are gui1.java and gui2.java consist of their GUI codes. And, one for their common parts. Let's call it as common.java. With gui selection part I can satisfy which gui selected (gui1 or gui2). I think to extend common.java by JFrame then extend gui1.java and gui2.java with common.java. Moreover, If there is external part of one of that GUIs, I can add external part using if condition (as I said I can determine which gui selected.)
The problem is that I want the common GUI parts to put into common.java to remove code duplication because of having two GUI same codes too much. Of course because common.java is super class it cannot be aware of elements of its sub class. (JLabels, JButtons, etc. can't be recognized) Even if their thread parts are same, I can't feed them from same source. The GUIs have been created using NetBeans. By the way, my solution for the problem is adding parameter all methods but what about for the method how many arguments are there? (Maybe I use vargs) however, I wonder whether there are more efficient solution.
 
Paul Clapham
Sheriff
Posts: 22482
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jack Kenshin wrote:I think to extend common.java by JFrame then extend gui1.java and gui2.java with common.java. Moreover, If there is external part of one of that GUIs, I can add external part using if condition (as I said I can determine which gui selected.)


So you're planning like this?



There are several reasons why that's a bad idea. First of all extending JFrame is already a bad idea for a GUI application. Such an application should simply build a JFrame and display it. The rule here is "Prefer composition to inheritance".

And second, it appears that you want Gui1 and Gui2 to extend Common simply because there is common code between the two, and you hope to reuse it. But reusing code is a bad reason for inheritance, and it appears you already realize that because your last paragraph tries to explain away all of the workarounds you would have to do. Again, prefer composition to inheritance.

Not only should your Common class extend JFrame, it shouldn't even contain a JFrame. Each of the GUI classes should contain (not extend) a JFrame, and they should use code from the Common classes as required. Since I don't know anything about what's common in your design, I can't say more than that. But if it includes GUI components, probably the Common class should have a factory method which produces those GUI components.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and welcome to the Ranch
 
Stephan van Hulst
Saloon Keeper
Posts: 7806
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch Jack!

I agree with everything Paul said. Please give us more details about why you need two different GUI classes and what's common between them. We may give you some more specific advice.
 
Jack Kenshin
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Welcome to CodeRanch Jack!

I agree with everything Paul said. Please give us more details about why you need two different GUI classes and what's common between them. We may give you some more specific advice.


Campbell Ritchie wrote:...and welcome to the Ranch


I thought up to today on that program but really and really it isn't worth the trouble. Because it has been cluttered up with threads and written very sporadically. So, I used that code to begin Swing but I think to keep going JavaFx. I'm glad to meet the experienced good people like you.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jack Kenshin wrote:. . . it has been cluttered up with threads . . .
That is a formula for disaster. All GUI programming shou‍ld be done on the GUI thread, or a worker thread, as described in the Java™ Tutorials.
If you know FX already, it may not be worth going back to the older Swing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!