• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

HELP! Problem with Listener

 
Greenhorn
Posts: 28
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Guys,
I am having a problem with my button. When I select one of the radio buttons and then click the button nothing happens so it must be something with my listener.

Moreover, if I want to use this same file but use paint and repaint to draw a rectangle (I know how to use paint() but I constantly get confused as to what goes where) what would I do?
Can I do


Here are my codes


next code
 
Pat Watson
Greenhorn
Posts: 28
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works now. I was able to fix it BUT now I am having a problem with the paint() and repaint()
If I want the image to be painted based on first the selection and second clicking the button, How do I go about it?
 
Ranch Hand
Posts: 51
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
couple of advises:
don't extend from JFrame unless you alter its base behaviour - your code isn't a JFrame, it uses one
when dealing with swing make sure to override paintComponent() instead of paint()
I didn't look at your listeners, but be aware of how to deal with swings thrwading policy
 
Pat Watson
Greenhorn
Posts: 28
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Bendford wrote:couple of advises:
don't extend from JFrame unless you alter its base behaviour - your code isn't a JFrame, it uses one
when dealing with swing make sure to override paintComponent() instead of paint()
I didn't look at your listeners, but be aware of how to deal with swings thrwading policy



Thank you for the response. I am overriding the paintComponent() but it is not working. Here is what I have so far:
 
Marshal
Posts: 72066
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Bendford wrote:. . . override paintComponent() instead of paint() . . .

And give it protected access, not public. There is a format for the first line of that method which you should use so as to get the display blanked before you draw anything.Go through the Java™ Tutorials (I think the 2nd and 3rd sections there sill will be the most useful) and find out what sort of Listener you want. Something like a change listener maybe, but I am not sure.
I don't like lots of ifs. I am afraid it doesn't look object‑oriented. You can probably writeI have probably got all the method names wrong, so you will have to check carefully. I presume you are aware of the XXXAdapter classes in the java.awt.event package? I don't think they are applicable to change listeners, because that is a Functional Interface even without showing that annotation.

You can probably remove most of those comments because they don't tell you anything that isn't obvious from looking at the code.
[edit]Correct some spelling errors
 
Pat Watson
Greenhorn
Posts: 28
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Moving to our GUIs forum



I am almost there but now the drawing is showing under my north nested panel. when I am drawing the rectangle I specify the X and Y but it is not taking. I tried to add a center nested panel but now it is painting under it. Here is my code so far


and here is an image of the GUI with the image underneath it. I don't know what to do at this point


Error.PNG
[Thumbnail for Error.PNG]
 
Master Rancher
Posts: 4188
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code does not compile because of missing Rectangle2D class definition.

How can the code be compiled and executed for testing?
 
Pat Watson
Greenhorn
Posts: 28
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:The code does not compile because of missing Rectangle2D class definition.

How can the code be compiled and executed for testing?



The Rectangle2D is defined and I created an object before using the reference. Is that what you mean?
These are my codes



 
Pat Watson
Greenhorn
Posts: 28
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pat Watson wrote:

Norm Radder wrote:The code does not compile because of missing Rectangle2D class definition.

How can the code be compiled and executed for testing?



The Rectangle2D is defined and I created an object before using the reference. Is that what you mean?
These are my codes





By the way, I see the red rectangle but it is hidden under the North panel. I specify X and X where I want it to start but  it is not painting in the middle which is my problem
The GUI is attached
Error2.PNG
[Thumbnail for Error2.PNG]
 
Norm Radder
Master Rancher
Posts: 4188
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is the color set?  I the bottom of a rectangle is displaying as red.
Also what was the value of the rectangle's width and height?

Note: Rectangle2D is the name of a java SE class.  It is better if you do NOT give your classes the same names as java SE classes.
What is the need for the Rectangle2D class in the current program?


I added this to ShapesPanels for testing:
 
Pat Watson
Greenhorn
Posts: 28
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Where is the color set?  I the bottom of a rectangle is displaying as red.
Also what was the value of the rectangle's width and height?

Note: Rectangle2D is the name of a java SE class.  It is better if you do NOT give your classes the same names as java SE classes.
What is the need for the Rectangle2D class in the current program?


