0

So I'm working on a Texas Hold 'Em project in University and I've been thrown headfirst into another person's GUI code. I've never used Swing, so I'm having a hard time figuring things out. Hopefully someone can help me out or, at the very least, tell me where I'm going wrong.

We have several panels that make up our GUI, most of them added to one "master" JPanel. We're tracking some global things, such as the deck of cards, in this master panel, but there are buttons within the "subpanels" that do things like placing bets. The problem comes with how these things can interact.

When the button is pressed to place a bet, a new card has to be dealt to the table from the global deck. So, how do I refer back (refer "up" if that makes sense) to the method in the master panel that handles the dealing and advancing of the game state from within the ActionListener attached to the "Place Bet" button in a subpanel?

  • 2
    Basically what you're looking for is one or more "models" and associated "controllers" (lookup "model view controller" for more details). The idea is, when a button is activated/pressed, it generates an event. The "view" then passes information back to the controller (via an observer pattern), the controller is then responsible for updating the model which will then generate additional events, which other controllers/observers will be monitoring for – MadProgrammer Jun 19 '18 at 02:48
  • 1
    Forgive me if I haven't said this thing a thousand times before, but you might consider having a look at [Listener Placement Adhering to the Traditional (non-mediator) MVC Pattern](https://stackoverflow.com/questions/31602113/listener-placement-adhering-to-the-traditional-non-mediator-mvc-pattern/31604919#31604919) for more of an explanation. – MadProgrammer Jun 19 '18 at 02:51
  • You might also look at exploring some of the other [MVC related answers](https://stackoverflow.com/search?tab=votes&q=%5bswing%5d%20%5bjava%5d%20mvc%20is%3aanswer) for Swing – MadProgrammer Jun 19 '18 at 02:51
  • The basic point is, as much as possible, your could should be decoupled so that changing in one part won't adversely affect the other parts. You should also avoid perform "reverse lookups" trying to "find" objects in the component hierarchy, cause the will break in a bad way very quickly – MadProgrammer Jun 19 '18 at 02:52
  • That's all well and good, but the complexity seems waaaay outside the scope of what I can get done on this little project that I have to finish by tomorrow night in between studying for exams and going to class... – W. Harrison Jun 19 '18 at 03:05
  • 2
    Well, any solution is going to be suitable complex based on the hole you've gotten yourself thrown into, at least the one suggest will prevent any other possible issues – MadProgrammer Jun 19 '18 at 03:07
  • 1
    The scope of what you can get done in an assignment doesn't negate a useful architecture that's ubiquitously implemented to allow teams of programmers to reason about their projects. If anything, use this opportunity with your assignment team as a learning experience for exactly why such patterns and architectures exist – vapurrmaid Jun 19 '18 at 03:24

0 Answers0