I am developing an application in which the first window is
after clicking on log-in the log-in window appears and after correct credentials is is directed to choose time stamp and data set id window.
This is how I am accessing the value of API key (Login.java
)
String value1=text1.getText();
String value2=text2.getText();
URL url = new URL("http://website-link/method/user.json?userName="+value1+"&password="+value2);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(httpCon.getInputStream()));
String inputLine;
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
}
String Result;
Result=sb.toString();
String jsonSource = Result;
JSONArray array;
try {
array = new JSONArray(jsonSource);
for (int i = 0; i < array.length(); i++) {
JSONObject firstObject = (JSONObject) array.get(i);
System.out.println("APIkey is " + firstObject.getString("apiKey"));
APIkey=firstObject.getString("apiKey");
}
I want that after successful log-in the user can anytime choose data set and time stamps.
That is Login.java
is passing a value to Algorithm.java
(for choosing the data set and timestamp window)
if (httpCon.getResponseCode()==200) {
Algorithm frame=new Algorithm(APIkey);
frame.setSize(450,200);
frame.setVisible(true);
dispose();
JLabel label = new JLabel("Welcome:"+value1);
}
now this API key is used in Algorithm.java
to set the connection and select some specific nodes based on entered time stamps and data set id.
Now I want that after log-in I can anytime click on the second button shown in this window and choose the time stamp and data set id. But in that case I don't have the value of API key as I am using this in Main.java
(responsible for the above window). And in Main.java
the value of API key is null. I don't want the user to enter the log in username and password again and again.
if (e.getSource() == myFirstButton) {
Login frame1=new Login();
frame1.setSize(450,200);
frame1.setVisible(true);
}
if (e.getSource() == mySecondButton) {
Algorithm frame=new Algorithm(APIkey);
frame.setSize(450,200);
frame.setVisible(true);
}
Now how can I have the value of API key stored in all files once the user entered the correct access credentials in Login window and can use it anywhere. Each of the file: Log-in.java , Algorithm.java,Main.java is extending jframe. and Main.java is having the main() function I am working in Netbeans 7.1.2. I am coding this in Swing Java. I am new to Java.