Results 1 to 11 of 11
  1. #1
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    57
    Rep Power
    0

    Default Creating a compound JComponent and notifications

    I am creating a class for a reusable item which consists of a JSlider, JLabel, and JSpinner inside of a JPanel. These interface currently to a "Block" class with setter and getter for the one parameter being adjusted and displayed. The JSlider is used for coarse adjustment and the JSpinner is used for fine adjustment and numerical readout of the variable. The label is just the label explaining what the value represents.

    The JSlider and the JSpinner do not communicate directly with each other for getting values. Each one gets the current value by calling the Block class' getter for the variable, scaling it accordingly, and then updating the control. However, the listener for the JSpinner calls a function to tell the JSlider to update its value and position, and the JSlider's listener includes a call for the JSpinner to update its displayed value.

    In addition, I am using external functions which perform the scaling from the Block class variable value to what is actually displayed. The JSpinner can of course handle numerical values directly, however the JSlider needs some scaling applied to map to an integer range. Also there are other complications like linear vs. logarithmic scale for the JSlider, depending on the specific variable being adjusted.

    My main question here relates to making this group of JComponents (which are arranged in a couple of BoxLayouts inside of a JPanel) act somewhat similar to a single control, so I can listen for changes and send new values to it. It's no problem to create a class with this structure. It's not clear to me how to make this class send notifications other than the ones already being sent by the JComponents inside.

    Thanks for any insight! And sorry if this is in the wrong place.
    Last edited by Digital Larry; 07-17-2014 at 10:32 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default Re: Creating a compound JComponent and notifications

    These are all parts of a single component.
    The outer bit of the component is your JPanel (could this be a simple JComponent instead?).
    I would consider extending that panel and calling it <YourWidget>.
    That's your component.
    Provide a way of attaching listeners of a type of ChangeListener maybe, or simply define your own listener interface.
    When an event happens that you think external stuff needs to know about then fire off an event with sufficient data for the listener to act.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    57
    Rep Power
    0

    Default Re: Creating a compound JComponent and notifications

    Thanks. I found this thread at some "other" forum, which seems relevant. I do have one question here. The example shows the need to declare a List of listeners just because we don't know how many would be added. I guess in general, Swing components that use listeners can have any number of things listening. But in my case, I can pretty much guarantee that there will only ever be one. Is there any reason I shouldn't simplify the implementation to eliminate the List?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default Re: Creating a compound JComponent and notifications

    No reason.
    If you decide that you need a List you can always add that in later, as it would be a fairly small change.
    But, because it's a small change, would it be a big problem to make it a List in the first place?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    57
    Rep Power
    0

    Default Re: Creating a compound JComponent and notifications

    Thanks for the response. I think my question was primarily driven by curiosity. I'm not a professional developer, although somehow I have wound up managing a group of them at work. And they are always talking about notifications and observer patterns so this gains some real world relevance for me. It's probably a simple concept but at my age learning things is getting slower (not yet impossible, thankfully).

    I sat down last night and starting adding some of the code to my existing multi-component class. I have to move from two interacting classes (a specific JFrame/JPanel/Jwhatever control dialog plus the class that actually consumes the values it generates) to three classes to three (the consuming class, the class for my compound control, and the intervening class which holds them together) and it wasn't really clear where things should move to. But I usually work on this stuff late at night when I'm not quite thinking straight (cough).

    I also noticed in my reading that the Observer pattern is used a lot in model-view-controller architecture, which my program purports to be. And since I am not a professional programmer, I think that I may have implemented the program in a way that WHILE IT WORKS, is a non-optimal structure. For example, I have a MODEL class, which only deals with the structure and manipulations of the model, and a FRAME class, which instantiates the model, and handles menus and displays (the VIEW, I guess) and a PANEL class which deals with the application specific mouse input (it's a graphical CAD app). But these things don't interact by sending notifications, I've actually passed a reference to the VIEW to the CONTROLLER so it can access the MODEL. It's probably a "bad" implementation, but I'm not likely to rewrite it any time soon, because, welllll.... it does work.

  6. #6
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    57
    Rep Power
    0

    Default Re: Creating a compound JComponent and notifications

    I finally got around to trying an implementation of this and I want to clarify something.

    Originally I had a JSlider with a Listener in another class. This Listener takes the JSlider position and calculates a coefficient for a digital filter.

    Then I added a JSpinner to both display the numerical value and allow finer control of the input value than the JSlider (by direct user entry).

    I should just have the JSpinner talk to the JSlider right? The JSlider is the only thing that really needs to talk to the outside world. Within that compound class, as long as everything is representing the same parameter. then only one of the controls needs to have an external Listener.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Creating a compound JComponent and notifications

    Quote Originally Posted by Digital Larry View Post
    Is there any reason I shouldn't simplify the implementation to eliminate the List?
    No, but in that case you should have a method setXxxListener(XxxListener l), not addXxxListener(XxxListener l). And if desired a complementary removeXxxListener() instead of removeXxxListener(XxxListener l).

    As to your last question, I would suggest that the 'outside world' should interact only with the composite component, your widget class. Calls can be forwarded to the internal, private, components that constitute the widget.

    You could opt to use PropertyChangeListener methods along with a SwingPropertyChangeSupport to handle adding/removing listeneres and firing property changes.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: Creating a compound JComponent and notifications

    If you do use a setListener or removeListener as suggested, you should decide how to handle the setListener
    method if some object invokes it after it has been set. You could find a situation in which the wrong listener (thru an error
    in programming) is processing the events. And if the class is published it needs to be documented as to how that
    is handled. For example, you could throw an exception if trying to set a new listener prior to removing the current one.
    You could silently ignore it. Or you could just silently replace it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    57
    Rep Power
    0

    Default Re: Creating a compound JComponent and notifications

    Thanks for the responses. After reading up on listeners a bit I am no longer afraid of implementing it in a standard way with the list. If nothing else, then that component becomes more like everything else that people are used to using rather than some oddball component.

    Regarding whether the outside world should communicate directly with one of the internal components, or rather through a new Listener that itself would communicate with that component in the same class, I don't really see what the advantage of adding a new Listener is. I can see that possibly you wish to isolate internal components and keep them private and then come up with the minimal methods that are required to accomplish one's goal. Is there any other rationale? To me, it's more code to go wrong, but maybe I'm just scared because I haven't implemented anything like this yet.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default Re: Creating a compound JComponent and notifications

    Quote Originally Posted by Digital Larry View Post
    I can see that possibly you wish to isolate internal components and keep them private and then come up with the minimal methods that are required to accomplish one's goal. Is there any other rationale?
    That is the rationale.
    It's the same one as applies to isolating the state of an object, or coding against an interface rather than a concrete class. The idea is that the outside world should only need to know what the class does (as in its contract) and not how it does it. Exposing the internals of your class directly makes it harder for you to change how those bits fit together should you encounter a problem.

    Expose what a class does, but hide how it does it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Creating a compound JComponent and notifications

    Quote Originally Posted by Digital Larry View Post
    The JSlider is the only thing that really needs to talk to the outside world. Within that compound class, as long as everything is representing the same parameter. then only one of the controls needs to have an external Listener.
    The easy way: (slider is a private field of Widget)
    Java Code:
    public class Widget extends .. implements ... {
    ...
      public void addChangeListener(ChangeListener listener) {
        slider.addChangeListener(listener);
      }
    
      public void removeChangeListener(ChangeListener listener) {
        slider.removechangeListener(listener);
      }
    ...
    }
    Now you don't have to maintain any listener list at all.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Trying to get a compound interest program to work
    By gtorgers in forum New To Java
    Replies: 6
    Last Post: 04-10-2014, 03:07 PM
  2. Replies: 2
    Last Post: 10-01-2010, 08:18 PM
  3. How to use compound property names in Spring
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-30-2008, 11:08 AM
  4. How to use compound property names in Spring
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-26-2007, 09:39 PM

Posting Permissions

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