I have problem when reading multiple lines from json.
Problem is when i start it, it calls out chooser to pick file to read, and after i click open, it reads only first value from file, and after that i need to select file again with chooser. I know i'm calling reader class every time, but how i can awoid that?
How can i open file once, and make it to read all values, and give them to that variables in f_elementi class?
Here is the part of my json reader class
import java.io.File; import java.io.FileReader; import javax.swing.JFileChooser; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; public class java_read_txt { public static void main(String[] args) { } public static String reader(String b){ JSONParser parser = new JSONParser(); JFileChooser chooser=new JFileChooser(); int returnVal = chooser.showOpenDialog(null); if(returnVal == JFileChooser.APPROVE_OPTION) { { }} try { File f = chooser.getSelectedFile(); Object obj = parser.parse(new FileReader(f)); JSONObject jsonObject = (JSONObject) obj; //--------------------------------------- //CVOR 1 //Link if (b.equals("Llambda")) { long LinkLambda = (long) jsonObject.get("LinkLambda"); String LinkLambda1 = Long.toString(LinkLambda); return LinkLambda1; } else if (b.equals("LMTTR")) { long LinkMTTR = (Long) jsonObject.get("LinkMTTR"); String LinkMTTR1 = Long.toString(LinkMTTR); return LinkMTTR1; } else if (b.equals("Lraspolozivost")) { long LinkRaspolozivost = (Long) jsonObject.get("LinkRaspolozivost"); String LinkRaspolozivost1 = Long.toString(LinkRaspolozivost); return LinkRaspolozivost1; } //MuxDemux else if (b.equals("MDLambda")) { long MuxDemuxLambda = (Long) jsonObject.get("MuxDemuxLambda"); String MuxDemuxLambda1 = Long.toString(MuxDemuxLambda); return MuxDemuxLambda1; }
and here is part of code in another class where i get value from reader, this is in f_elementi class.
final String lambda = java_read_txt.reader("Llambda"); textField = new JTextField(); textField.setHorizontalAlignment(SwingConstants.RIGHT); textField.setBounds(142, 9, 127, 20); Link_P.add(textField); textField.setColumns(10); textField.setText(lambda); final String mttr = java_read_txt.reader("LMTTR"); textField_1 = new JTextField(); textField_1.setBounds(142, 34, 127, 20); textField_1.setHorizontalAlignment(SwingConstants.RIGHT); Link_P.add(textField_1); textField_1.setColumns(10); textField_1.setText(mttr);