• Post Reply Bookmark Topic Watch Topic
  • New Topic

(JFrame) Does this seem ok?  RSS feed

 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm just messing around with JFrames and stuff like that (like previous posts). There is no ultimate goal here, which I realize violates that I should write everything down beforehand and know what I'm trying to accomplish. My question is, does this look ok? I'm going to start doing if...else statements but don't want to move on if I think this is bad programming. Any tips on how I create new JFrames under and if...else statement?

 
K. Tsang
Bartender
Posts: 3620
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the output (GUI) look what you expect? If so yes.

A pointer about action listeners, it is better to separate out these into their own classes especially if they can be reused. In fact, in swing, you can separate each component into it own class and and the JFrame class add the components accordingly.











 
Piet Souris
Rancher
Posts: 1782
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Implementing all action listeners in separate classes might give problems
relating to the visibility of other data. For instance, if a click on a button
must lead to some JPanel to be repainted, you must organize your code
in such way that that is possible.

One way would be to make all these classes nested classes, but that
somehow defeats the reusability.

But I agree with K. Tsang that "addActionListener(this)" for many
components, implies that you must come up with one huge
'actionPerformed' method. It is possible, of course.

Another possibility is to create a lot of anonymous actionlisteners,
and add the appropriate one to each component. For instance:

This way you won't have scoping problems.

Or you could create many Actions (see the API), and apply these to your components.

And there are the fancier ways. What about creating a HashMap<JComponent, List<ActionListener>>,
and then creating just one general actionPerformed, that simply forwards the ActionEvent
to the relevant JComponent via this map?

Well, you see, as always there are many possibilities. My advice:
try out as much of these possibilities as you can, just to gain experience and find out which way
you like best.

Greetz,
Piet
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actions are slightly more complicated to create than anonymous Listeners because they are longer. An Action is probably better than a Listener, however; not only does an Action incorporate the Listener, but it can also incorporate the colour, text, etc., of the button.
Now that Java8 permits functional features in the programming, you can pass a λ instead of an object reference. The details are rather too many for me to write now. Go to the Java 8 forum and see whether there is any discussion about how to replace an anonymous class with a λ. Or find the book promotion threads and get one of the introducing Java8 books from them.

startButton.addActionListener(() -> start(););

Where start() is an accessible method in the surrounding class.

Beware of having all those methods in the GUI. You should have the whole of the logic in other classes and the GUI should simply be a display.
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might need {} around start(); in my previous post.
 
Piet Souris
Rancher
Posts: 1782
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and a parameter too.

But I doubt that lambda's are very usefull here. For instance, let's add
one to button 'LastDragon':

which I find not easy to read, and we only have one statement. An average ActionListener contains
more than one statement.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that you don't need the braces and semicolon for a single statement in a lambda.

An average ActionListener contains more than one statement.

I would use a method reference for that.Note that the class does not have to implement ActionListener; all you need is a public void method that accepts a single parameter of type ActionEvent. This allows you to code separate methods for each listener and attach them to the JButton/whatever with a concise single line of code.
 
Piet Souris
Rancher
Posts: 1782
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sure makes it concise, but not much more than the classic anonymous classes
that I mentioned. It looks attractive though. I prefer the (e ->) notation, for that
makes you explicitly think of what parameter you are dealing with.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:I prefer the (e ->) notation, for that makes you explicitly think of what parameter you are dealing with.

Preferences are personal. I don't consider that significant seeing as how most listener (except PropertyChangeListener and a couple of others) code doesn't make use of the Event passed to a method.
 
Piet Souris
Rancher
Posts: 1782
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you do use the form 'e ->', you don't even need to implement a void method with
an ActionEvent as parameter. For instance

And maybe you do not consider a preference as significant, someone other
than you may. In more complex situations, say mappings, an IDE gives you
the opportunity to actually put the type of 'e' into the code. I've found that
to be of great help.

 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:And maybe you do not consider a preference as significant, someone other than you may.


Sorry for not being more clear. Personal preferences are definitely significant. The view I was trying to express is that much event listener code in the most commonly used (IMO) functional interfaces (ActionListener, ChangeListener) makes no use of the event parameter. Notable exceptions to this are ListSelectionListener, TreeSelectionListener where the event actually contains useful information.

Listener interfaces with multiple methods (KeyListener, MouseListener ...) usually do extract information from the event. But you can't use a lambda there anyways.
 
Piet Souris
Rancher
Posts: 1782
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed,

and I must say: using lambda's as Campbell suggests, and in the form you give it here,
is much more attractive than I thought. I'll keep that in mind next time I have to
work with these.

Greetz,
Piet
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!