So, I've been making a gui for a client to connect to a server using it. What I want is when I press Login to connect with the specfied Server address and port name on the gui. If I have hardcoded the Server and port it works but I don't want it like that ... I want to be able to switch the server address and port on the go. Any ideas? I tried rearranging the code and what I get is everytime I click Login now it creates a new GUI. So basically I see the problem on this line
finalClient fc = new finalClient(defaultHost,defaultPort);
where I need the object of the class to be able to use run(); but ofc it creates new gui every time, and what I want to do is use the one created by default when lauching .. Here is the entire code:(omitting the implementation besides the GUI as its vector clocks n stuff)
class finalClient
{
private String sentence;
private String modifiedSentence;
private JFrame frame = new JFrame("Distributed Systems project");
private JTextField textField = new JTextField(40);
private JTextArea messageArea = new JTextArea(30, 40);
private DataOutputStream outToServer;
private PrintWriter out;
private int defaultPort;
private String defaultHost;
private JTextField tfServer, tfPort;
private JLabel label;
private JButton login, logout;
private Boolean connected;
private int msgNumber;
private Pattern msgre = Pattern.compile("\\[(.*?)\\] \\((.*)\\)"); //Regular expression for the vector clocks
private Pattern vclock = Pattern.compile("\\((\\d+),(\\d+)\\)"); // regular expression for the numbers of the vector clocks
private HashMap<Long, Long> lastClock = new HashMap<Long,Long>();
private ArrayList<Message> received = new ArrayList<Message>();
public finalClient(String host, int port) {
defaultHost = host;
defaultPort = port;
// Layout GUI
textField.setEditable(true);
messageArea.setEditable(false);
// The NorthPanel with:
JPanel northPanel = new JPanel(new GridLayout(3,1));
// the server name anmd the port number
JPanel serverAndPort = new JPanel(new GridLayout(1,5, 1, 3));
// the two JTextField with default value for server address and port number
tfServer = new JTextField(host);
tfPort = new JTextField("" + port);
tfPort.setHorizontalAlignment(SwingConstants.RIGHT);
serverAndPort.add(new JLabel("Server Address: "));
serverAndPort.add(tfServer);
serverAndPort.add(new JLabel("Port Number: "));
serverAndPort.add(tfPort);
serverAndPort.add(new JLabel(""));
// adds the Server an port field to the GUI
northPanel.add(serverAndPort);
// the Label and the TextField
label = new JLabel("Enter your username below and click Log in", SwingConstants.CENTER);
northPanel.add(label);
northPanel.add(textField);
frame.getContentPane().add(northPanel,"North");
/* The received messages area */
frame.getContentPane().add(new JScrollPane(messageArea), "Center");
/* Buttons */
login = new JButton("Login");
logout = new JButton("Logout");
logout.setEnabled(false); // you have to login before being able to logout
JPanel southPanel = new JPanel();
southPanel.add(login);
southPanel.add(logout);
frame.getContentPane().add(southPanel,"South");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.pack();
// Add Listeners
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
if(connected){
out.print(textField.getText()+"\n");
out.flush();
textField.setText(""); }
else return;
}
});
login.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
finalClient fc = new finalClient(defaultHost,defaultPort);
out.print(textField.getText() + "\n");
out.flush();
textField.setText("");
login.setEnabled(false);
logout.setEnabled(true);
label.setText("Enter your message below");
connected = true;
try{
fc.run();
}
catch(IOException io){
System.out.println("Can't connect");
}
}
});
logout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
frame.dispose();
System.out.println("Client closed");
}
});
}
private void run() throws IOException {
Socket clientSocket = new Socket(defaultHost, defaultPort);
BufferedReader inFromUser = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
while (true){
modifiedSentence = inFromServer.readLine();
//messageArea.append(modifiedSentence + "\n");
Matcher m = msgre.matcher(modifiedSentence);
m.find();
String vdeltas = m.group(1);
String msg = m.group(2);
messageArea.append(msg+"\n");
Matcher vectorM = vclock.matcher(vdeltas);
while(vectorM.find()){
long indx = Long.parseLong(vectorM.group(1));
long value = Long.parseLong(vectorM.group(2));
lastClock.put(indx,value);
}
Message nmg = new Message(lastClock,msg);
received.add(nmg);
Collections.sort(received, new Comparator<Message>() {
public int compare(Message a, Message b){
//return 0;
if(a.isLess(b))
return -1;
else if(a.isEqual(b))
return 0;
else
return 1;
}
});
updateMessages();
//messageArea.append(modifiedSentence + "\n");
}
//clientSocket.close();
}
public void updateMessages() {
StringBuilder sb = new StringBuilder();
for(Message m : received) {
sb.append(m);
sb.append("\n");
}
messageArea.setText(sb.toString());
messageArea.setCaretPosition(messageArea.getDocument().getLength()); // Scrolls auto to the button
}
public static void main(String[] args) throws Exception {
new finalClient("localhost", 9000);
//program.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//program.frame.setVisible(true);
//program.run();
}