1 Kasım 2018 Perşembe

Nümerik Literal

Float
Açıklaması şöyle. Literal sonuna f veya F harfi gelir.
A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise, its type is double and it can optionally be suffixed with an ASCII letter D or d.
Örnek
Şöyle yaparız.
float value = 19.5f;
Örnek
Şöyle yaparız.
Foo.bar(11.5f, 11.5f );
Şu kod ile aynıdır.
float a = 11.5;
float b = 11.5;
Foo.bar(a, b)
Double
Şöyle yaparız.
Foo.bar(11.5, 11.5));
Şu kod ile aynıdır.
double a = 11.5;
double b = 11.5;
Foo.bar (a, b)
Octal Literal
Açıklaması şöyle.
An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 interspersed with underscores, and can represent a positive, zero, or negative integer.
Şöyle yaparız.
int g = 014;
Literal Okumayı Kolaylaştırmak
Java 7'den itibaren rakamlar arasına okumayı kolaylaştırma amaçlı _ (underscore) karakteri konulabiliyor.
Örnek
Şöyle yaparız.
int i;
//Java 7 allows underscore in integer
i=3455_11_11;

Swing TransferHandler Sınıfı

Giriş
Şu satırı dahil ederiz.
import javax.swing.TransferHandler;
JTree
JTree hedef nesne ise önce drop mode değerini atamak gerekir. Açıklaması şöyle. Böylece üzerinde drop olunca yeni bir düğüm mü eklenecek, mevcut olan mı güncellenecek belirtiriz.
When enabling drop on a component, such as a list, you need to decide how you want the drop location to be interpreted. For example, do you want to restrict the user to replacing existing entries? Do you want to only allow adding or inserting new entries? Do you want to allow both? To configure this behavior, the JList class provides the setDropMode method which supports the following drop modes.
Sonra JTree için TransferHandler nesnesini atamak gerekir. Açıklaması şöyle. Böylece gelen veriyi kabul edebilir.
As mentioned previously, the default Swing transfer handlers, such as those used by text components and the color chooser, provide the support considered to be most useful for both importing and exporting of data. However list, table, and tree do not support drop by default. The reason for this is that there is no all-purpose way to handle a drop on these components. For example, what does it mean to drop on a particular node of a JTree? Does it replace the node, insert below it, or insert as a child of that node? Also, we do not know what type of model is behind the tree — it might not be mutable.

While Swing cannot provide a default implementation for these components, the framework for drop is there. You need only to provide a custom TransferHandler that manages the actual import of data.
Şöyle yaparız.
JTree tree = new JTree();
tree.setDragEnabled(true);
tree.setDropMode(DropMode.ON_OR_INSERT);
tree.setTransferHandler(new MyTreeTransferHandler());
Export 
Export işlemi için şu metodlar kullanılır.
getSourceActions()
createTransferable()
exportDone()

Import 
Import işlemi için şu metodlar kullanılır.Bu metodlar TransferSupport sınıfını parametre olarak alırlar.

canImport()
importData()

exportAsDrag metodu
Drag drop işlemini desteklemeyen bir sınıfa bu yeteneğin eklenebilmesini sağlar. Açıklaması şöyle
...when the mouse is pressed, the transfer handler initiates the drag from the label by invoking exportAsDrag with the Copy argument:
MouseMotionAdapter veya MouseAdapter arüyüzünü gerçekleştiren bir sınıf eklenir. Bu sınıfın mouseDragged metodunda exportAsDrag () çağrısı yapılır.

Örnek
Şöyle yaparız.
class DragPanel extends JPanel {
  public DragPanel() {
    super();
  }
  public JLabel draggingLabel;
}
class Handler extends MouseAdapter {
  @Override public void mousePressed(MouseEvent e) {
    DragPanel p = (DragPanel)e.getSource();
    Component c = SwingUtilities.getDeepestComponentAt(p, e.getX(), e.getY());
    if(c!=null && c instanceof JLabel) {
      p.draggingLabel = (JLabel)c;
      p.getTransferHandler().exportAsDrag(p, e, TransferHandler.MOVE);
    }
  }
}

Swing TransferHandler.TransferSupport Sınıfı

getDropLocation metodu
Şöyle yaparız.
JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
geTransferable metodu
Şöyle yaparız.
Transferable transferable = support.getTransferable();
isDataFlavorSupported metodu
String olup olmadığını öğrenmek için şöyle yaparız.
if (!support.isDataFlavorSupported(DataFlavor.stringFlavor) {
  return false;
}
isDrop metodu
Clipboard paste istemiyorsak şöyle yaparız.
if (!support.isDrop()) {
  return false;
}