I am making my own component similar to the JOptionPane, but with some added GUI components specialised for my current project. I have finished with the GUI of the component, but I do not know how to make wait for the user to enter input before continuing, as the JOptionPane does... I want my component to exit when my user provides input. I've also attempted at making it a singleton class to ensure that there can only be one visible at a time. My code goes as follows:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class NewChatter extends JFrame {
private static NewChatter instance = new NewChatter();
private JTextField text;
private JComboBox<String> box;
private JButton ok,
cancel;
private JPanel pnl1,
pnl2,
pnl3;
private JLabel lbl;
private Dimension size = new Dimension(600, 160);
private static String[] names,
addresses;
private static String address = "127.0.0.1";
NewChatter(){
//constructor sets up the interface
//all the components have action listeners
//each value assigns a value to the "address" variable
/* Components:
* 1 JTextField
* 1 JComboBox
* 2 JButtons (ok, cancel)
* 3 JPanels to hold the different components
*/
}
public static NewChatter getInstance(){
return instance;
}
public String showInput(){ //this method is called in the main program
instance.setVisible(true);
instance.setAlwaysOnTop(true);
if(!address.equals("--")){ //my attempt at making the program wait
String hold = address;
address = "--";
return hold;
}else{
//Tried to use recursive programming
}
return address; //supposed to return the address only,
} //once the user has entered a value
}