I added this to ShapesPanels for testing:



Thank you for the response. We have to create shapes objects within the paintComponent. Expanding from a previous project where we create different Shapes classes
I just realized that the issue is with my main Panel Layout. I have it as a BorderLayout so that I can position one nested panel in the North and the other in the South with the middle used to paint but this is not what is happening here.
I changed the main panel size in between each nested panel to this and now when I make circle 400, 400 I get the attached. So . Why is it coming from behind my north panel and is there a way to attach it to a nested center panel?

Error3.PNG
[Thumbnail for Error3.PNG]
 
Norm Radder
Master Rancher
Posts: 4188
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post your current code  that can be compiled and executed for testing.
 
Pat Watson
Greenhorn
Posts: 28
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Please post your current code  that can be compiled and executed for testing.



I am attaching the files








 
Norm Radder
Master Rancher
Posts: 4188
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The  paintComponent method is in the ShapesPanels class so the drawing coordinates are for that panel.  If you want the drawing to be defined by the coordinates for the centerDrawPanel object, then the code needs to override the paintComponent method for that instance of JPanel.
 
Saloon Keeper
Posts: 2828
149
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
paintComponent which is called during every layout calls getDim1/getDim2 which calls a JOptionPane.showInputDialog !!!
This means that the user is prompted with a dialog every time the screen is repainted or layout is done. That's quite bad.
 
Pat Watson
Greenhorn
Posts: 28
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:The  paintComponent method is in the ShapesPanels class so the drawing coordinates are for that panel.  If you want the drawing to be defined by the coordinates for the centerDrawPanel object, then the code needs to override the paintComponent method for that instance of JPanel.



Yes I previously tried an inner class but nothing was happening with ActionListener on the button
Here is what I had
 
Norm Radder
Master Rancher
Posts: 4188
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nothing was happening with ActionListener


Please explain what that means.  What actions did you take, what did the program do.
Add some print statements to show what methods are being executed. Print a unique message in all the relevant methods so the execution trail is shown,
Also print out the bounds for all the GUI class instances involved.
 
Marshal
Posts: 26383
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your problem is that you're creating a class which implements ActionListener, when all you need is an ActionListener. That's just extra stuff to distract and confuse. Just create (and use) an ActionListener:

Or better still, a newer and less verbose way:
 
Paul Clapham
Marshal
Posts: 26383
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And also... mixing AWT and Swing components can introduce unhelpful problems. Stick with Swing components: JLabel, JButton, JTextField, and any others that I missed in your earlier posts.
 
salvin francis
Saloon Keeper
Posts: 2828
149
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although we have a rule against doing this (https://coderanch.com/wiki/659991/Code-Mill), I am making an exception here since you have shared your complete code. I have modified your program and removed all the unnecessary code:

Since your initial problem is based on listeners, notice how I have used actionCommand in the code above. You can also note that the total number of field variables are bare minimum. The shapes in my program are also responsive to resizing the frame. The code is a bit long since it's swing UI code. There are some TODOs in the code above that I suggest you can research on.
Hope this helps.
 
Campbell Ritchie
Marshal
Posts: 72066
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:. . . missing Rectangle2D class definition. . . .

I can see another potential problem: there already is a class called Rectangle2D. If you use that awt.geom class, you can have all sorts of confusion.

. . These are my codes . . .

Sorry. Not pleased with that inheritance. I don't know about the Shape class, but let's forget that and create a Shape2D class without specifying a supertype, so its direct supertype is now Object. Let's start with the class looking the same as what you showed.
  • 1: Don't give subtypes direct access to the fields: as usual all fields other than global constants should be private, and in this case probably final, too. Any subtype access would be via the getXXX() methods.
  • 2: Don't override the getXXX() methods. In fact I would make the getXXX() methods final, too.
  • 3: I don't think you need a direct instance of Shape2D: make that class abstract.
  • 4: Minor point: I would call the fields and constructor parameters length and width. It isn't difficult to guess what l and w mean, but length and width are much easier to read.
  • 5: At this point you will have nothing in the subtypes but a constructor. In my opinion, that is what inheritance should look like.
  • You may consider adding getArea() or getPerimeter() methods; if you do, make them abstract methods in Shape2D.
    Consider whether your classes need toString(), hashCode(), and equals() methods.
    Advanced suggestion: Find Effective Java by Joshua Bloch, page 5 in the 2nd and 3rd editions. Can you restrict the access of the constructors and use factory methods such that Rectangle2D.getInstance(5, 5) returns a Square instance?
     
    Campbell Ritchie
    Marshal
    Posts: 72066
    312
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Salvin, can we have a bit of explanation about the use of Streams and the factory for panels please? I think that may go over OP's head otherwise. I am still not happy about multiple ifs, which I mentioned in my first post in this thread. Is there some way to map the text on each button to a shape class? Should we use Actions instead of action listeners?
    Please explain why you got rid of the variables c, r, and s from an earlier version of the code.
    What would happen if you made Shape implement a Paintable interface?You can now writeThe create() method takes a copy of the Graphics object in case you do something like affine transforms with it because they can spoil the effect of repeated paint events. Rotate scale and translate aren't too bad, but shear can cause such problems. I shall leave correcting the spellling as “an exercise for the reader”.
     
    Campbell Ritchie
    Marshal
    Posts: 72066
    312
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Some of us have been writing xyz -> pqr() for so long that we have forgotten it is unfamiliar code to some people. Have a look in the Java™ Tutorials; pay particular attention to the section about lambda expressions.
     
    salvin francis
    Saloon Keeper
    Posts: 2828
    149
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Let me explain a few sections of my code for better understanding. The code starts with a lambda expression:
    This can be written as:
    Effectively, the main program only creates an object of ShapeDrawingApp and then calls it's initUI() method. This follows the paradigm that https://coderanch.com/wiki/660020/Main-Pain
    Now, you might wonder why dont we directly call the method instead of using invokeLater. The answer is well explained in the java tutorials here: https://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
    It simply states:

    java tutorial wrote:all code that creates or interacts with Swing components must run on the event dispatch thread

    I suggest reading about lambdas a bit if you find that statement confusing: https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
    To put it simply, it's just a compact way of writing anonymous classes with a single method.

    Now, let's go to the variables... As you can see, I have only used 3 variables: The JFrame, the ButtonGroup and the ShapeDrawingPanel. This way, we only have field variables that are required to be accessed across methods.
    The ButtonGroup has a method called getSelection. This method returns the selected button from the "circle", "square", "rectangle" ones. Hence we don't a reference to each of them (your original circle, square, triangle, rectangle, sphere, cube, cone, cylinder, torus variables). We are only interested in the selected one. Let me know if that makes sense

    Moving to the initUI method, I created all panels in methods to keep the code neater and concise.
    Looking at the code for JRadioButton, the code you wrote was repeating the same steps for each of them, so this is where a loop comes into picture.

    The code reads as:
    We can discuss this further if it's a bit confusing. Streams may seem a bit scary at first ;)
    Once we have a list of all created JRadio buttons, we simply loop through them and add them to the button group as well as the panel. As you probably discovered already, adding them to the group creates this bond that only allows one of them to be selected. I have also taken the first item from the list using stream's findFirst() method and set it to selected by default. This is in line with your original code.

    Now that the GUI is done, we add the listeners to the buttons. Since shapeSelectionGroup is a field variable, we can access it and find out which one is selected every time the user clicks the button. Additionally, since drawShapePanel is a field variable as well, we can ask it to draw our selected shape. As Campbell rightly stated, the if-else condition is still not a good way to draw the shape especially given that we know that they can be different objects with behaviors.

    I'll let you work that one out, let us know if you are still unable to get that to work. Hint, Campbell has shared a nice little interface that can draw itself !!
     
    salvin francis
    Saloon Keeper
    Posts: 2828
    149
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Should we use Actions instead of action listeners?

    Yes, that's a good idea. It exactly matches our purpose in this case.
     
    Creator of Enthuware JWS+ V6
    Posts: 3386
    311
    Android Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Congratulations Pat Watson,

    Your question has made it to our Journal    

    Have a Cow!
     
    ice is for people that are not already cool. Chill with this tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic