• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Netbeans - How to make current color (on a color selection GUI) available to outside components  RSS feed

 
Pete Yanchek
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have my GUI in place and the sliders and textfields (which are restricted to 0-255 input) are bound and cause the color panel to instantly update.
This GUI is pictured on the left.

How do I now allow an outside component--like the test component example on the right--to access and use this GUI's current color?
I'm supposed to make the color an instance variable and expose it to outside classes, but I can't get it to work.
(Also, I don't quite fully understand how the fireColorEvent() method works--specifically the synchronized and Vector aspects)...

Any help whatsoever on how to allow outside classes to access the current selected color would be greatly appreciated!
I also need to make a text class like the example to show that it is working.
Here's how I'm getting the color within the ColorChooser (which is just the main panel on my GUI which contains the sliders and textfields):


JColorChooser.jpg
[Thumbnail for JColorChooser.jpg]
 
Campbell Ritchie
Marshal
Posts: 56197
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't ask the same question twice; I have deleted the other copy. If you confirm there is a difference between the two copies, I can find the deleted post.
I am afraid I can see all sorts of things wrong with that code.
  • 1: Avoid making a display Component a Listener. The panel oughtn't to implement a Listener interface (except where the event relates directly to that component). Create a Listener class and add its instances to the three sliders. For some Listeners, it is possible to use a λ expression instead (Java8+ only).
  • 2: Why have you got a List? Why have you used a raw type? Why have you used Vector, which nobody uses any more? Where are you adding anything to that List? Why are you cloning that List?
  • 3: Why do you have a synchronized block? Since you mustn't access Swing components from multiple T‍hreads, that looks unnecessary to me.
  • 4: Where do the colour listener and colour event interfaces come from? I am sure that isn't the correct way to change colours. And you appear not to be creating any instance of those two interfaces, anyway.
  • 5: Where are you using the binding group?
  • I suggest you start by not using NetBeans GUI builders until you have lots more experience with Swing. I also suggest you don't need to extend any display classes, nor do you need that List in this class. Having a List there confuses the meaning of the panel: you are using it for business logic whereas display Components ought to be used for display only. If you do use a List, use an ArrayList. I also think you don't need the two interfaces at the bottom.

    You are, I think, asking the wrong question. You do not want to make the colour available to different components. You want to tell the components to change to a particular colour, in this case the same as the current panel. Getting a colour is really easy:I think it is not necessary to take a defensive copy, but I am not certain. In fact you will probably find that a JPanel already has a method like getBackground() which does exactly that, so you don't need to write any methods here. Yes, here is that method. But that won't sort out your problem. Notice that you don't need to have a colour field either, because there already is one in the panel, called background.
    I think you need an object which encapsulates all the Components whose colours you want to change, so that entails a new class, with these methods:-I also think you want a Listener for each slider. ChangeListener is the correct listener to use, I think. It is a Functional Interface so you can create a λ with it (Java8+ only): read about λs in the Java™ Tutorials.That λ is the equivalent of adding an instance of the same class to each of the three sliders, which is what I recommended at first.
     
    Campbell Ritchie
    Marshal
    Posts: 56197
    171
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A few minutes ago, I wrote: . . . . . .
    That would probably be better as...changing this to colourPanel in lines 5‑6.
     
    Pete Yanchek
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the advice. I am in a unique situation where I was allowed to take this course online, even though it is an in-person course. (My work doesn't allow me to leave mid-day). I haven't heard a word of lecture and I haven't had a single interaction with other students. Basically, twice a week, I look at the programs that have been posted online and I figure them out. This particular assignment is very difficult for me, as much of this was not covered in our text. It's essentually trying to learn with no help, based merely on the provided code.

    This program was designed by my professor. The only modification that I have made thus far is the addition of the textfields, which were then binded to their respective sliders (using "binded beans"). The program <i>does</i> work, and the color panel changes in sync with both the textfields and sliders. I'm not sure about the synchronized code--I had to look up synchronized myself, and after trying the code both with and without that part, I determined that it was necessary, since otherwise, the color panel does not change. The vectors part in general is really confusing me.

    I had been doing so well in my first Java class and in the first part of this one, but my anxiety level is now very high, as I am placed at an extreme disadvantage by taking a meant-to-be in-person course without ever attending lectures.
     
    Pete Yanchek
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I also added the StayeChanged events for the sliders, in order to reflect tjose values within the textfields.
     
    Campbell Ritchie
    Marshal
    Posts: 56197
    171
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I merged your stuff with the following thread. I hope that is okay by you.
     
    Pete Yanchek
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Suppose that I have an instance of a JFrame which contains a JPanel. This JPanel keeps track of a color instance variable (which changes based on its current Red, Green, and Blue slider values).
    I need a way to access that color variable from a completely separate JFrame.
    I am running into errors related to static vs. non-static.

    Is it possible to accomplish this access?
    In other words, my JFrame contains a specific JPanel (which is an instance of a certain JPanel class).
    Each JPanel is associated with a variable for its current collor and I need to access that color in completely separate classes.
     
    Paul Clapham
    Sheriff
    Posts: 22691
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Of course. Presumably when you say "a JPanel" you actually mean "an object of a class which extends JPanel".

    So you have something like this:



    The natural thing to do would be to add a getter for that Color object:



    Then your "completely separate JFrame" would need a reference to the Whatsit object; most likely you'd take care of that when you create the Whatsit object as part of initializing the GUI. Like this:



    To get the Color all you need to do is call the appropriate method on the Whatsit object:



     
    Pete Yanchek
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks, Paul! It does make more sense to open both windows at once, as you kindly suggested.
    (I have no idea why I was viewing the problem as one which presumably involved running them separately).
    I now have access to the color variable! I can grab it directly from a JColorChooser object with a getColor() method.

    I'm very close to making it all work now, but I still have a little issue.
    Here's what I have for the test class...

     
    Dave Tolls
    Ranch Foreman
    Posts: 2996
    37
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Separate out the Colour from the Gui code.
    That is, create something that sits "above" the Gui and monitors what goes on, otherwise known as a controller.

    This controller can register itself (think "listeners" in Swing) with the Chooser so that, whenever the Color changes, it is informed and can modify the Color object that it controls.
    The controller can then react to changes.  One of those reactions is to inform the other parts of the Gui that the colour has changed.
     
    Campbell Ritchie
    Marshal
    Posts: 56197
    171
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Don't create new th‍reads about the same question. You have not sorted out the questions in the first discussion.
    Is this an exercise in the use of beans and bindings? Are you being taught about bindings? If so, you shou‍ld tell us; we can only help if we know the full details.

    Why are you unable to see the lectures? Can't you have them online? I suggest you see your teachers and discuss this problem with them. I do not believe you can learn much simply by reading programs.
     
    Pete Yanchek
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No problem. I'll only post here for this question.
    It's an in-person class. I was only allowed to skip the lectures because I work full-time.
    The problem is that this assignment has very little to do with the content on the syllabus.
    I feel overwhelmed.

    I have gotten all 100 scores on my lab exercises in my Programming Introduction course (Java) and on the first few exercises of this course.
    I am really good with certain aspects of coding, but all of a sudden, I feel very confused.
    If I had been in class, I'm certain that I'd know what to do, since I am a visual learner.
    Looking at code with no instruction--that doesn't tie into the textbook--has left me at a disadvantage.
    It's almost like looking at some arcane text and trying to decipher it without help.
    I was able to follow everything up until this point... but now I am not able to fully grasp what must be done.

    Is there any place that you guys know of where I can pay a professional to look at the full program and tutor me briefly?
    The assignment is due Monday... I am trying to switch over to computer science, so I need A's and I can't let a terrible lab grade bring me down.
    (Most importantly, I need to understand).
     
    Pete Yanchek
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks, Dave. I'm not certain if I can do it, but I will try.
    PS - does it help to know that the following Class and Interface already exist? . . .




    Also, below is the code for JColorChooser and ColorChooser.
    ColorChooser is just a JPanel bean that is binded to the JColorChooser JFrame. It contains the sliders which control the color.





     
    Pete Yanchek
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I spoke too soon, gentlemen. I just figured it out.
    Dave, you were absolutely correct. I created the following class:


    And then I used the following method in my JColorChooserTest program:
        @Override
        public void actionPerformed(ActionEvent ae) {
                testingLabel.setForeground(ColorStorer.color);
                setColorButton.setBackground(ColorStorer.color);
        }

    It worked like a charm.
    The only thing that I need to do now is figure out how to leave the JColorChooserTest window open when I close the JColorChooser window.
    I recall hearing about a dispose() method. I'll look into it and come back if it doesn't make sense.
    Thanks to all who helped out - especially Paul and Dave.
    Computer science is even more appealing because of how helpful everyone is to one another. Thanks.
     
    Campbell Ritchie
    Marshal
    Posts: 56197
    171
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Non placet. You have made the mistake of using the static modifier. That means, if you have two GUIs, they will willy‑nilly be the same colour. It is good to create an object to store the colour, but you are not reating an object there. The colour field shou‍ld be private and not static. Give the class a constructor for the initial colour, and changeColour and getColour methods. The changeColour method is a sort of setXXX method. Use the getXXX method instead of direct access to the field.
     
    Pete Yanchek
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Good point. I fixed it. Thanks.
     
    Pete Yanchek
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Does anyone recognize the look and feel of the window on the right?
    Mine (default) is on the left, but I need it to look like the one on the right, which is the example that the professor provided.
    comparison.jpg
    [Thumbnail for comparison.jpg]
     
    Pete Yanchek
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It looks similar to Macintosh, but with transparent buttons in the top left.
     
    Rob Spoor
    Sheriff
    Posts: 21117
    85
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Try using the system look&feel:
     
    Pete Yanchek
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well, my textfields aren't working. I'm trying to restrict the input to 0 - 255.
    This has been a real pain. This stuff isn't in our textbook and I'm not getting it.
    The example program from class works, but mine doesn't.
    Could it have anything to do with binding?

     
    Paul Clapham
    Sheriff
    Posts: 22691
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have two questions about that: what does "doesn't work" mean, and what is this "binding" which you think could be a problem?
     
    Pete Yanchek
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So by doesn't work, I mean that any value can still be entered. But since each JColorIntegerField that I have is binded to a slider, if anything over 255 is entered, it defaults to 255 (the slider max).
    (Values such as 255555, 25566665064, etc. can still be entered, however - as long as it starts with 255 or less, which is wrong and not what I want to happen).

    I used the same three class structures as my professor and his textfield restrictions work, but mine fail.
    The only different that I can think of is the fact that my textfields are binded to sliders.
    The textfields are also a part of a panel, which itself is binded to my JColorChooser JFrame.
    Would any of this impact the validity of the RegEx restrictions? It makes no sense to me.
    (My classes are literally the same as his; only I change the RegEx - and when I tested it in his program, it worked and restricted input to 0 - 255).
     
    Pete Yanchek
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    When I say binded, I mean as a Bean.
     
    Pete Yanchek
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I got that mixed up - the JColorIntegerFields are binded to JSliders, yes...
    And they are contained within a JPanel which is a Bean component on my JFrame.
    Yeah...
     
    Pete Yanchek
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Problem solved - my instinct was correct and it was the binding between the JTextFields and JSliders that caused the problem.
    Now my RegEx works.

    I just need to implement some manual coding to make sure that my fields and sliders always reflect the same value.
     
    Paul Clapham
    Sheriff
    Posts: 22691
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You may think that your RegExDocument works, but it doesn't always work the way its design says.

    Example: you can type "200" into it and it accepts that, since your regex matches that. But then you can delete the "2", leaving "00". Your regex doesn't match that so it should reject the deletion. But there's no code for it to listen to deletions so it accepts that as well.
     
    Pete Yanchek
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Guys, this program is almost running, but I'm having a huge problem with my DocumentListener.
    I need the color sliders on my panel to reflect new values as they are entered into the color textfields.
    I'll show you my two classes below. The problem is that my StateChanged methods for my sliders (which in turn change the textfield values) conflict with my DocumentListener methods.
    It doesn't know which to execute first, I guess.

    The error starts with:
    Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Attempt to mutate in notification


    I can change the background of the textfield, based on insertion or removal, but I can't change the slider values.
    How can I change the sliders?


     
    Darryl Burke
    Bartender
    Posts: 5167
    11
    Java Netbeans IDE Opera
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your problem lies in that the DocumentListener updates the slider value and the slider's ChangeListener tries to update the text field.  One way to get around this is to use a boolean flag.  Something likeThis limited example assumes both listeners are in the same class and will need to be enhanced to work between classes.  I would probably approach this with a class containing ONE text field and ONE slider, possibly encased in a JPanel, and two anonymous listeners; then add three instances of the class to the GUI.
     
    Pete Yanchek
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks. What would the code be to set the sliders, though?
    How can I determine the source (textfield) which had the insertion or deletion?
    Would you use my customer MyDocumentListen class somehow, or just implement standard DocumentListener?
     
    Pete Yanchek
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I meant to type custom; not customer.
     
    Pete Yanchek
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I may have it.
    I am trying this to start:


    Does that look correct? I'll test it soon and see.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!