Should I extends JComponent?

S

Stupid Dustbin

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.
 
T

Tom Hawtin

Stupid said:
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
 
S

Stupid Dustbin

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

So you mean my canvas class extends JComponent? or my item class
should extend JComponent?
 
R

Roedy Green

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.

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
 
S

Stupid Dustbin

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!
 
C

cyprian

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!

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.
 
D

Daniel Pitts

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top