0

I am developing an application in which the first window is

enter image description here

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.

shalini
  • 145
  • 3
  • 14
  • Is this question related to the [original version](http://stackoverflow.com/revisions/11373922/1) of your [previous question](http://stackoverflow.com/q/11373922/230513) on this topic? It is unlikely that anyone can solve your design problem without sufficient background. Please edit your question to include an [sscce](http://sscce.org/) that exhibits the problem you describe. – trashgod Jul 10 '12 at 11:29

1 Answers1

0

The fastest way to do this would be to have a global variable APIKey in Main.java and set it's value after the user logs-in. And if you do this, I would also add a Logout button to unset this variable.

vainolo
  • 6,907
  • 4
  • 24
  • 47
  • the value of API key is set in Login.java ,Main.java is just invoking it ,after the click on login button.So How can I set the value of API key in Main.java?How can I set the value of a variable which is in the file which called Login.java when I am getting the value of API key afetr the user clicks on submit button in tha Login .java window. – shalini Jul 10 '12 at 07:37
  • If `Login` is modal (that is, it blocks the execution of main, as I think it is from how your code looks), then you can add a `getAPIKey()` method in `Login` and call it after the frame is closed (that is, after the `frame.setVisible(true)` line. – vainolo Jul 10 '12 at 07:44
  • can you please explain it more.which frame are you referring to in your above comment.?I have edited the question it may help you.To get the value of API key I need to run Log-in.java at least one time.But I don't want the user to enter the log-in details again and again if the user wants to choose some different time stamp and data set id. – shalini Jul 10 '12 at 07:48
  • What is `Login.java`? it it a `JFrame`? a `JDialog`? – vainolo Jul 10 '12 at 07:59
  • it is a Jframe. algortihm.java is also extending jframe. – shalini Jul 10 '12 at 08:00
  • So create only on `Login` instance and every time you open it, clean the user and password field. create in this class a `getAPIKey` method which returns the `APIKey` or null if the user has not logged in. Then when you create an algorithm instance, check if `getAPIKey` returns a non-null value and pass it on to the algorithm – vainolo Jul 10 '12 at 08:25