1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
public class PersoPanel extends JPanel implements MouseMotionListener, MouseListener
{
public PersoPannel()
{
super();
BorderLayout borderLayout = new BorderLayout();
setLayout( borderLayout );
setSize( new Dimension( 500, 18 ) );
addMouseListener(this);
addMouseMotionListener(this);
add( getJPanelLabel(), BorderLayout.CENTER );
}
private JPanel getJPanelLabel()
{
if( jPanelLabel == null )
{
FlowLayout flowLayout = new FlowLayout();
flowLayout.setAlignment( FlowLayout.LEFT );
flowLayout.setHgap( 0 );
flowLayout.setVgap( 5 );
jPanelLabel = new JLabel();
jPanelLabel.setText( "Texte" );
jPanelLabel.setOpaque( true );
}
return jPanelLabel;
}
public void mouseDragged( MouseEvent e )
{
if( firstMouseEvent != null )
{
e.consume();
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() );
if( dx > 5 || dy > 5 )
{
JComponent c = (JComponent) e.getSource();
System.out.println( "panel " + c );
TransferHandler handler = c.getTransferHandler();
handler.exportAsDrag( c, firstMouseEvent, action );
firstMouseEvent = null;
}
}
}
public void mouseMoved( MouseEvent e )
{
}
public void mousePressed( MouseEvent e )
{
System.out.println( "mousePressed" );
firstMouseEvent = e;
e.consume();
}
public void mouseReleased(MouseEvent e)
{
firstMouseEvent = null;
}
} |
Partager