what's wrong with my component?

i make a new component which is a dragable icon here is my piece of code

//class: DragAbleComponent.the father of my component

import java.awt.Graphics;

import java.awt.event.FocusEvent;

import java.awt.event.FocusListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;

import javax.accessibility.Accessible;

import javax.swing.JComponent;

public abstract class DragAbleComponent extends JComponent implements MouseListener,

FocusListener, Accessible ,MouseMotionListener{

private int X;

private int Y;

/**

*

*/

private static final long serialVersionUID = 1L;

public void mouseClicked(MouseEvent e) {

this.requestFocusInWindow();

}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

public void focusGained(FocusEvent e) {

this.repaint();}

public void focusLost(FocusEvent e) {

this.repaint();}

protected abstract void paintComponent(Graphics graphics);

public int getX() {

return X;

}

public void setX(int x) {

X = x;

}

public int getY() {

return Y;

}

public void setY(int y) {

Y = y;

}

}

//class:DragableIcon the component

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.event.InputEvent;

import java.awt.event.MouseEvent;

import javax.swing.Action;

import javax.swing.ActionMap;

import javax.swing.InputMap;

import javax.swing.JComponent;

import javax.swing.KeyStroke;

import javax.swing.TransferHandler;

public class DragableIcon extends DragAbleComponent {

private Image image;

private MouseEvent firstMouseEvent = null;

private static boolean installKeyBordMapings = true;

/**

*

*/

private static final long serialVersionUID = 1L;

public DragableIcon(Image image) {

super();

this.image = image;

this.setFocusable(true);

this.addMouseListener(this);

this.addFocusListener(this);

this.addMouseMotionListener(this);

this.initKeyBordMaping();

}

private void initKeyBordMaping(){

if (installKeyBordMapings) {

InputMap imap = this.getInputMap();

//imap.put(KeyStroke.getKeyStroke("ctrl X"),

//TransferHandler.getCutAction().getValue(Action.NAME));

imap.put(KeyStroke.getKeyStroke("ctrl C"),

TransferHandler.getCopyAction().getValue(Action.NAME));

imap.put(KeyStroke.getKeyStroke("ctrl V"),

TransferHandler.getPasteAction().getValue(Action.NAME));

}

ActionMap map = this.getActionMap();

//map.put(TransferHandler.getCutAction().getValue(Action.NAME),

//TransferHandler.getCutAction());

map.put(TransferHandler.getCopyAction().getValue(Action.NAME),

TransferHandler.getCopyAction());

map.put(TransferHandler.getPasteAction().getValue(Action.NAME),

TransferHandler.getPasteAction());

}

@Override

protected void paintComponent(Graphics graphics) {

Graphics g = graphics.create();

//Draw in our entire space, even if isOpaque is false.

g.setColor(Color.WHITE);

g.fillRect(0, 0, image == null ? 125 : image.getWidth(this),

image == null ? 125 : image.getHeight(this));

if (image != null) {

//Draw image at its natural size of 125x125.

g.drawImage(image, 0, 0, this);

}

//Add a border, red if picture currently has focus

if (isFocusOwner()) {

g.setColor(Color.RED);

} else {

g.setColor(Color.BLACK);

}

g.drawRect(0, 0, image == null ? 125 : image.getWidth(this),

image == null ? 125 : image.getHeight(this));

// g.drawImage(image, 0, 0, this);

g.dispose();

}

public void mouseDragged(MouseEvent e) {

//Don't bother to drag if the component displays no image.

if (image == null) return;

if (firstMouseEvent != null) {

e.consume();

//If they are holding down the control key, COPY rather than MOVE

int ctrlMask = InputEvent.CTRL_DOWN_MASK;

int action = ((e.getModifiersEx() & ctrlMask) == ctrlMask) ?

TransferHandler.COPY : TransferHandler.MOVE;

int dx = Math.abs(e.getX() - firstMouseEvent.getX());

int dy = Math.abs(e.getY() - firstMouseEvent.getY());

//Arbitrarily define a 5-pixel shift as the

//official beginning of a drag.

if (dx > 5 || dy > 5) {

//This is a drag, not a click.

JComponent c = (JComponent)e.getSource();

TransferHandler handler = c.getTransferHandler();

//Tell the transfer handler to initiate the drag.

handler.exportAsDrag(c, firstMouseEvent, action);

firstMouseEvent = null;

}

}

}

public void mouseMoved(MouseEvent e) {}

public void mousePressed(MouseEvent e) {

//Don't bother to drag if there is no image.

if (image == null) return;

firstMouseEvent = e;

e.consume();

}

public void mouseReleased(MouseEvent e) {

firstMouseEvent = null;

}

public static boolean isInstallKeyBordMapings() {

return installKeyBordMapings;

}

public static void setInstallKeyBordMapings(boolean installKeyBordMapings) {

DragableIcon.installKeyBordMapings = installKeyBordMapings;

}

public Image getImage() {

return image;

}

public void setImage(Image image) {

this.image = image;

}

}

//and here is my pane

import java.awt.BorderLayout;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

import javax.swing.ImageIcon;

import javax.swing.JPanel;

import javax.swing.JToolBar;

import cn.com.ynld.bms.applet.gui.handler.IconTransferHandler;

import cn.com.ynld.bms.applet.service.TransfAble;

import cn.com.ynld.bms.applet.service.UpdateAble;

public class ElectronicPane extends JPanel

implements ItemListener,UpdateAble{

private UpdateAble updateAble = null;

private ScrollAbleImage image;

private JToolBar toolBar;

private IconTransferHandler handler;

/**

*

*/

private static final long serialVersionUID = 1L;

public ElectronicPane(){

super(new BorderLayout());

handler = new IconTransferHandler();

this.creatToolBar();

this.add(this.toolBar);

this.setTransferHandler(handler);

}

public void itemStateChanged(ItemEvent e) {}

public void addTransferAble(String key, TransfAble transferAble) {

// TODO

}

public TransfAble getTransferAble(String key) {

// TODO

return null;

}

public void removeTransferAble(String key) {

this.updateAble.removeTransferAble(key);

}

public void update(UpdateAble updateAble) {

this.updateAble = updateAble;

this.init();

}

private void init(){

this.image = new ScrollAbleImage();

this.image.update(updateAble);

this.add(image,BorderLayout.CENTER);

this.repaint();

//this.image.repaint();

}

private void creatToolBar(){

this.toolBar = new JToolBar();

DragableIcon alarm = new DragableIcon(createImageIcon("/root/Desktop/drawing/png-0003.png", "alarm").getImage());

alarm.setTransferHandler(handler);

DragableIcon damage = new DragableIcon(createImageIcon("/root/Desktop/drawing/png-0007.png", "device").getImage());

alarm.setTransferHandler(handler);

this.toolBar.add(alarm);

this.toolBar.addSeparator();

this.toolBar.add(damage);

this.toolBar.setFloatable(false);

}

/** Returns an ImageIcon, or null if the path was invalid. */

protected static ImageIcon createImageIcon(String path,

String description) {

// java.net.URL imageURL = DragPictureDemo.class.getResource(path);

if (path == null) {

System.err.println("Resource not found: "

+ path);

return null;

} else {

return new ImageIcon(path, description);

}

}

}

the problem is i add DragableIcons to my toolbar but only the first icon can be seen!!!!

what's wrong

thanks for any help!

[8537 byte] By [dxuranua] at [2007-10-3 2:45:54]
# 1
to start off with it's not in code tags.
morgalra at 2007-7-14 20:34:34 > top of Java-index,Security,Cryptography...
# 2

what's wrong

private void creatToolBar() {

this.toolBar = new JToolBar();

ImageIcon alarm = createImageIcon(

"/root/Desktop/drawing/png-0003.png",

"alarm");

//alarm.setTransferHandler(handler);

// register icon with its Action

alarmAction.putValue(Action.SMALL_ICON, alarm);

ImageIcon damage = createImageIcon(

"/root/Desktop/drawing/png-0007.png",

"device");

//damage.setTransferHandler(handler);

// set the icon to the Action that will use it

damageAction.putValue(Action.SMALL_ICON, damage);

// toolBars are equipped to receive an Action or JComponent

// they don't know what to do with ImageIcons

this.toolBar.add(alarmAction);

this.toolBar.addSeparator();

this.toolBar.add(damageAction);

this.toolBar.setFloatable(false);

}

/** Returns an ImageIcon, or null if the path was invalid. */

protected static ImageIcon createImageIcon(String path, String description) {

// Use the containing class name. This works for static context in

// which the "this" keyword does not work.

// Otherwise, you could simply use getResource.

java.net.URL imageURL = ElectronicPane.class.getResource(path);

if (path == null) {

System.err.println("Resource not found: " + path);

return null;

} else {

return new ImageIcon(path, description);

}

}

private Action alarmAction = new AbstractAction() {

public void actionPerformed(ActionEvent e) {

Toolkit.getDefaultToolkit().beep();

}

};

private Action damageAction = new AbstractAction() {

public void actionPerformed(ActionEvent e) {

System.out.println("damage action");

}

};

74philipa at 2007-7-14 20:34:34 > top of Java-index,Security,Cryptography...
# 3
Perhaps I could persuade you to include a short description telling us what it is that you want it to do and it is not doing. A list of error codes would also help.
morgalra at 2007-7-14 20:34:34 > top of Java-index,Security,Cryptography...
# 4
thanks for replay,i hava fixed the problem. by the way can i make a jcomponent by myself and place it on the toolbar?or if any i must notice?thanks agin.
dxuranua at 2007-7-14 20:34:34 > top of Java-index,Security,Cryptography...