I have the following code to get 2 input from user through JCombo box, then I return the value s of the obtained input to the calling function but the problem is that this code displays the frame to obtain user input but before user can press the 'Ok' button it returns the null value to the calling function.I am looking to halt the code in this method till user presses 'Ok' button. Please suggest something.
package io;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Insets;
import java.io.FileNotFoundException;
public class SrcAndTargLangInput implements ActionListener {
public static JFrame frame;
public static JComboBox sourcLang;
public static JComboBox targLang;
public static JLabel setSrcLang;
public static JLabel setTargLang;
public static JButton ok;
static String[] lang=new String[2];
public SrcAndTargLangInput(){
ok = new JButton("Ok");
ok.setBounds(150,150,100,50);
frame = new JFrame();
frame.getContentPane().setLayout(null);
frame.getContentPane().add(ok);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Insets ins = frame.getInsets();
frame.setSize(400+ins.left+ins.right, 200+ins.bottom+ins.top);
setSrcLang=new JLabel("Source Language");
frame.getContentPane().add(setSrcLang);
setSrcLang.setBounds(50, 50, 100, 40);
setTargLang=new JLabel("Target Language");
frame.getContentPane().add(setTargLang);
setTargLang.setBounds(50, 100, 100, 40);
String[] srcLangList={"English","Spanish","French"};
sourcLang = new JComboBox(srcLangList);
frame.getContentPane().add(sourcLang);
sourcLang.setBounds(250,50,100,40);
String[] targLangList={"English","Spanish","French"};
targLang = new JComboBox(targLangList);
frame.getContentPane().add(targLang);
targLang.setBounds(250,100,100,40);
frame.setVisible(true);
ok.addActionListener(this);
}
public static String[] langInfo(){
SrcAndTargLangInput ob = new SrcAndTargLangInput();
return lang;
}
public void actionPerformed(ActionEvent e){
lang[0]=(sourcLang.getSelectedItem().toString());
lang[1]=(targLang.getSelectedItem().toString());
frame.setVisible(false);
}
}
The calling function is :
String[] lg = new String[2];
lg = io.SrcAndTargLangInput.langInfo();
System.out.println(lg[0]);
System.out.println(lg[1]);