Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java GUI BorderLayout application 4 buttons Left, right, up and down each move location  RSS feed

 
Eli Kook
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been breaking my head for days over a project I have to do in my Java beginner class about GUI BorderLayout Jbutton and I really hope some one here can help me out to understand it or shed some light. My task is to create a BorderLayout window with 4 button left right up and down Each button moves the window/ Borderlayout 20 pixel left or right or up or down. I have already created a code with the buttons but I do not to know how to make the buttons move and above all I must not allow the Window to move out/ disappear from the desktop. Please be patient with me I am totally fresh student. I must write an inner class the event listener a code for locating the position on screen and change it. Any help will be very much appreciated. Here is my code so far:
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I would suggest you don't want an inner class, but four: LeftMoveListener UpMoveListener etc. That way you get rid of horrible things like checking the action command. Always call classes which implement Listeners XYZListener rather than InnerClass.
You need to add the Listeners to the buttons otherwise nothing will happen. You cannot add the listeners from inside your inner class.
leftButton.addActionListener(new LeftMoveListener());

It seems weird to have a frame move by itself; is that really what you are supposed to do?
I tried it and got Exceptions that I was dividing by 0. You will have to change those 0s to something else.

Once I sorted out those problems, the frame moved, but not necessarily in the direction I expected

We usually discuss GUIs in our GUIs forum so I shall move you there.

[edit]Change listener names to start with CapitalLetters[/edit]
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some of your lines were too long, so I broke them and you can see how it should be done. I left the 0s for you to find however.
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may have to move the inner classes so they are declared before the add instructions.
 
Eli Kook
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello and Thank you for your reply first of all.
Unfortunately I must have the event Listener in an inner class because that is a part of the task.
I am not quite sure if I get you right could you please elaborate?
The idea is that when you press the Button Right, the frame will move 20 pixel to the right and so on.
 
Piet Souris
Rancher
Posts: 1782
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Eli,

first of all I would like to point you to something that I see with some regularity
on this site, and which often leads to much confusion.

Your 'WindowBorder' class extends JFrame. But look at your code: what do you
actually do with this 'extending JFrame'? Nothing. Instead, you work only
with an instance member called 'frame', which is also a JFrame.

This can lead to much confusion, as I said. In your constructor, you have the
parameter "String titel". However, the only thing you see on your screen
is 'frame', which, accidentally, is given the same titel. That's probably why
you did not notice anything strange.

Easiest way to remedy: drop the 'extends JFrame' from your WindowBorder
class, and work solely with 'frame'.

However, that will lead to some other errors. First of all, 'super(titel) will no
longer work! Drop the line.

The real confusion of having actually two JFrames can be seen in your innerclass
'InnerClass'.
For instance, you have there:

Notice the expression 'getSize().width.

Of what thing is this taking the width? Exactly, from the instance of WindowBorder,
which is in all likelyhood 0, since we do nothing with that.
What you want to do is take frame.getSizse().width.
And your formulae are incorrect. Why not simply add 20 to whatever the x and/or
y of the frame are?

Lastly: one innerclass is fine, it is simple enough. You only must create an instance,
so right after the definition, do:


Finally: look at how you create your buttons, and how you decide the 'actionCommand'
of the button pressed. Do you notice any difference?

Greetz,
Piet
 
Eli Kook
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Piet, thank you for your reply!
I have removed the frame, so there will be no more 2 frames, it was not necessary to have duplicity, you were right.
Regarding the the Inner class, If I change as you suggested I get error.
As for the Button, I have no idea what am I doing, I am only now learning this and I still don't get how to build the code right so that the button will work and the window will move.
 
Piet Souris
Rancher
Posts: 1782
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's deal first with the ActionListener.

Suppose you have this code:

However, this is a class, and what we need to do is to put
an instance to the buttons. So, let's create an instance of
InnerClass:

This code can get right after the definition of InnerClass.

But now we have to add this instance as ActionListener to
our buttons. We do it as follows:

You see that this code ("up.addActionListener(inc)") is NOT
part of the InnerClass definition!

What exactly did you remove? The variable 'frame' or the 'extends JFrame'?

And what I meant about the determination of the ActionCommand:
If you creat a button like this:

then "Eli" is by default the actioncommand. What do you think will be
the result if you, later, do:
?

Anyway, try to put it into the code. If you still have some teething problems,
show us an updated code here, and don't forget to use the code buttons while
you do!

Greetz,
Piet
 
Eli Kook
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Piet,
Thank you for your message!
I have made some changes to my code and now all seems to be working but my only problem now is that after pressing the right button and
changing the location of the window, it seems to display out of me screen.
How can I prevent it and what is it that is wrong in my code?
Here is my code:
 
