Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query about creating JFrames and best practice

 
Daniel Dalton
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • 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?
     
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander
    Pie
    Posts: 12014
    220
    C++ Firefox Browser IntelliJ IDE Java Mac Oracle
    • Mark post as helpful
    • send pies
    • 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
    • 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
    Marshal Commander
    Pie
    Posts: 12014
    220
    C++ Firefox Browser IntelliJ IDE Java Mac Oracle
    • Mark post as helpful
    • send pies
    • 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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic