0

I'm trying to 'embed' an existing Java app into my own application. The existing app is a JFrame with a number of components added to it. I copied the code into my project and replaced the JFrame with a new JPanel that I then added to my own app.

The problem is everything painted in the JPanel appears to be shifted up by a small amount (e.g. ~20px). I wondered if it's anything to do with window menubars etc but so far haven't been able to find a solution apart from manually adding an offset when painting (definitely not ideal!).

I know without the code it can be difficult to offer helpful suggestions, but what I want to know is, do the JFrame and JPanel behave differently in a way that would cause this? Or is there anything else obvious to fix it?

user2272952
  • 81
  • 10
  • 3
    I have a pony, guess what color it is - Without some kind of runnable example which demonstrates your problem, we'd only be guessing at what color your pony is, which isn't going to help anybody :( - And no, there's nothing "obvious" which would cause this – MadProgrammer Jul 07 '17 at 04:23
  • 1
    @MadProgrammer That's an unfair question. The pony is a Unicorn and it's rainbow colored. – Andrew Thompson Jul 07 '17 at 04:25
  • 2
    @AndrewThompson Damn, you know me to well – MadProgrammer Jul 07 '17 at 04:28

1 Answers1

0

One solution is to create an empty border. Try something like this:

panelName.setBorder(BorderFactory.createEmptyBorder(20, 0, 0, 0));

This will shift everything in the JPanel down 20 pixels, and it should solve your problem. Try adjusting the number of pixels until you find the center.

Take a look at this post.

  • *"This is a problem which JPanels have"* - Since when? I've never had a panel offset itself without me doing something to warrant it. So, instead of actually fixing the issue, you're just suggesting to apply a "hack" to correct for whatever the real, underlying issue is? Talk about using a bandaid to cover a hemorrhage – MadProgrammer Jul 07 '17 at 05:03
  • @MadProgrammer I have come across this problem many times, and this fixed my problem. It's only a cut. Perfect for a band-aid. –  Jul 07 '17 at 05:05
  • If you've come "across this issue many times" then you're doing something wrong – MadProgrammer Jul 07 '17 at 05:11
  • @MadProgrammer I'm not doing anything "wrong." There's nothing harmful about it. It just happens when you set the label too small to fit the text or image normally –  Jul 07 '17 at 05:13
  • NEVER had this kind of issue - and there are a lot of assumptions associated with the question – MadProgrammer Jul 07 '17 at 05:14
  • @MadProgrammer I have edited my answer. There is NOTHING false about it, and it will solve the OP's problem. –  Jul 07 '17 at 05:19
  • It's still hack around the base problem, if the linked answer is a hack – MadProgrammer Jul 07 '17 at 05:20