Piet Souris
Rancher
Posts: 1782
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Eli,

we're nearly there.

The problem you're now facing is: how to get the x and y values
of the instance of your MoveBorderDemo, in the actionlistener.

It is a little bit technical. First a wrong attempt:


Try it, and you'll see that this doesn't work. The reason is that 'this' is not
referring to the MoveBorderDemo instance, since we're dealing with the class
'ListenerInnerClass'.

The correct way is:


You see that I use: 'MoveBorderDemo.this', to get a reference to the instance.

By the way: if you look at the API of JFrame, you see that you also have
the ints 'x' and 'y'. You might think, as I did, that these were the same as
when using the methods 'getX()' and 'getY()'. In the code above, I print
both to System.out. Try it, and figure out what the direct 'x' means. Strange, isn't it?

There are other ways to achieve what you want, and they might be a bit easier.
But I think that what you have now is a fine program.

Well, time to complete the ActionListener!

Greetz,
Piet
 
Eli Kook
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is quite confusing me I must add but I do thank you for all your effort to help me, I appreciate it a lot!
I made other change in my code and it works well (moving 20 pixel) but there is still one thing that is a part of the task that I try to understand in abstract way.
The last part of the task:
I must make sure that the window is not pushed out of the Desktop
and determine the current size of the desktop and compute the maximum possible coordinates for the shift/ move
right and down. For the shift to the left or above I can use the value 0 as a boundary accordingly to the task... how can I limit the X and the Y?

 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might do well to draw a diagram of the screen and the window you have. If your screen is 1024px wide (most screens nowadays are at least 1368px wide) and your window is 350px wide, then you should not move its left margin farther right than 674. You can do similar calculations for downward motion.
You should not let the top left corner of your window go farther up/left than (0, 0), or never allow the xy of the top left corner go negative.

Which method are you using to move the frame? Is it setBounds? Have you come across this method?
 
Piet Souris
Rancher
Posts: 1782
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Eli,

first of all: I'm typing this on a Windows machine with IE9 as browser, and on this browser
the code that I give looks a complete mess that I cannot seem to get n a normal state!
So, if it is unreadable, I wil change it this evening when I am at home.

/****************************************************/
@attending officer
is this a known shortcoming of IE9, or is it me doing something wrong?
I copied the code from Wordpad into the reply-frame.
/****************************************************/

yes, I've been neglecting this point until now. But this requirement is a bit overcautious,
since by clicking the buttons you never can get your window far out of the screen! Only as
far as your buttons are wide/high.

But incorporating this requirement in the current ActionListener will make that ActionListener
a dragon, even more than it is now. So, it is time to do some simplifications, that will also
help giving the code some more structure. So, here goes (note: where I have '???' it is
your task to put something useful in!)

Let's simplify the determination of the screensize a little bit. Create this method:


Now, lets remove all complicated calculations about new coordinates for
your frame out of the ActionListener and put it in a dedicated method:

Now, having this method, with parameter being either “left” (or “Left”), "up" or "Up",
et cetera, the ListenerInnerClass can now be greatly simplified



/*****************************************************************/

And finally, another simplification. This might be far ahead of your course, and anyway
only applicable if you have Java 8. If either condition is not met, then skip this part.

So, we have the methods from above, and we have Java 8. Now, drop the whole
ListenerInnerclass, and do this in the constructor:



/*****************************************************************/

Greetz,
Piet
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still think it would be better to have four inner classes than one inner class.
 
Eli Kook
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that when I create a variable imaxX and maxY it does not work for some reason beyond my understanding.
 
Piet Souris
Rancher
Posts: 1782
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your length is two meters, and you stand on top of a ladder of 1,5 meters,
then how far is the top of your head from the floor?
Or suppose the ceiling is 2.6 meters high, then how high can your ladder
maximum be for you to stand straight up on that ladder?

Look at what you have. The left edge of your frame has coordinate: currentX,
and the top edge of your frame has coordinate: currentY.
You know the width and heigt of your frame (Dimension frame), so you
can calculate what the coordinate is of the right and bottom edge of your
frame.

Now, suppose that the left edge is currentX, and that the width of the
frame is 'width'. What condition must be met in order for that frame
to not go over the right side of your screen? (remember that you know the
coordinate of the right side of your screen. What is is?)

Think about it. Campbell, in his last but one reply, gave you a numeric example.

By the way: remember that the top left of your JPanel has coordinates (0, 0)
and the bottom right has (screenwidth - 1, screenheight - 1). So the positive
x direction is going to the right, and the positive y direction is going from
top to bottom.

Greetz,
Piet
 
Eli Kook
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, sorry to my late reply was a bit busy.
I ended up with this code and it works well, I wanna thank you for your patient and explanations.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!