• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to stop a JFrame from sticking to the top/north of the screen.  RSS feed

 
Peter Lock
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I hope someone out there can help me with a problem. I have MainFrame which extends JFrame; when the program loads the MainFrame window is drawn and sticks to the top of the screen, that is to say, on  load beings north, after clicking and dragging the frame to the bottom of the screen (and releasing the mouse) it slingshots straight to the top (north) again. I know I did something several months ago to make this happen but can't remember what. Does anyone have an idea what might be causing this action and how to fix it? The window uses a box layout and contains a frame which calls other building block objects for my project module.

Best Regards
 
Peter Lock
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone - found the problem - should have included:         frame.setLocationByPlatform(true);

Regards
 
Peter Lock
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry- my mistake - I had the wrong window open - the problem still hasn't been resolved.
Any help would be appreciated
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post your SSCCE that demonstrates the problem.

That is just create a frame with no components and see if you have the problem. (I doubt you will since I have never seen this problem is 15 years using Swing).

Then you add a couple of components and see if you still have the problem. Once the problem reappears then you know what you last changed so you can provide us with more information.

 
Peter Lock
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob - Thanks for taking a look. Here are the cut down classes. Its not pretty but it demonstrates the problem.
Thanks again:

The code:


 
Peter Lock
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An observation - the problem occurs when the screen starts at 1020. The program should start set at the minimum width 1020, then the user can expand it if they choose. After expanding the screen the problem stops.
 
Charles Nduka
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really don't know exactly what caused this problem but i noticed you initiated "setLocationByPlatform(false);"
why don't you try replacing it with "setLocationRelativeTo(null);" in the MainFrame constructor.
 
Peter Lock
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Charles - Thanks for the suggestion. I made the change but the problem remains. Regards
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  Its not pretty but it demonstrates the problem


Well the code you posted contains two main() method. What is the point on the TabPane class and related code.

The point of a SSCCE is to make the code simple and self explanatory. We should not be spending any time guessing what that code if for. If it is not needed to demonstrate the problem then don't include it. Don't confuse us with unnecessary code.

I am using JDK8 on Windows 7. I don't see any problems. The frame is displayed at location (500, 500) and can freely drag the frame around and it remains where I drag it. Is this a version/platform issue.



I also stated you should start with a simple frame and then add back in code. The above two statements are NOT needed to display a simple frame.

What happens when you remove them?

Is the problem that you are trying to assign a height equal to the height of your window so the frame is always moving to the top?

This is a simple way to debug. Remove all the completely unnecessary code and see what happens.
 
Peter Lock
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program structure - mainframe->tabpane->each tab calls its own class module (displaying different screens of the program to the user). The TabPane is integral.

The point of the TabPane class and related code is that I have narrowed down the problem to these parts of the program. By the very definition of the word problem, they have been included as I cannot see how they might be interfering with the Frames location, its a problem Rob. The entire program is quite large, each class does have a main method, used to test and debug further without loading everything each time. The SSCCE as you mentioned is subjective to what is simple and needed to understand a problem in its complete nature to the largest audience of the community. I disagree with you that the extra code is not needed. If I knew exactly where the problem was then I wouldn't post the extra code. As for : 



not being needed, and you asking what would happen if removed??? Well on OS X JDK8 the frame DOES NOT get displayed when I remove them. I DID DO what you asked and removed everything and built it back again before posting the code initially, leading me to the TabPane class and no observable problems. As you mentioned, this may very well be a platform issue. It wouldn't be the first nor last between OS X and Windows. Thanks for your time and help .. you have made some good suggestions which have been tried before my initial posting. You said : "
Is the problem that you are trying to assign a height equal to the height of your window so the frame is always moving to the top?
"; I don't know but will look into it more. There could be something here, if it panes out I will post an answer. Thanks again and happy programming
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The SSCCE as you mentioned is subjective to what is simple and needed to understand a problem in its complete nature to the largest audience of the community.


No it is not subjective. Your complaint is that the "MainFrame" sticks to the top of the screen.

In order to display the "MainFrame" you need to invoke the main() method of the  CutDownProblem class.

If you invoke this methen then yes the TabPane is added to the mainframe.

However the showFrame() and main() methods of the TabPane class will never be invoked. Therefore they are irrelevant to the stated problem and should not be included in the SSCCE.

The fact that you have a main() method to test each class independently is irrelevant for creating the SSCCE and only causes confusion when looking at the code.

Well on OS X JDK8 the frame DOES NOT get displayed when I remove them.


Normally you use a frame.pack() or a frame.setSize(...) to give the frame a size. I have never had to use either of the above methods and examples in the Swing tutorial never use those methods to display a frame. I would expect the Swing tutorial examples would work on all platforms.
 
Peter Lock
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob -
Normally you use a frame.pack() or a frame.setSize(...) to give the frame a size.
- Got it, thank-you for pointing that out.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!