0

Is there any way to create a custom component in swing. By custom I mean say right now I am able to create a circle and do actions like dragging it etc.

But now I also want that along with the circle a text label with its number is also present. Can we combine them into a new type of component where say we can do actions on it collectively?

If yes please give me pointers on how to do so.

jzd
  • 23,473
  • 9
  • 54
  • 76

2 Answers2

0

Yes, just extend JComponent and handle painting and interaction however you want. If there is a component that already does most of what you want you could extend that class and tweak it slightly.

I would suggest reading some of the tutorials about how JComponent works: http://download.oracle.com/javase/tutorial/uiswing/components/jcomponent.html

Here is a similar question but I am sure you can find more specific ones that have been asked once you get deeper into it. How to create a custom Swing Component

The Swing tutorial as a whole if very useful to understand how other components work etc. http://download.oracle.com/javase/tutorial/uiswing/components/index.html

Community
  • 1
  • 1
jzd
  • 23,473
  • 9
  • 54
  • 76
  • Like I want to have a label associated with a circle then for that too I would need to extend jcomponent ??? Or is there any other method?? –  Mar 21 '11 at 13:00
  • @user, yes, no other method. (As a side note, you don't need to repeat punctuation marks) – jzd Mar 21 '11 at 13:20
0

You can create your own Swing component extending JComponent. Several question here on Stack on the topic like this one. Personally, I advise you to get this book: Java Swing as it contains an excellent guide on how to create Swing components.

Community
  • 1
  • 1
JoeSlav
  • 4,479
  • 4
  • 31
  • 50