3

I'd like to create a custom swing component such as a desktop widget which does not require a JFrame (or to extend it) to be printed on the screen.

I don't want to extend the JFrame because my component is really simple and JFrame implements a lot of functionality I dont need.

Who do I start? Which class should I extend?

Many Thanks

EDIT ---------------------------------------

Thanks guys!

I'll check the references you sent. Also, is it possible to java to draw on the screen without swing API?

Mikhas
  • 851
  • 1
  • 12
  • 31
  • 3
    What functionality don't you need? You can invoke [`setUndecorated(true)`](http://download.oracle.com/javase/6/docs/api/java/awt/Frame.html#setUndecorated%28boolean%29) on your `JFrame`, but regardless, you'll need a container object to render your components with! – mre Jun 27 '11 at 16:02
  • Toolbars, menubar, child components, dragging ... – Mikhas Jun 27 '11 at 16:07
  • @Mikhas, Then it sounds like invoking the method on your `JFrame` will do the job, although I don't understand the "child components" part...Anyway, if you wanted to add more customization to your frame, consider reading [How to Create Translucent and Shaped Windows](http://java.sun.com/developer/technicalArticles/GUI/translucent_shaped_windows) – mre Jun 27 '11 at 16:10
  • 3
    Here's an [example](http://stackoverflow.com/questions/2163544/re-paint-problem-on-translucent-frame-panel-component/2166500#2166500). – trashgod Jun 27 '11 at 16:11
  • @trashgod, +1, good ole Java2D...very cool!:) – mre Jun 27 '11 at 16:12
  • +1 on trashgod comment :) for referencing my answer. I love this screenshot: http://img36.imageshack.us/img36/9195/capturadepantalla201002.png – OscarRyz Jun 27 '11 at 16:34
  • JWindow, if you don't want the button in the taskbar. – bestsss Jun 27 '11 at 17:08

3 Answers3

5

If you don't want the functions a JFrame provides and just want floating graphics then use a JWindow. It does not have window decorations.

Example with plain label in a JWindow:

public static void main(String args[]) {
    JWindow w = new JWindow();
    w.add(new JLabel("Testing a Window!!!!!"));
    w.setLocation(300, 300);
    w.pack();
    w.setVisible(true);
}
jzd
  • 23,473
  • 9
  • 54
  • 76
  • 2
    Um, I think you need a [top-level container](http://download.oracle.com/javase/tutorial/uiswing/components/toplevel.html). – trashgod Jun 27 '11 at 16:19
  • @Trashgod, I assume you could have an invisible Frame as the owner for the Window. I haven't tried it myself though. – jzd Jun 27 '11 at 17:54
  • 1
    @trashgod, you got me curious so I just tried and added an example. The Window does need an owner, but that owner does not need to be visible. – jzd Jun 27 '11 at 17:59
  • @jzd why AWT Containers, is there something specific – mKorbel Jun 27 '11 at 18:23
  • @mKorbel, the question was how to avoid a JFrame. And want to extend/use instead that has less functionality. It is a strange question, but a Window fits the requirements. – jzd Jun 27 '11 at 18:46
  • @jzd for this question is only JDialog answer, how can I foud out I can't see difference between Awt Frame and Swing JFrame, looks like as this topic is nonsence... :-) +1 – mKorbel Jun 27 '11 at 19:33
  • The topic is a little bit nonsence. However, in my example I create a Frame, but just use it as an owner. The Window is the only thing that is set to visible. – jzd Jun 27 '11 at 19:35
  • @mKorbel, looking back at this with a fresh view, I have no idea why I just didn't use a JWindow instead. I have updated my example. Thanks – jzd Jun 28 '11 at 12:26
  • @mKorbel, yea, I just wanted you to know that thinking about it with a clear head made me reconsider your comment about AWT vs Swing. – jzd Jun 28 '11 at 13:55
1

All in all, you'll need to familiarize yourself with Java2D, as demonstrated by @trashgod's comment. In particular, Composting Graphics. And understand that using a top-level container to render Swing components is always required.

NOTE

If @trashgod provides an answer, I'll gladly remove mine and up-vote his accordingly.

mre
  • 43,520
  • 33
  • 120
  • 170
1

Depend on what you need. Probably an undecorated JFrame is all you need. I don't really understand your argument about "all the functionality I don't need", because, at different levels you'll get it anyway, either by another component or by the underlaying OS. You can't get rid of that.

Now addressing your question you can always use a JDialog

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • 1
    JFrame leave a button in the taskbar (uncool); (J)Window is usually the one of choice (imo) – bestsss Jun 27 '11 at 17:09
  • 1
    Yeap, much better than a (J)Window is a `JDialog` wich doesn't leave the button in the taskbar either and still toplevel containter – OscarRyz Jun 27 '11 at 18:20
  • JDialog w/o parent Frame uses a shared swing frame (which can be obtained via `new JDialog().getParent()` ) – bestsss Jun 27 '11 at 18:37
  • It is still not clear what does the OP means with *a lot of functionality I dont need* – OscarRyz Jun 27 '11 at 19:51