• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Query about creating JFrames and best practice

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of my Frames contains two JPanels - one of the panels contains JButtons
and the other contains mainly JTextFields to enter stuff. When a button is
pressed in the buttonPanel, I need the application to do something with
the values in the other panel.

From the point of view of best practice, would I be better to:
  • create the two JPanels as inner classes of the JFrame, or
  • create the two Jpanels as top-level classes.

  • The first option always seems to lead to a huge long source file which seems to contain far too much code.
    The second option seems a far better OO choice, but so far I've either had to expose the inner workings of one class to the other, or create a third class to handle messaging between the two via a PropertyChangeSupport object - which greatly increases complexity.

    At the moment, I'm tending to lean back towards the first option because the two panels need to know about each other anyway.

    Any opinions?
     
    author and jackaroo
    Posts: 12200
    280
    Mac IntelliJ IDE Firefox Browser Oracle C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Daniel,

    Given that the two panels appear to be tightly interconnected, I would probably create the two JPanels as inner classes of the JFrame.

    Regards, Andrew
     
    Daniel Dalton
    Ranch Hand
    Posts: 146
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Andrew - I'd pretty much come back round to doing it that way. I was just curious to see what other people thought about it.

    Despite reading the O'Reilly Swing book and Suns Core Java amongst others, I've not actually found solid recommendations for best practices on how to go about constructing GUI objects. For example, when to subclass a JPanel vs when to just add objects such as JButtons etc straight to an instance of one.
    From what I can see, it's a design choice which way you do it - which is fine, as long as you can justify your choice I guess!

    Regards, Daniel
     
    Andrew Monkhouse
    author and jackaroo
    Posts: 12200
    280
    Mac IntelliJ IDE Firefox Browser Oracle C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Daniel

    For example, when to subclass a JPanel vs when to just add objects such as JButtons etc straight to an instance of one.

    Well this is a difficult issue, but sometimes it is easiest to go back to OO fundamentals: the old "IS A" versus "HAS A" debate. Are you creating a new form of panel that will be used elsewhere? Before you answer that - remember that if you subclass panel (so you are saying that your new class IS A panel) then the user of your panel can call any of the standard panel methods - which even gives them the ability to add extra components.

    Regards, Andrew
     
    reply
      Bookmark Topic Watch Topic
    • New Topic