2016-08-26 2 views
-3

私はどのようにデータがJavaのあるページから別のページに転送されるかの例を示すことができます。 javafxでもswingでも構いませんが、javafxが好きです。私はオンラインで検索しましたが、これについての援助はほとんどまたはまったく見つかりませんでした。私は私は、どのようにデータが1つのページから別のページに移動するのかを示すことができます

+0

あなたはMVCパターン(モデル - ビュー - コントローラ)になっているはずです – Orin

+1

'どのようにデータがanother'する一つのページから転送されている - 私は見当がつかないそれはどういう意味ですか? – camickr

+0

ページ間で情報を移動または共有する方法について質問しています。別のページにユーザーのログイン情報が渡されたようなもの... – javaphp

答えて

0

Swingコントローラではその後

public class employee{ 
    private String name; 
    //getter and setter 
} 

ようPOJOクラスを作成するJava FXMLを使用していないオブジェクトをインスタンス化する方法でオブジェクトを渡すと、受信側でのデータを得るためにあなたのgetterメソッドを呼び出しますオブジェクト

0

これを行うには複数の方法がありますが、私はシングルトンを使いたいです。 (これはJavaFXにあります)。シングルトンを使うことで、いつでもクラスからいつでも自由にアクセスできます。あるクラスに格納されている値を別のクラスから変更することもできます。

/** 
* Class that has data that you want another class to use 
*/ 
public class SingletonExample extends Application{ 
    private String stringOfData; 
    private SingletonExample self; 
    private SingletonExample(){ 
     stringOfData = "Hello World!"; 
     //Do other constructor stuff 
     self = this; 
    } 
    /** 
    * Returns an instance of SingletonExample. Will never return null 
    * @return SingletonExample 
    */ 
    public SingletonExample instanceOf(){ 
     if(self == null){ 
      new SingletonExample(); 
     } 
     return self; 
    } 
    /** 
    * This allows you to change the value of stringOfData from another class 
    */ 
    public void changeData(String newData){ 
     this.stringOfData = newData; 
    } 
    /** 
    * Simple getter method for stringOfData 
    * @return String, stringOfData 
    */ 
    public String getData(){ 
     return this.stringOfData; 
    } 
    public void start(Stage primaryStage){ 
     new SingletonExample(); 
     //Do stuff for the program here 
    } 
} 

/** 
* Class that you want to use SingletonExample's data in 
*/ 
public class UseData extends Application{ 
    private UseData(){ 
     //Do other constructor stuff 
    } 
    public void start(Stage primaryStage){ 
     String neededData = SingletonExample.instanceOf().getData(); 
     Label data = new Label(neededData); 
     //Do other stuff for the program here 
    } 
    public static void main(String[] args){ 
     launch(args); 
    } 
} 
0

私は、これは非効率的であるかもしれないが、私はちょうどステージにステージからのデータを永続化するために、セッションオブジェクトの並べ替えを作成し、シリアライズとデシリアライズを意味します。ここに私のクラスには、次のとおりです。

/* 
    * To change this license header, choose License Headers in Project Properties. 
    * To change this template file, choose Tools | Templates 
    * and open the template in the editor. 
    */ 
    package Session; 

    import entities.ErpUser; 
    import entities.Run; 
    import java.io.FileInputStream; 
    import java.io.FileOutputStream; 
    import java.io.ObjectInputStream; 
    import java.io.ObjectOutputStream; 
    import java.io.Serializable; 

    /** 
    * 
    * @author samo 
    */ 
    public class SessionHandler implements Serializable { 

     private static final String _location = "session.ser"; 

     private Boolean saved = false; 

     private Session session; 

     public SessionHandler() { 

     } 

     private void serialize() { 
      saved = true; 
      try { 
       FileOutputStream fout = new FileOutputStream(_location); 
       ObjectOutputStream oos = new ObjectOutputStream(fout); 
       oos.writeObject(session); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

     } 

     private Session deSerialize() { 
      Session ses = new Session(); 
      try { 
       FileInputStream fin = new FileInputStream(_location); 
       ObjectInputStream ois = new ObjectInputStream(fin); 
       ses = (Session) ois.readObject(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      return ses; 
     } 

     public void setCurrentPlant(String plant) { 
      getSession().setPlant(plant); 
      this.serialize(); 
     } 

     public String getCurrentPlant() { 
      if (!saved) { 
       session = this.deSerialize(); 
      } 

      return getSession().getPlant(); 

     } 

     /** 
     * 
     * @return 
     */ 
     public String getCurrentType() { 
      if (!saved) { 
       session = this.deSerialize(); 
      } 
      return getSession().getType(); 
     } 

     /** 
     * 
     * @param type 
     */ 
     public void setCurrentType(String type) { 
      getSession().setType(type); 
      this.serialize(); 
     } 

     /** 
     * 
     * @param run 
     */ 
     public void setCurrentRun(Run run) { 
      getSession().setRun(run); 
      this.serialize(); 
     } 

     /** 
     * 
     * @return 
     */ 
     public Run getCurrentRun() { 
      if (!saved) { 
       session = this.deSerialize(); 
      } 
      return getSession().getRun(); 
     } 

     /** 
     * @return the currentUser 
     */ 
     public ErpUser getCurrentUser() { 
      if (!saved) { 
       session = this.deSerialize(); 
      } 
      return getSession().getUser(); 
     } 

     /** 
     * @param currentUser the currentUser to set 
     */ 
     public void setCurrentUser(ErpUser currentUser) { 
      getSession().setUser(currentUser); 
      this.serialize(); 
     } 

     /** 
     * @return the session 
     */ 
     public Session getSession() { 
      if (session == null) { 
       session = new Session(); 
      } 
      return session; 
     } 

     /** 
     * @param session the session to set 
     */ 
     public void setSession(Session session) { 
      this.session = session; 
     } 

    } 

-

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package Session; 

import entities.ErpUser; 
import entities.Run; 
import java.io.Serializable; 

/** 
* 
* @author samo 
*/ 
public class Session implements Serializable { 

    private ErpUser user; 

    private Run run; 

    private String type; 

    private String plant; 

    public Session() 
    { 

    } 

    /** 
    * @return the user 
    */ 
    public ErpUser getUser() { 
     return user; 
    } 

    /** 
    * @param user the user to set 
    */ 
    public void setUser(ErpUser user) { 
     this.user = user; 
    } 

    /** 
    * @return the run 
    */ 
    public Run getRun() { 
     return run; 
    } 

    /** 
    * @param run the run to set 
    */ 
    public void setRun(Run run) { 
     this.run = run; 
    } 

    /** 
    * @return the type 
    */ 
    public String getType() { 
     return type; 
    } 

    /** 
    * @param type the type to set 
    */ 
    public void setType(String type) { 
     this.type = type; 
    } 

    /** 
    * @return the plant 
    */ 
    public String getPlant() { 
     return plant; 
    } 

    /** 
    * @param plant the plant to set 
    */ 
    public void setPlant(String plant) { 
     this.plant = plant; 
    } 



} 
関連する問題