简体   繁体   中英

Java TransferHandler with Jbuttons to update Jbutton Text

My question is on how do I go about know what the text of the drag and drop location is. This is current working code.

     gameCell.addMouseListener(new MouseAdapter() {
         public void mousePressed(MouseEvent e){
             JButton button = (JButton)e.getSource();

             int currentNumber = Integer.parseInt(button.getText());

             TransferHandler handle = button.getTransferHandler();
             handle.exportAsDrag(button, e, TransferHandler.COPY);

So the idea is there is a gameboard which is just a bunch of cells (all JButtons), one large table. When I drag one cell to another then the dragged cell's value will become the clicked cell's value, so therefore how do I tell the original value of the JButton cell before it is copied over by the dragged cell.

If you are just trying to "copy" the text from one button to another then you can use code like the following:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DragIcon extends JPanel
{
    public DragIcon()
    {
        TransferHandler iconHandler = new TransferHandler( "icon" );
        MouseListener dragListener = new DragMouseAdapter();

        JLabel label1 = new JLabel("Label1");
        label1.setTransferHandler( iconHandler );
        label1.addMouseListener(dragListener);
        label1.setIcon( new ImageIcon("copy16.gif") );

        JLabel label2 = new JLabel("Label2");
        label2.setTransferHandler( iconHandler );
        label2.addMouseListener(dragListener);

        add( label1 );
        add( label2 );
    }

    private class DragMouseAdapter extends MouseAdapter
    {
        public void mousePressed(MouseEvent e)
        {
            JComponent c = (JComponent)e.getSource();
            TransferHandler handler = c.getTransferHandler();
            handler.exportAsDrag(c, e, TransferHandler.COPY);
//          handler.exportAsDrag(c, e, TransferHandler.MOVE);
        }
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("Drag Icon");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new DragIcon());
        frame.setLocationByPlatform( true );
        frame.setSize(200, 100);
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

The default TransferHandler allows you to specify a property that you want to copy. In my example I'm copying the Icon. In your case you would use:

TransferHandler iconHandler = new TransferHandler( "text" );

to copy the text.

Note in my example I also tried to "move" the Icon from one label to another but it doesn't work. I'm not sure what needs to be changed to move a property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM