
-----------------------------------
sliverofshadows475
Fri Mar 19, 2010 11:18 pm

Beginner with Swing
-----------------------------------
Hey guys,

I'm new to swing, so this might be a really stupid question. >.> For textArea, can you convert what the user writes into the textArea by using an Action Listener?

I'm writing a basic substitution cipher for school, and I'd like for the user to be able to copy the ciphertext into other programs, instead of having the DOS window that you can't really do much with.

Thanks a lot,
-SS

-----------------------------------
chrisbrown
Sat Mar 20, 2010 2:25 am

Re: Beginner with Swing
-----------------------------------
Not a stupid question, and yes, you certainly can. Just checking, but you're using a JTextFrame, right? The non-J version is AWT, not Swing.

There are a few different ways to do it, depending on the functionality you want. The easiest way would be to add a button and attach an ActionListener to it, so that when it is clicked, the text is changed to the encrypted version:
final JTextArea textArea = ...
JButton cipherButton = new JButton("Cipher it!");
cipherButton.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
		String cipherText = textArea.getText();
		textArea.setText(cipherText);
	}
});
Notice that you have to declare textArea as final to be able to access it inside the action listener.
Also, I havent tested this and it's late so there might (will) be errors.

-----------------------------------
sliverofshadows475
Sat Mar 20, 2010 9:24 pm

RE:Beginner with Swing
-----------------------------------
Oh, ok, thanks! It worked perfectly :D

Just another random question, but can you make it so that in your text area the text someone types wraps around, instead of just going off-screen? :S

Thanks a lot.
+SS

-----------------------------------
chrisbrown
Sat Mar 20, 2010 9:36 pm

RE:Beginner with Swing
-----------------------------------
textArea.setLineWrap(true);

You should familiarize yourself with the Java API. It will answer most questions of the form "Can [some class] do [something]?"

-----------------------------------
sliverofshadows475
Sun Mar 21, 2010 11:31 am

RE:Beginner with Swing
-----------------------------------
Oh, ok thanks. >.<

I read the Java API part for JTextArea, but I still have a question. My encryption relies on changing the characters to their ASCII counterpart, changing the number, and then reverting it back into a character. As you know, this turn the character into one of those rarely-used strange characters.

I compared the same sentence in the DOS window, and in the .awt/Swing equivalent, with the same cipher. The JTextArea doesn't show the weird characters.

Is there any way to change this, because my whole program relies on this?

Thanks,
+SS

-----------------------------------
chrisbrown
Sun Mar 21, 2010 12:04 pm

RE:Beginner with Swing
-----------------------------------
There's probably a way, but I don't see anything helpful offhand. If you're using a simple letter shift encryption, you can limit the possible outcomes. The printable ASCII characters are between values 32 and 126 inclusive, thats 95 possibilities.
[code]char a = 'A';
int b = Character.getNumericValue();
int c = b + 9999;   // Way outside the printable range
int d = c % 95;     // Reduce to a value between 0 and 94
int e = d + 32;      // Add 32, so e is between 32 and 126
char f = Character.valueOf(e);  // f should be printable
[/code]

-----------------------------------
sliverofshadows475
Sun Mar 21, 2010 12:58 pm

RE:Beginner with Swing
-----------------------------------
That seems like a good way to limit your outcomes, but how would you change it back into regular text?

When you mod a number, there's no telling what the original number might have been >.>

Thanks,
+SS

-----------------------------------
chrisbrown
Sun Mar 21, 2010 1:09 pm

Re: RE:Beginner with Swing
-----------------------------------
Similar process, you just have to normalize first (the int h... part)
[code]// continued from above
int g = Character.getNumericValue(f);   // Equal to e, just here for clarity
int h = g - 32;
int i = h - 9999;   // Subtracting instead of adding
int j = i % 95;
int k = i + 32;   // k should be equal to b
[/code]
