-2

I have seen a lot of example code but none that specifically shows me how to display an ImageIcon through the use of a JLabel on a JFrame without using a layout manager. I am repeatedly told that absolute positioning is a hassle and less efficient than a layout manager but I'd prefer complete control over where my elements are going. A full scale example class (with no extra things added) of a JLabel that will display an image on a JFrame with absolute positioning would be much appreciated, thank you

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
nicknicknick111
  • 69
  • 1
  • 2
  • 12
  • 1
    Pixel perfect positioning are an illusion in modern/professional UI development. You don't have any control over the rendering processes which can change the required size of the components, meaning you will need to basically reinvent the layout manager api in order to achieve the results you are ultimately after. This is, of course your choice and you're free to ignore it, but with a combined total of over 40 years of professional software development among us, this is why we've told you should use layout managers – MadProgrammer Apr 03 '15 at 21:37
  • When you say rendering processes, what exactly are you referring to? I know that resizing the JFrame may screw things up but I plan on my application being full screen. The image I need has to move around the screen thus I don't want to essentially "pin" it to a specific location of the window. Which layout would you prefer then for an image that, say I wanted to move around with arrow keys on the screen? – nicknicknick111 Apr 03 '15 at 21:41
  • 1
    Java/Swing uses the OS's rendering pipeline, DirectX/OpenGL on Windows, OpenGL on linux and Apples own on Mac. A single font will be rendered differently on each platform (for example), requiring more or less space on each platform. *"Which layout would you prefer then for an image that, say I wanted to move around with arrow keys on the screen?"* - That's not nearly enough context available to make suggestion, but personally, I'd prefer to use custom painting – MadProgrammer Apr 03 '15 at 21:47
  • I think this is a situation in which the code should be doing custom painting. In that case layouts become irrelevant. – Andrew Thompson Apr 04 '15 at 01:25

1 Answers1

2

The image I need has to move around the screen

An important piece of information missing in the original question. I don't have a problem using a null layout for this.

I wanted to move around with arrow keys on the screen?

And don't use a KeyListener for this. Instead you should be using Key Bindings.

See the KeyboardAnimation.java code found in Motion Using the Keyboard for one solution to this problem.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • That's for the tip on key binding, I think I'm starting to understand. I still need an example code of an absolute positioned jlabel that shows up in a JFrame since I can't seem to get it to work, I'd post my own code but it's so far off that I don't think it would help. – nicknicknick111 Apr 04 '15 at 00:47
  • @nicknicknick111, `I still need an example code of an absolute positioned jlabel` - ??? that is what the code does in all three examples. – camickr Apr 04 '15 at 01:35
  • The code shows the differences between binding keys and KeyListeners, not absolute positioned labels on a JFrame. I'm glad for all the extra info but this was really what my question was asking for – nicknicknick111 Apr 04 '15 at 02:44
  • @nicknicknick111, `The code shows the differences between binding keys and KeyListeners` yes. `not absolute positioned labels on a JFrame.` - how do you think the label gets moved when you use one of the keys? – camickr Apr 04 '15 at 03:21
  • I understand that I need absolute positioning, and that key binding is the best way to move the jlabel, but I can't figure out how to actually get the JLabel on the screen in the first place. It won't render for me and show up, hence my asking for example code – nicknicknick111 Apr 04 '15 at 03:24
  • @nicknicknick111, `hence my asking for example code` I gave you working code. The link contains 3 examples of working code!!! Did you not even bother to read the link??? – camickr Apr 04 '15 at 05:22
  • I have looked at link, and read the example code. The code demonstrates KeyListeners and key bindings. This information you have is great, but again, I need to know how to get the jlabel itself working. I am asking for code that will make an image pop up on a JFrame via the use of a jlabel. – nicknicknick111 Apr 04 '15 at 05:52
  • @nicknicknick111, and this is exactly what the code does. Before you can move the label around the screen you must e first add the image to the panel. The code initially displays the label at (100, 100), so your question has been answerd. The KeyListener or Key Binding code has absolutely nothing to do with initially displaying the label. That code is only used to move the label around the panel., so just comment out the line that "adds motion support" and you have your simple example. – camickr Apr 04 '15 at 16:04