1

enter image description here

Here is the picture of my program. All these JMenuItems are in one JFrame and I added JPanels for each of them. When user click on one JMenuItem all JPanels will be invisible and only corresponding JPanel works.

Is it a good way? My class is huge. Before adding functionality is 5000 lines.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Bernard
  • 4,240
  • 18
  • 55
  • 88
  • 2
    A big GUI will be a few lines, but you have them all in one class? Next week you won't even understand how it works! – arynaq Jun 05 '13 at 12:04
  • @arynaq What is the alternative. When user click on JMenuItem invisible this frame and active the other one and when user close the other one I activate this one? – Bernard Jun 05 '13 at 12:23
  • 1
    I would recommend walking through [CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html), this exactly fits well in your application's scenario. – nIcE cOw Jun 05 '13 at 13:28
  • 1
    See also [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) for ideas to combine or display the panels. – Andrew Thompson Jun 06 '13 at 01:02

1 Answers1

5

Since you seem to have a fair number of JPanels, I don't think it would be a good idea to convert them to JFrames, since that would clutter user's dektop (who wants to run one application and all of the sudden find their desktop filled with 6 or more new windows ?).

So the "JPanel" choice seems logical.

Still, in case you want to give your users the option to customly arrange those panels (e.g. overlapping, side-by-side etc), then JDesktopPane might be a nice solution.
(Also, take a look at the relevant section of the Java Tutorials.)


Not directly related to your question, but having such huge classes (5000 lines without functionality...) is not considered a good coding practice (if not anything else for maintanability reasons).
From Oraclre's Code Conventions for the Java Programming Language, section 3 - File Organization:

Files longer than 2000 lines are cumbersome and should be avoided.

So, it might be a good idea to try and break it down into smaller, more reusable and maintanable pieces of code/classes.

gkalpak
  • 47,844
  • 8
  • 105
  • 118
  • Thanks for your reply, When you have a program like that which user click on each JMenuItam and there is a functionality. What would you do? one JPanel for each of them inside the JFrame or seprate JPanel class and add them to JFrame. How can I do that? – Bernard Jun 05 '13 at 12:20
  • If you are asking if the **code** for each JPanel should be in the same class/file as the main JFrame class, the answer is definitely NO. (If you are asking something else, then I misunderstood you :)) – gkalpak Jun 05 '13 at 12:34
  • Yes you were right and if you say no what is the alternative? – Bernard Jun 05 '13 at 12:35
  • 2
    One class for the main JFrame, one class for each JPanel (probably a generic JPanel class which all JPanels would extend). If your project/app is large enough (which it seems it is), you should also cosider separating the presentation (GUI) from the "business"-logic, so for each component (i.e. "JPanel") you could have separate classes dealing with different aspects according to the MVC paradigm.) – gkalpak Jun 05 '13 at 12:52