Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Editing string from a JTextBox

807601Mar 24 2008 — edited Mar 24 2008
hallo.
I'm trying to make a simple text editing program. You input a sentence, and a method changes some words to others:
For example, you put in this sentence: "Patty is not a tool"
And this comes out: "Patty is totally a tool"

The edited sentence will appear in JLabel fixed.

Here's the code:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Sentence extends JFrame implements KeyListener {
	JTextField text = new JTextField(10);
	public String text2 = text.getText();
	public String sent1 = "";
	JLabel label = new JLabel("Type a sentence so that I can fix it for you.");
	JLabel fixed = new JLabel("--");
	
	Sentence() {
		super("Sentence Correcter");
		setSize(350,100);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBackground( Color.black );
		text.addKeyListener(this);
		FlowLayout flo = new FlowLayout();
		setLayout(flo);
		add(label);
		add(text);
		add(fixed);
		setVisible(true);
		
	}
	
	public void keyTyped(KeyEvent in) {
		fixSentence(text2);
		System.out.println(""+text2);
	}
	
	public void keyPressed(KeyEvent na) {

	}
	
	public void keyReleased(KeyEvent na) {
	}

	public void fixSentence(String sent) {
		fixed.setText(""+sent);
		String a = "a";
		String b = "b";
		sent.replaceAll(a, b);
		fixed.setText(""+sent);
		System.out.println(""+sent);
	}
	
	public static void main(String[] args) {
		Sentence s = new Sentence();
	}
}
that changes all "a" to "b". Doesn't work of course. What am i missing?

Edited by: pattyisalive on Mar 23, 2008 9:34 PM

Edited by: pattyisalive on Mar 23, 2008 9:35 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 21 2008
Added on Mar 24 2008
9 comments
185 views