Showing posts with label example. Show all posts
Showing posts with label example. Show all posts

Java Frame Drag and drop example program

Wednesday, 21 November 2012
Dragging requires more physical effort than moving the same pointing device without holding down any buttons. Because of this, a user cannot move as quickly and precisely while dragging. However, drag-and-drop operations have the advantage of thoughtfully chunking together two operands (the object to drag, and the drop location) into a single action. Extended dragging and dropping (as in graphic design) can stress the mousing hand. A design problem appears when the same button selects and drags items. Imprecise movement can cause a dragging when the user just wants to select. Here the JFrame will be used for drag and drop



import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class dragdemoex
{
  public static void main(String[] args)
    {
    JFrame frame = new JFrame("Drag and Drop Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new JPanel());
    JTextField textField = new JTextField(25);
    textField.setText("http://codercheck.blogspot.com/");
    frame.add(textField);

    JTextArea textArea = new JTextArea(4, 25);
    textArea.setText("Demonstrating\ndrag and drop");
    frame.getContentPane().add(new JScrollPane(textArea));
    textArea.setDragEnabled(true);
    textField.setDragEnabled(true);
    frame.pack();
    frame.setVisible(true);

   }
}
Output 




java code for reading input from console


import java.io.Console;
public class MainClass
{
  public static void main(String[] args) throws ClassNotFoundException
   {
    Console console = System.console();
    if (console == null)
  {
      System.err.println("sales: unable to obtain console");
      return;
    }

    String username = console.readLine("Enter username: ");
    System.out.println(username);
  }
}


We can read Java’s input from “System.in” console, there are two common ways to read input from console.

1) InputStreamReader wrapped in a BufferedReader
2) Scanner classes in JDK1.5

Output


C:\pgm\console>java MainClass
 Enter username: vineeth
vineeth


Here is the java program for read string from console application.

Masking password input from the console : Java


import java.io.Console;
import java.util.Arrays;

public class ConsoleMain
 {

  public static void main(String[] args)
 {
    Console console = System.console();
    String username = console.readLine("Username: ");
    char[] password = console.readPassword("Password: ");

    if (username.equals("vineeth") && String.valueOf(password).equals("vineeth"))
  {
      console.printf("Welcome to Java Application %1$s.\n", username);

      Arrays.fill(password, ' ');
    } else {
      console.printf("Invalid username or password.\n");
    }
  }
}




The idea here is that you can call Arrays. fill (or equivalent) to "blank" the char array as soon as you've validated the password, and from that point the password is no longer stored in memory.
 Since Strings are immutable, the String will remain in the heap until it is garbage collected - which if it manages to get itself interned will be never, and in any other case could still be "too long". All the while it is there, it's potentially vulnerable to sniffing from a variety of vectors.The Console object supports secure password entry through its readPassword method. This method helps secure password entry in two ways. First, it suppresses echoing, so the password is not visible on the user's screen. Second, readPassword returns a character array, not a String, so the password can be overwritten, removing it from memory as soon as it is no longer needed.
 

learn java programming Copyright © 2011-2012 | Powered by appsackel.org