0

I am new in AWT and I'm facing a problem. Here is the code sample of my function func(Container container), where container != null:

Canvas embedder = new Canvas();
embedder.setBackground(...);
embedder.setPreferredSize(...);
container.add(embedder);
container.setVisible(true);

Then I am trying to get frameWindow, long and my first action is

ComponentPeer peer = embedder.getPeer();

and I am getting peer == null

Please, tell me why this may happen?

Amaury Medeiros
  • 2,093
  • 4
  • 26
  • 42
tratto
  • 252
  • 2
  • 11

1 Answers1

2

Why do you want the peer? java.awt.peer has been disappeared and wasn't useful anyway. And use Swing, not AWT components.

But it's not there because the [heavyweight] component has not been realised [essentially put on the screen] yet.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
  • Tom's right; presumably, the container that you're passing in to your method isn't yet visible on-screen. That is, it's not the child of a Window or Dialog that has itself been made visible on-screen. On the other hand, of the container HAD belonged to some kind of visible window, then embedder WOULD have a peer, and you'd get a non-null answer. In any event, Tom's first statement is REALLY useful. Think very, very hard about WHY you want the native peer. Then, think again... – Bob Gilmore Dec 08 '11 at 05:45
  • I need to check particulary an AWT component, thats why i am using it. ` JInternalFrame frame = new JInternalFrame("frame", false, false, false, false); frame.setSize(220, 220); func(frame);` this is what i am using before calling my func. so, i can't get what i am missing here to make it work – tratto Dec 08 '11 at 06:59
  • i am not the one who wrote it, i need to fix it and make it work using AWT – tratto Dec 08 '11 at 07:03
  • 1
    +1 for the second statement, which helpfully answered the q. Tempted to downvote for "has been disappeared" (it's long been deprecated, but is still present in the latest release version) and "wasn't useful anyway" - that's a broad claim that contradicts a lot of useful examples. If you can tell me how to do http://stackoverflow.com/questions/9855743/create-child-window-of-another-processs-hwnd-e-g-screensaver-preview without getPeer(), I'll be glad to be schooled! – LarsH Apr 03 '12 at 22:11
  • @LarsH `java.awt.peer` is disappeared in the sense that http://docs.oracle.com/javase/7/docs/api/java/awt/peer/package-summary.html is a 404. – Tom Hawtin - tackline Apr 03 '12 at 23:37
  • @Tom: ok. I should have said "latest recommended version" rather than "latest release", which according to http://www.java.com/en/download/manual.jsp is 1.6.0u31. And getPeer() is still there: http://docs.oracle.com/javase/6/docs/api/java/awt/Component.html#getPeer() – LarsH Apr 04 '12 at 01:53