Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

Should I extends JComponent?

Thread view: 
Stupid Dustbin - 15 Jul 2007 14:34 GMT
Hi, I'm currently doing an application which users drag and drop
graphical items on a canvas and drag them around the canvas. I have
always wondered if I should extends JComponent or should I not to for
this task.

I don't know much about JComponent though.

My current implementation is to just make the canvas (a JPanel)
implements MouseMotionListener and Mouse Listener, and keep a list of
items on the canvas and when my mouse is pressed, it just loop through
the list of items and check if it is selected and set it as the
selected item so that when I drag my mouse, the selected item move
too.

Didn't make my item class extends JComponent in this implementation.
The application is working fine but I decide to ask about this since
it is something I thought for a long time and my school teacher say I
probably should extends JComponent which I'm not sure why.
Tom Hawtin - 15 Jul 2007 15:53 GMT
> My current implementation is to just make the canvas (a JPanel)
> implements MouseMotionListener and Mouse Listener, and keep a list of
> items on the canvas and when my mouse is pressed, it just loop through
> the list of items and check if it is selected and set it as the
> selected item so that when I drag my mouse, the selected item move
> too.

If you look at the source of JPanel (src.zip in the JDK) you can see
that there are only a few differences:

 * Defaults to FlowLayout.
     - Not a big deal.

 * Defaults to double buffered.
     - Because components will use ancestor's buffering, this is almost
certainly irrelevant.

 * The accessibility role changes to panel.
     - Probably the wrong thing.

 * Sets the PL&F to PanelUI.
     - This is the big problem with JPanel. It becomes highly
unpredictable. For instance, it is often claimed the JPanel defaults to
being opaque, but in fact this depends upon which version of which PL&F
happens to be running.

So, I strongly suggest moving to JComponent.

Tom Hawtin
Stupid Dustbin - 15 Jul 2007 16:34 GMT
> > My current implementation is to just make the canvas (a JPanel)
> > implements MouseMotionListener and Mouse Listener, and keep a list of
[quoted text clipped - 25 lines]
>
> Tom Hawtin

So you mean my canvas class extends JComponent? or my item class
should extend JComponent?
Roedy Green - 15 Jul 2007 19:35 GMT
On Sun, 15 Jul 2007 06:34:25 -0700, Stupid Dustbin
<students.dumping.ground@gmail.com> wrote, quoted or indirectly quoted
someone who said :

>I don't know much about JComponent though.
>
[quoted text clipped - 4 lines]
>selected item so that when I drag my mouse, the selected item move
>too.

Actually you likely already know a lot more about JComponent than you
think. Nearly all the swing classes derive from it.  It is just any
component stripped back to methods common to all JComponents.

If you can pull off what you want with a layout, then extend a JPanel.
If not, and you have to get down to pixels, then do your work in
JComponent.paintComponent

See http://mindprod.com/jgloss/paintcomponent.html
http://mindprod.com/jgloss/paint.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Stupid Dustbin - 16 Jul 2007 04:41 GMT
Hi,

It seem to me that what I'm trying to say is misunderstood. My
question is not really on the canvas should extends JPanel or
JComponent but rather should the item class extends JComponent?

Current Implementation:

class SomeLameCanvas extends JPanel/JComponent implements
MouseListener, MouseMotionListener{

List<Item> itemsList;
Item selectedItem;

protected void paintComponent() {

}

public void mousePressed(MouseEvent e) {

   for (Itemitem : itemsList) {
       if (item.isSelected()) {
           selectedItem = item;
       }
   }

   //if it is right click, remove item instead.

}

public void mouseDragged(MouseEvent e) {
//code to drag the selected item around the canvas.

}

My question is should I consider making my Item class extends
JComponent? and make it kind of event driven as suggested by a
teacher? That the part I'm unsure of.

Thanks!
cyprian - 16 Jul 2007 12:05 GMT
On Jul 16, 5:41 am, Stupid Dustbin <students.dumping.gro...@gmail.com>
wrote:
> Hi,
>
[quoted text clipped - 36 lines]
>
> Thanks!

You have two alternatives, either inherit JComponent and place your
ItemClass in a JPanel or inherit the specific subclass of JComponent
and that will give you other functionalities that you need for that
item that is selected. I believe the latter option is better.
Daniel Pitts - 16 Jul 2007 21:07 GMT
> On Jul 16, 5:41 am, Stupid Dustbin <students.dumping.gro...@gmail.com>
> wrote:
[quoted text clipped - 44 lines]
> and that will give you other functionalities that you need for that
> item that is selected. I believe the latter option is better.

Specifically, if you need to place an Image at a specific location in
your JPanel, you can use a JLabel with the icon property set to the
ImageIcon representing your Image.

You can then attach any Mouse(Motion)Listener you want on the JLabel.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.