Jump to content

Java Swing Application - trouble with methods, saving text

So basically, the app (a chat app) I'm working on allows a client (one project) to connect to a server (another project) and messages can be sent between them. I have implemented some code in the Server project which saves the text sent from the Server to the client and the text received by the Server from the Client. However, I'm thinking of making a window in which the user can choose to disable this using a checkbox. The issue I am having is that the code for saving the text is implemented in two separate methods, one in which text is sent, and another in which text is received.

 

Here is the code for these methods:
 

Received Text:

void setReceivedText(String text) {
	try{
		BufferedWriter logWriter = new BufferedWriter (new FileWriter("log.txt",true));
		logWriter.newLine();
		logWriter.write("User:\t" + text + "\n");
		logWriter.close();
	} catch (IOException e) {
	
	}
	historyText.append("User:\t" + text + "\n");
}

historyText is the JTextArea in which the text appears once it is sent in the chat

 

Sent Text:

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == closeButton) {
			// Close the program
			System.exit(0);
		}
		if (e.getSource() == sendButton) {
			String text = messageText.getText();
			server.getConnection().sendLine(text);
			historyText.append("Me:\t" + text + "\n");
			messageText.setText("");
			
			try{
				BufferedWriter logWriter = new BufferedWriter (new FileWriter("log.txt",true));
				logWriter.newLine();
				logWriter.write("Me:\t" + text + "\n");
				logWriter.close();
			}
			catch (IOException f){
				
			}
		}
				
	}

 

I am thinking that using a single method to disable the code is how I should go about this but I am unsure how to do it and still have the program differentiate between the text sent and the text received.

 

Any help would be great, will provide more code if needed

Link to post
Share on other sites

Encapsulate this into its own method:

try{
	BufferedWriter logWriter = new BufferedWriter (new FileWriter("log.txt",true));
	logWriter.newLine();
	logWriter.write("Me:\t" + text + "\n");
	logWriter.close();
}
catch (IOException f){
			
}

And have a boolean flag whose value is based on the checkbox state, then call said method depending on the flag state.

 

EDIT: I do notice you have a different write-out depending if the text is sent or received, but I'll leave that as an exercise to you how to make the method reflect that. ;)

Edited by M.Yurizaki
Link to post
Share on other sites

OP:

You may wanna use System.getProperty("line.separator"); instead of hardcoding "\n" since, in files, new lines actually differ between Windows and *nix systems.

To ensure cross compatibility, either query the system's line separator or use PrintWriter class which will do this for you, automatically: https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html 

("these methods use the platform's own notion of line separator rather than the newline character")

And, if my thought-dreams could be seen,
they'd probably put my head in a guillotine.
But, it's alright, ma, it's life, and life only.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×