Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing AWT Event GUI  RSS feed

 
crystal smith
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What am I not getting right here? nothing shows up but a blank window when i run this program.




 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. If you say implements ActionListener you are getting it wrong. You should have a separate class for each Listener, or if you are using Java8 you can use a code reference instead of an anonymous class for things which you only ever use once.

Also don't put lots of code in the actionPerformed method. Make it 1 line long and simply call a method in your Converter class. The Converter class should be separate from the display classes. Most of those components should be local variables in the constructor rather than fields.

I am moving this discussion to our GUIs forum.
 
Piet Souris
Rancher
Posts: 1983
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi crystal,

I'm not sure what the problem is. When I run your code, I see a nice frame
with all the buttons and textfields in place.

However, I would like to put some remarks on your code:

1) you have a JFrame member in your class. However, unless you have
some special purpose for that frame, leave it out. Your class extends JFrame,
so it is already a JFrame, and such an extra JFrame can only lead to confusion.

2) you use a FlowLayout for your contentpane. Nothing wrong with that,
but leave the determination of the size of the panel to that LayoutManager.
It's their job to determine the size.

3) when everything is defined and added, use 'frame.pack()', or in your case
just 'pack() (your class is already a JFrame), and it will calculate its own size
(it will ask the contentpane what size it has, and that contentpane will forward
the question to the LayoutManager).

So, with these remarks, I like to see your code like this:

Finally, I find Campbells remarks a bit extreme. I would have written
this program in the same way as you.

Greetz,
Piet
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing wrong with a class that "extends JFrame implements ActionListener" but it's not recommended or best practice. Why?

By combining the 2 types (JFrame and ActionListener), the ability to reuse diminishes. A class should be one thing and one thing only.

When it comes to GUI development, think of each component (frames, listeners, panels, menu, dialog etc) as a module (class). This approach enables you to plug and play the modules accordingly.

 
Piet Souris
Rancher
Posts: 1983
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have you looked at the program? It is short, it is concise,
very easy to follow and it does what it was designed for.
What more do you want?

Of course Campbell has a point, but in this case it is
like using a canon to shoot a fly.

I would say: put the theory to practise, write this program
in the way you want it to see, and show it to crystal.
Then she has two views, and so can make her own judgement.

Greetz,
Piet
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might be short and concise, but in ten minutes Crystal will want to convert ℉ to ℃ and then we are outwith the realms of short and concise. Then we are in the realms of multiple ifs in the method and horrible things like evt.getSource() == cToFButton, and as K Tsang says, the chance of reuse vanishes. Also the big actionPerformed method will become a maintenance nightmare. By not following the S in the “SOLID” principles of programming, we are moving from the conventions of OO programming into the conventions of procedural programming.

I believe the correct way to do it is to start with a class called CentrigradeFahrenheit or similar, which can be a utility class, since all both its methods are functions (even according to the stricter definition here), so all static members and private constructor, never called (because you never need an instance). Test it with a CToFDemo class or similar. Work out what to do when you pass -274℃ as an argument! Overload the two methods so they can take Strings as parameters.
Then you can use the CentigradeFahrenheit class unchanged in your GUI app.
I would use anonymous classes for the Listeners, since you are using them once and once only and you don't have two Listeners doing similar things. Only in Java8 you can use code references to dispense with the anonymous classes. You can read about it in the Java Tutorials here and here.

You do not need to look for the content pane to add things to a Frame; the add method on the Frame adds straight to the content pane. I don't think flow layout is any good; the sooner you learn other layouts the better. Maybe border layout.
The reason you aren't seeing anything is that you tried adding things after setVisible; setVisible should be the last statement (or you have to call revalidate).
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Piet The program OP has written is indeed concise and to the point. But think at a higher level up. How should the GUI app be designed? How should the GUI look like, where the components goes, what layout manager to use.... The list can go on and on.

If I were doing it using the OP's code as base, 3 classes: class with main, jframe class, listener class

 
Piet Souris
Rancher
Posts: 1983
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@K. Tsang
This is very helpful, now crystal has something to compare. Thanks for the effort.

@Campbell
Yes, I said you have a point. But my vision is different from yours. Let me explain.

People at this forum, of big name and even bigger fame and who have worked
most of their life in the IT business, have confessed that it took them "years to master
the OO concept". And I believe them. I myself struggled for years to get a little
into Java.

Now, if I look at crystals program, and the question that she has, I infer that she
is not a Java expert, with huge knowledge and half a life of experiece (if I'm wrong here,
then please give me the beating that I deserve!).
Then, why on earth should I demand of her to follow "strict OO priciples", and not even
solve her problem? This is not my attitude. Following OO principles will come with
growing knowledgde and growing experience.

Every replier here is entitled to his/her own style of replying, I know, and he/she has
all the right to it. But what I find the most important of all, is the tone. And sometimes
the tone of a reply on this forum irritates me. I do not doubt anyones intention though,
anyone replying does it with the best intention. But sometimes I find a reply rude,
arrogant, totally out of line, and that fact that seldomly anone reacts, is strange.

Your reaction as to that a slight change in what she wants will lead to extra if's and some recoding,
yes, spot on. Reusability? Indeed. But I'd say: let's give her the chance to find out herself. It's part
of the learning process.

Greetz,
Piet
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell & @Piet

Everyone who learns to program needs to start somewhere. Teachers don't teach you "design" from day 1 (at least mine didn't), they teach you the syntax to get a problem/exercise to compile and run "correctly". Refactoring is never mentioned not until the exercises get harder.


With this simple GUI (converter or not), having separate classes has its own advantages and disadvantages. One disadvantage is you may need to pass in parameters to the listener classes, which may end up lowering the reuse rate, even the logic in the action perform is generic.

Now thinking about it, this converter GUI thing can indeed do C-to-F and F-to-C with an additional option (eg radio button). I shall let the OP ponder on this.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when you can buy books which tell you to use public class Foo extends JFrame implements XYZListener
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote: . . . People . . . have confessed that it took them "years to master the OO concept". . . .
Agree. That is a particularly severe problem if you learn a different paradigm first. People (myself included) who learnt a procedural language first can take a long time to learn to think in objects. But it you let people write procedural code and only teach the syntax and not OO, then it takes the students that much longer to start thinking OO.

I like K Tsang's solution combining the Listeners and the Converters. Nice separation of responsibilities between classes, and a completely different solution from what I thought of. It shows there are always several ways to do something in computing.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote: . . . this converter GUI thing can indeed do C-to-F and F-to-C with an additional option . . .
But adding a new Listener would be following strict OO principles
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!