• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Swing questions

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
yesterday I've begun programming my first 'big' project (big compared to other stuff I've been doing anyways) and while I could look up most of the things I didn't know, I've come across a few problems which couldn't solve.
First of all, you should know that I'm still relatively new to java and I've been using the Head First Java book to learn the language. I've just gotten past the GUI section, so I decided to play around with the swing class.
What started out as a few tests soon turned into a small project and now I'm almost done. Only a few things don't work, but before I ask my questions, I think I'll post my code first. (Note that I didn't use any kind of IDE and that the code may be very unorganized)



Questions:
1. Was I right in making all those inner classes for the buttons etc.? In Head First Java it is done like this. However, after looking at code in the internet I've noticed that most of the time the class itself simply implements ActionListener and overrides the method actionPerformed. Then, by using the getSource method with if/else statements, the method will run code depending on which button called the method. What is the right way? Does it come down to preference or are there situations when one is better than the other?
2. As you may or may not have notice, when clicking on the credits menuItem a new Frame will be created. I didn't know how else I could open another window. While this method works, when trying to close the second window the first one will close as well. I could probably fix this by removing the setDefaultCloseOperation line, but I don't think this is the right way to do it. I could have also created the frame in the initComponents method and set it invisible until the credits button was clicked, however I don't know how i would set it invisible again. What's the right way to make such a window?
3. Is the design of my program right? The Head First Java book always says how one should build their program Object-Oriented, but I don't know how that would apply to a program like this (Should I have created more than one class? Or should I have extended the Frame class? etc.) Also I wasn't sure what methods to create. I could have put all the code from the initComponents method into the constructor. I could have also put the code into a non-static block (learned about those in the oracle tutorials). How do I know what the right way is?
4. Finally, is there anything which I could've done better? Is any code maybe badly written or doesn't follow the coding convention etc.? All input is appreciated really. I would like to become a programmer, so this isn't just a hobby for me. If there is anything bad, point it out.

PS.: I hope this is in the right section. I wasn't sure if I should put this in general or beginner Java. In the end i put it here, because I still consider myself a beginner, but if decided wrong please move it. (I also didn't want to put it in the AWT/SWING section because I also ask questions about design)
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Well I guess your questions are more Swing than beginning/general Java.

Looking briefly at your code, it looks good - clear enough to what is what. The OO (Q3) here becomes whether you want to "split" each Swing component (JFrame, JMenuBar, JPanel, Actions etc) into separate classes. The initComponent() method then diversify into many classes...

As for action listeners (Q1), I personally haven't used event.getSource() but it really again depends on what you want to do with the action. If the action is inter-related or should I say tightly-coupled with some other component, then inner class would be a good choice. Else it is practical to separate it into its own class. Also you may also want to look into AbstractAction. The programming is the same (override actionedPerformed method) only the class declaration is different. Instead of implements ActionListener, it is extends AbstractAction.

For your credit action (Q2), use a JDialog instead of JFrame. Even this JFrame is another variable independent from the main JFrame.
 
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is some good stuff in that code.

Janis Mittelstaedt wrote: . . .
1. Was I right in making all those inner classes for the buttons etc.?

Yes

However, after looking at code in the internet I've noticed that most of the time the class itself simply implements ActionListener . . .

That is an abomination against object‑oriented programming.


2. As you may or may not have notice, when clicking on the credits menuItem a new Frame will be created. I didn't know how else I could open another window. . . . What's the right way to make such a window?

There is no right way to make a second window. You use a dialogue.


3. Is the design of my program right? . . . (Should I have created more than one class? . . .

You should never try to squeeze everything into one class. You should start with a class which calculates ℉↔℃, which can be a utility class: private constructor, and static fToC and cToF methods only. You should have that class running correctly before you write a single byte of GUI code.

I prefer the sort of design where you do not extend JFrame, so I think you are correct there.


4. Finally, is there anything which I could've done better? Is any code maybe badly written or doesn't follow the coding convention etc.? . . .

You asked for it
  • 1: Recommend not to use on demand imports. Use the type of import without *
  • 2: Why did you choose those two initial values for your ℃ and ℉ fields?
  • 3: What errors do you expect from using integer arithmetic for your conversion?
  • 4: You are using a boolean to decide conversion direction. Are you not aware of enumerated types, which allow you to write C_TO_F or similar?
  • 5: Instead of the cToF flag, you might do better to have two methods, one called from the CtoF button and the other from the FtoC button.
  • 6: Try these Unicode escapes: \u2103 \u2109
  • PS.: I hope this is in the right section. . . .

    We usually discuss GUI design in our GUI forum, but rather than moving this discussion I can duplicate it in the other forum, where Rob is in charge. He is much benign‑er than me!
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I see somebody else has already duplicated this thread.
     
    Janis Mittelstaedt
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you for the fast replies!

    K. Tsang wrote:As for action listeners (Q1), I personally haven't used event.getSource() but it really again depends on what you want to do with the action. If the action is inter-related or should I say tightly-coupled with some other component, then inner class would be a good choice. Else it is practical to separate it into its own class.


    That makes sense, thanks.

    K. Tsang wrote:Also you may also want to look into AbstractAction. The programming is the same (override actionedPerformed method) only the class declaration is different. Instead of implements ActionListener, it is extends AbstractAction.


    I will look more into that once I'm done with my book, seems very interesting.

    K. Tsang wrote:For your credit action (Q2), use a JDialog instead of JFrame. Even this JFrame is another variable independent from the main JFrame.


    This is exactly what I was looking for!

    Campbell Ritchie wrote:You should never try to squeeze everything into one class. You should start with a class which calculates ℉↔℃, which can be a utility class: private constructor, and static fToC and cToF methods only. You should have that class running correctly before you write a single byte of GUI code.


    So I wouldn't make an instance of that class, kind like using the Math classes, right? Does this mean I should declare the class abstract and final as well? Or does the private constructor take care of that?

    Campbell Ritchie wrote:

  • 1: Recommend not to use on demand imports. Use the type of import without *
  • 2: Why did you choose those two initial values for your ℃ and ℉ fields?
  • 3: What errors do you expect from using integer arithmetic for your conversion?
  • 4: You are using a boolean to decide conversion direction. Are you not aware of enumerated types, which allow you to write C_TO_F or similar?
  • 5: Instead of the cToF flag, you might do better to have two methods, one called from the CtoF button and the other from the FtoC button.
  • 6: Try these Unicode escapes: \u2103 \u2109

  • 1: So you mean importing 'javax.swing.JFrame' etc., right? So when do you use the '*'? If ever? Would you only use it if you use all the classes, or maybe once you use about half of them?
    2: (The celcius number was actually supposed to be '0' by the way) I'm not sure why I did that. I probably needed initial values at some point during development and forgot to remove them. I'd have to change a little code, because one label uses the fahrenheit Integer before it gets any value.
    3: I'm sorry, english isn't my main language so I'm having trouble understanding what exactly you mean. Should I be expecting any errors? The convert button won't work if you put in a double or String and otherwise the conversion seems to work fine.
    4: I haven't read anything about enumerated types yet, so I guess the answer is no. Once I've made the JDialog, I'll look into it though.
    5: I guess this will be more OO, since it would make it easier to add another temperature conversion. I will have to change that anyways once I remove the cToF boolean.
    6: That's a good idea. I didn't even think of that. Mainly because I used the oracle java tutorial on Swing GUI with netbeans for my project plan. That's actually how this project came to be. I stumbled upon this tutorial, saw how the finished product was supposed to look and made it a kind of challenge for me to see if I could do it without any tutorial (or IDE)

    Campbell Ritchie wrote:That is an abomination against object‑oriented programming.


    That's good to know!
     
    Rancher
    Posts: 3324
    32
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    However, after looking at code in the internet I've noticed that most of the time the class itself simply implements ActionListener . . .

    That is an abomination against object‑oriented programming.



    You don't want to do that for your main class. However, it is quite acceptable when creating factoring your code into functional classes. For example if you want to create a class that does mouse handling for you you might do:

     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Janis Mittelstaedt wrote: . . . 1: So you mean importing 'javax.swing.JFrame' etc., right? . . .

    Yes. It is probably better to be able to see which classes have been imported than to try guessing with *.
    And there is something else. Try this:-/

    3: I'm sorry, english isn't my main language so I'm having trouble understanding what exactly you mean. . . . The convert button won't work if you put in a double or String and otherwise the conversion seems to work fine.

    So you can accurately convert 21℃ to ℉ and convert it back? Why won’t it work for doubles?

    . . .
    6: That's a good idea . . .

    Too much time spent hanging about the Unicode website.

    . . . challenge for me to see if I could do it without any tutorial (or IDE)

    You are doing quite well there

    Campbell Ritchie wrote:That is an abomination against object‑oriented programming.
    That's good to know!

    People who write that sort of thing on the internet ought to have their fingernails pulled out very slowly
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Rob Camick wrote: . . . For example if you want to create a class that does mouse handling for you you might do:

    There is nothing wrong with a Listener which implements several interfaces.
    In the case of mice, however, the events might be directly associated with the Component, for example because they have a location. That location is relative to the Component, so it is logical for the Component to listen to its own mouse events.
    What is wrong is to have several buttons and one frame which listens to all those buttons.
     
    Rob Camick
    Rancher
    Posts: 3324
    32
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I think we are saying the same thing. The original quote was:

    I've noticed that most of the time the class itself simply implements ActionListener . . .



    I just wanted to clarify there is nothing wrong with a class implementing a listener.

     
    Janis Mittelstaedt
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:So you can accurately convert 21℃ to ℉ and convert it back? Why won’t it work for doubles?


    I guess i could rewrite the program so it converts doubles. Not a bad idea actually.

    Campbell Ritchie wrote:And there is something else. Try this:-


    Do you mean the non-static block? I've read about those, but I'm not sure when I should use them. Can't you just assign values to instance variables when you declare them? And if those values depend on other values there is already the constructor...
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Janis Mittelstaedt wrote: . . .
    Do you mean the non-static block? ...

    No. I don’t like initialisers, but that is simply a personal preference. That code was not a block, but a complete class. Compile it and see what happens.
     
    Janis Mittelstaedt
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ah, you get an error because both have the class Timer.
    So if there was a case when you needed them both, would you import the one from util and use 'javax.swing.Timer' for the other (or the other way around)?
    Or how does that work?
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If you need both Timers, you would have to use the fully‑qualified name for both.
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Janis Mittelstaedt wrote: . . . would you import the one from util and use 'javax.swing.Timer' for the other (or the other way around)? . . .

    I think that would work, too, but am not certain.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic