CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2009
    Location
    Israel
    Posts
    126

    Some clarification about Swing

    Hi all,
    I am new to Swing and GUI generally, and need some clarifications.
    I want to have a single GUI window which have several components (Still don't handle the semantics of GUI completely): Video player, log window, several buttons, etc.
    After a lot of googling, I understood that I need to create a JFrame which is the root of that window and than a JPanel to insert to that frame, and finally insert all the components to that panel and not to that frame.
    My question is why can't I insert components to a JFrame? Isn't it a little bit OH to have a panel in the middle?

    Thanks
    Guy

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Some clarification about Swing

    Actually in swing you can just insert your components into the JFrame because it comes with a default JPanel. However it is normal practice to add you components to your own panel and add that panel to the frame.

    A lot of people, when developing a GUI, have their GUI class extend JFrame. This is generally not the best approach, you should subclass JPanel instead. The minor disadvantage of extending JPanel is you need a write a little more code to create a window (eg JFrame, JDialog etc) to put your panel into but the advantage is you haven't tied your GUI to a JFrame. For example, if in the future you decide you could use this panel elsewhere but it needs to be in a JDialog then if you have sub-classed JPanel this isn't a problem. Or if you want to embed your panel in a window containing other panel again not a problem.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Jun 2009
    Location
    Israel
    Posts
    126

    Re: Some clarification about Swing

    Thanks,
    So what I have done so far is to have a main GUI object (Nothing to so with main function), that encapsulates a JFrame, and in to it I insert a JPanel that includes all my components.
    Actually this GUI object creates all components such as the player, panel, etc. and inserts all of them to the frame.

    Is it a good design?

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Some clarification about Swing

    It's hard to say without seeing the full design but it sounds reasonable.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Jun 2009
    Location
    Israel
    Posts
    126

    Re: Some clarification about Swing

    Thanks,
    My main issue was separate the main GUI Thread (The one that is invoked), and let it have already prepared contents.

    Lets hope it will work well

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured