JComponent: getTransferHandler() : JComponent « javax.swing « Java by API






JComponent: getTransferHandler()

  
/**
 * Demonstrate various aspects of Swing "data transfer".
 * @author Ian Darwin, http://www.darwinsys.com
 * @author Jonathan Fuerth, http://www.SQLPower.ca
 */       
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.TransferHandler;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class MainClass extends JFrame {
  public static void main(String[] args) {
    new MainClass().setVisible(true);
  }

  private JTextField tf;

  private JLabel l;

  private JComboBox propertyComboBox;

  public MainClass() {
    Container cp = new Box(BoxLayout.X_AXIS);
    setContentPane(cp);
    JPanel firstPanel = new JPanel();
    propertyComboBox = new JComboBox();
    propertyComboBox.addItem("text");
    propertyComboBox.addItem("font");
    propertyComboBox.addItem("background");
    propertyComboBox.addItem("foreground");
    firstPanel.add(propertyComboBox);
    cp.add(firstPanel);
    cp.add(Box.createGlue());

    tf = new JTextField("Hello");
    tf.setForeground(Color.RED);
    tf.setDragEnabled(true);
    cp.add(tf);

    cp.add(Box.createGlue());

    l = new JLabel("Hello");
    l.setBackground(Color.YELLOW);
    cp.add(l);

    cp.add(Box.createGlue());

    JSlider stryder = new JSlider(SwingConstants.VERTICAL);
    stryder.setMinimum(10);
    stryder.setValue(14);
    stryder.setMaximum(72);
    stryder.setMajorTickSpacing(10);
    stryder.setPaintTicks(true);

    cp.add(stryder);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500, 300);

    setMyTransferHandlers((String) propertyComboBox.getSelectedItem());

    MouseListener myDragListener = new MouseAdapter() {
      public void mousePressed(MouseEvent e) {
        JComponent c = (JComponent) e.getSource();
        TransferHandler handler = c.getTransferHandler();
        handler.exportAsDrag(c, e, TransferHandler.COPY);
      }
    };
    l.addMouseListener(myDragListener);

    propertyComboBox.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ce) {
        JComboBox bx = (JComboBox) ce.getSource();
        String prop = (String) bx.getSelectedItem();
        setMyTransferHandlers(prop);
      }
    });

    tf.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        JTextField jtf = (JTextField) evt.getSource();
        String fontName = jtf.getText();
        Font font = new Font(fontName, Font.BOLD, 18);
        tf.setFont(font);
      }
    });

    stryder.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent evt) {
        JSlider sl = (JSlider) evt.getSource();
        Font oldf = tf.getFont();
        Font newf = oldf.deriveFont((float) sl.getValue());
        tf.setFont(newf);
      }
    });
  }

  private void setMyTransferHandlers(String s) {
    TransferHandler th = new TransferHandler(s);
    tf.setTransferHandler(th);
    l.setTransferHandler(th);
  }
}

           
         
    
  








Related examples in the same category

1.JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
2.JComponent.WHEN_IN_FOCUSED_WINDOW
3.JComponent: addPropertyChangeListener(PropertyChangeListener listener)
4.JComponent: addVetoableChangeListener(VetoableChangeListener listener)
5.JComponent: getAccessibleContext()
6.JComponent: getActionMap()
7.JComponent: getHeight()
8.JComponent: getInputMap()
9.JComponent: getWidth()
10.JComponent: getX()
11.JComponent: getY()
12.JComponent: putClientProperty(Object key, Object value)
13.JComponent: requestFocusInWindow()
14.JComponent: registerKeyboardAction(ActionListener anAction, KeyStroke aKeyStroke, int aCondition)
15.JComponent: revalidate()
16.JComponent: setComponentPopupMenu(JPopupMenu popup)
17.JComponent: setFont(Font font)
18.JComponent: setFocusTraversalKeys(int arg0, Set arg1)
19.JComponent: setInputMap(int condition, InputMap map)
20.JComponent: setInputVerifier(InputVerifier inputVerifier)
21.JComponent: setOpaque(boolean isOpaque)
22.JComponent: setTransferHandler(TransferHandler newHandler)