8 Replies - 4918 Views - Last Post: 24 January 2012 - 10:06 AM Rate Topic: -----

#1 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Alternative to inner classes?

Posted 23 January 2012 - 03:23 PM

When building Swing GUIs that involve multiple JButtons with ActionListeners attached, I usually include inner classes in my Interface class (GUI-builder class) that hold the actionPerformed() methods. This is so the actionPerformed() methods have access to fields in the Interface class.

Generally with simple GUI applications much of their functionality relies on these inner classes and their methods, so I feel like they should be contained outside of my Interface class. My code often looks sloppy when I'm forced to keep both my GUI-building code and the functional code on one page. Of course not all of the functional code is inside the Interface class, but essentially every method is called from there, and you can't avoid doing certain conditional checks in the inner class methods.

I really don't see a way around this, so I'm wondering if it's normal to have functionality stem from the GUI class like this. In a perfect world, although it might make less sense, I would prefer to keep the GUI as separate from the functional code as possible.

Is This A Good Question/Topic? 0
  • +

Replies To: Alternative to inner classes?

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Alternative to inner classes?

Posted 23 January 2012 - 03:29 PM

You can link all the buttons to a single actionPerformed function which then would need an if statement to determine which button called it. To keep it clean I usually make the actionPerformed function only a set of if statements, then call a function to handle the event. This removes the overhead of your class initialization for each button.

Edit:
Then your class assigning the actions would pass in the this pointer instead of the new object.

This post has been edited by William_Wilson: 23 January 2012 - 03:30 PM

Was This Post Helpful? 1
  • +
  • -

#3 The Adrian  Icon User is offline

  • New D.I.C Head

Reputation: 16
  • View blog
  • Posts: 47
  • Joined: 09-January 12

Re: Alternative to inner classes?

Posted 23 January 2012 - 03:30 PM

You could create for example a ButtonActionListener that holds all the code for actionPerformed()

That way, you could create one object and hold that in your program and every time you create a new button, make it refer to the one that is held in your program.

Whenever dealing with building objects or event listeners like the one your talking about, I like to make systems or logic components that are dedicated to listening for events and I make factories for creating the objects. The buttons fire a message when they are clicked on, and the event listeners handle that event outside of the object.
Was This Post Helpful? 1
  • +
  • -

#4 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: Alternative to inner classes?

Posted 23 January 2012 - 03:51 PM

You can always use the parameter passed to the actionlistener, get the object that fired the event, cast it to your interface, and access the variables that way.

This post has been edited by ianian112: 23 January 2012 - 03:51 PM

Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Alternative to inner classes?

Posted 23 January 2012 - 06:25 PM

If your code is that long is that you way need to separate your GUI in different classes like NorthPanel, WestPanel, EastPanel, SouthPanel
All of them can have their JButton and their actionPerformed()
The constructors of these JPanel may receive as parameter the JComponent they will affect
Was This Post Helpful? 1
  • +
  • -

#6 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,533
  • Joined: 05-May 05

Re: Alternative to inner classes?

Posted 23 January 2012 - 08:11 PM

It's generally not a bad idea to make event handlers private classes, but only when your code is cleanly separated. Sounds like your design needs refactoring. What you should focus on is separating your business logic and view logic. I'll explain. The UI code is supposed to control the UI and nothing else. It shouldn't deal with any tasks outside of presenting information to your user. Your business logic should implement any model changes (updating a database or a Java bean), services (validating the input or doing a computation). If you had a GUI-based calculator application, the math code should be not be in the same class as the swing code. This is known as the MVC pattern. Here's a small example of how UI applications should be written. It makes your code a lot easier to extend and debug (especially when using Mantisse). Avoid spaghetti coded conditionals, method invocations, etc. in your GUI code.

In my code, the controller is an instance of ActionListener. It manages the view and model, which are passive. They do absolutely nothing. The controller is alerted when the user triggers a GUI event, and it acts accordingly. Since the controller is passed references to the view and model, it can do whatever it wants. It just relays information.

Mac did a tutorial on MVC that's worth checking out too.

This post has been edited by blackcompe: 23 January 2012 - 08:19 PM

Was This Post Helpful? 3
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Alternative to inner classes?

Posted 23 January 2012 - 10:19 PM

blackcompe is right
most probably your class becomes too long because your business rules are incorporated into your GUI
Was This Post Helpful? 0
  • +
  • -

#8 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Re: Alternative to inner classes?

Posted 24 January 2012 - 08:57 AM

I only use these inner classes because you can't have multiple instances of actionPerformed methods in a single class. Is it good practice (or even MVC) to continue using inner classes to hold these methods but do all conditional checking (or most of it... some of it you may as well keep in the GUI class) by passing values to methods in a controller class?
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Alternative to inner classes?

Posted 24 January 2012 - 10:06 AM

Is this a question ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1