Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 5.42 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Get Array from Another Class
  2. package com.luugiathuy.apps.remotebluetooth;
  3. import java.awt.Dimension;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.WindowAdapter;
  7. import java.awt.event.WindowEvent;
  8. import java.io.File;
  9.  
  10. import javax.swing.JButton;
  11. import javax.swing.JFileChooser;
  12. import javax.swing.JFrame;
  13. import javax.swing.JPanel;
  14.  
  15. public class DemoJFileChooser extends JPanel implements ActionListener {
  16.    JButton go;
  17.  
  18.    JFileChooser chooser;
  19.    String choosertitle;
  20.    public int x=0;
  21.    public String path[] = new String[10];
  22.  
  23.   public DemoJFileChooser() {
  24.     go = new JButton("Do it");
  25.     go.addActionListener(this);
  26.     add(go);
  27.    }
  28.  
  29.    public Dimension getPreferredSize(){
  30.     return new Dimension(200, 200);
  31.     }
  32.  
  33.   public static void main(String s[]) {
  34.       Thread waitThread = new Thread(new WaitThread());
  35.         waitThread.start();
  36.  
  37.     JFrame frame = new JFrame("");
  38.     DemoJFileChooser panel = new DemoJFileChooser();
  39.     frame.addWindowListener(
  40.       new WindowAdapter() {
  41.         public void windowClosing(WindowEvent e) {
  42.           System.exit(0);
  43.           }
  44.         }
  45.       );
  46.     frame.getContentPane().add(panel,"Center");
  47.     frame.setSize(panel.getPreferredSize());
  48.     frame.setVisible(true);
  49.     }
  50.  
  51. @Override
  52. public void actionPerformed(ActionEvent arg0) {
  53.     // TODO Auto-generated method stub
  54.     int result;
  55.  
  56.     chooser = new JFileChooser();
  57.     chooser.setCurrentDirectory(new java.io.File("."));
  58.     chooser.setDialogTitle(choosertitle);
  59.     chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
  60.        //
  61.     // disable the "All files" option.
  62.     //
  63.     chooser.setAcceptAllFileFilterUsed(false);
  64.     //    
  65.  
  66.     if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
  67.       System.out.println("getCurrentDirectory(): "
  68.          +  chooser.getCurrentDirectory());
  69.       System.out.println("getSelectedFile() : "
  70.          +  chooser.getSelectedFile());
  71.  
  72.           File paths = chooser.getSelectedFile();
  73.           path[x] = paths.getAbsoluteFile().toString();
  74.           x++;
  75.  
  76.     for (int y=0;y<9;y++){
  77.           System.out.println(path[y]);
  78.           setArray(path);
  79.  
  80.     }
  81.     }
  82.     else {
  83.       System.out.println("No Selection ");
  84.       }
  85.  
  86. }
  87.     void setArray(String paths[]) { // saves surname to variable
  88.              path= paths;
  89.  
  90.     }
  91.  
  92.     String[] getArray() { //gets the surname when called
  93.           return path;
  94.     }
  95. }
  96.        
  97. package com.luugiathuy.apps.remotebluetooth;
  98.  
  99. import java.awt.MouseInfo;
  100. import java.awt.Point;
  101. import java.awt.PointerInfo;
  102. import java.awt.Robot;
  103. import java.awt.event.InputEvent;
  104. import java.awt.event.KeyEvent;
  105. import java.io.InputStream;
  106. import java.io.OutputStream;
  107. import com.luugiathuy.apps.remotebluetooth.DemoJFileChooser;
  108.  
  109.  
  110. import javax.microedition.io.StreamConnection;
  111.  
  112. public class ProcessConnectionThread implements Runnable{
  113.     //DemoJFileChooser path;
  114.     //public String[] path = new String[10];
  115.  
  116.     private StreamConnection mConnection;
  117.  
  118.     // Constant that indicate command from devices
  119.     //private static final int EXIT_CMD = -1;
  120.     private static final int STEER_RIGHT = 1;
  121.     private static final int STEER_LEFT = 2;
  122.     private static final int ACCE = 3;
  123.     private static final int REV = 4;
  124.     private static final int MOVE = 5;
  125.     private static final int LEFT_CLICK = 6;
  126.     private static final int DOUBLE_CLICK = 7;
  127.     private static final int RIGHT_CLICK = 8;
  128.     private static final int HOLD = 9;
  129.     private static final int RELEASE = 10;
  130.     private static final int MOVEX = 11;
  131.     private static final int MOVEY = 12;
  132.     private static final int GAME = 20;
  133.     private static final int GAME1 = 11;
  134.     private static final int GAME2 = 12;
  135.     private static final int GAME3 = 13;
  136.     private static final int GAME4 = 14;
  137.     private static final int GAME5 = 15;
  138.     private DemoJFileChooser patharray[] = new DemoJFileChooser[10]; //create object array from student class
  139.  
  140.     public ProcessConnectionThread(StreamConnection connection)
  141.     {
  142.         mConnection = connection;
  143.     }
  144.  
  145.     public void FileChooser() { //constructor use keyword public just to make sure it is visible
  146.         for (int studI=0;studI<10;studI++) {
  147.             patharray[studI] = new DemoJFileChooser();
  148.         }
  149.     }
  150.     @Override
  151.     public void run() {
  152.         try {
  153.  
  154.             // prepare to receive data
  155.             InputStream inputStream = mConnection.openInputStream();
  156.             OutputStream outputStream = mConnection.openOutputStream();
  157.             System.out.println("waiting for input");
  158.  
  159.             while (true) {
  160.                 int command = inputStream.read();
  161.                 System.out.println(command);
  162.  
  163.                 processCommand(command);
  164.  
  165.             }
  166.         } catch (Exception e) {
  167.             e.printStackTrace();
  168.         }
  169.     }
  170.  
  171.     /**
  172.      * Process the command from client
  173.      * @param command the command code
  174.      */
  175.     private void processCommand(int command) {
  176.         try {
  177.  
  178.  
  179.             case GAME1:
  180.                 for (int y=0;y<10;y++){
  181.                       patharray[y].getArray();
  182.                       System.out.println(patharray[y]);
  183.                 }
  184.                 Process p = Runtime.getRuntime().exec(patharray[0]);
  185.                 p.waitFor();
  186.  
  187.  
  188.  
  189.                 break;
  190.  
  191.  
  192.             }
  193.         } catch (Exception e) {
  194.             e.printStackTrace();
  195.         }
  196.     }
  197.  
  198.  
  199.     }
  200.        
  201. String [] srcArray = { "item 1", "item 2", "item 3" };
  202. String [] copy = Arrays.copyOf(srcArray, srcArray.length);