Home arrow static arrow Java Programming [Archive] - JTextArea +appending texts
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - JTextArea +appending texts
This topic has 7 replies on 1 page.

Posts:9
Registered: 7/20/04
JTextArea +appending texts  
Jul 20, 2004 10:18 PM



 
hi ppl..

i got some problems wit JTextArea. here is the problem.

................
...............
public void keyTyped( KeyEvent e)
{

textArray[arrayIndex++] = e.getKeyChar(); // Add text typed by user to array
parser = new Parsing();
// Convert to String
String word = new String(textArray);
word = word.trim();

if(parser.isTextAppend(word)) {
textArea1.append("Text Append");
}
}


class parser.

......
............
public boolean isTextAppend(String text) {
if("test".equals(text))
return true;
return false;
}//end isInclude

--------------end------------
when i entered the string ("test") on my TextArea1,isTextAppend metod will return TRUE.so it is ok.but when i call TextArea1.append("text append), the problems occurs. it not actually append the text behind the string ("test") but append before ("e").here is some examples. let say i put a string "test" inside my TextArea1 .the output come like this "tesText Appende".see, 'e' hav been push away to last position.
what i want is the output should come out like this "test Text Append" on my TextArea1.

i do apprecitae if all off u could help me.thanx...:)
 

Posts:1,995
Registered: 12/10/00
Re: JTextArea +appending texts  
Jul 20, 2004 10:58 PM (reply 1 of 7)



 
I don't believe that the problem is in the append() method. This simple example below works fine for me. Are you sure you are not changing the text from another thread or before appending? You can debug this by registering yourself as a document listener and dump the stack whenever change occurs.

HTH
Mike

import java.awt.event.ActionListener;import java.awt.event.ActionEvent; import java.awt.BorderLayout; import javax.swing.*; public class TestJTA extends JFrame implements ActionListener{	private JButton appendButton;		private JTextArea area;		public TestJTA(){		super("Test");		construct();	}		public void actionPerformed(ActionEvent e){		if(e.getSource() == appendButton){			appendAction();		}	}		public static void main(String args[]){		new TestJTA();	}		private void construct(){		area = new JTextArea(2, 20);		getContentPane().add(area);		appendButton = new JButton("Append");		appendButton.addActionListener(this);		getContentPane().add(appendButton, BorderLayout.SOUTH);		pack();		setLocationRelativeTo(null);		show();	}		private void appendAction(){		area.append("[Appended]");	}}
 

Posts:9
Registered: 7/20/04
Re: JTextArea +appending texts  
Jul 20, 2004 11:48 PM (reply 2 of 7)



 
thanx bellyripper...

yeah..it is work when i tried ur code. but i still clueless with my code. i try to figure out any differents from ur code with my code.so i guest the EVENT that we are using.emm..so.here i post mycode.
-------------------------begin-----------------------------
import javax.swing.*;
import java.awt.*;
import javax.swing.JApplet;
import java.awt.event.*;

public class TesttextArea extends JApplet implements ItemListener {
char[] textArray=new char[255];
int arrayIndex=0;
Parsing1 parser = new Parsing1();
JTextArea textArea1, textArea2;
public void init() {
// Get the applet's GUI component display area
Container container = getContentPane();
container.setBackground(Color.pink);
container.setLayout(new FlowLayout());

// Text area 1
textArea1 = new JTextArea();

JScrollPane scrollPane1 = new JScrollPane(textArea1);
scrollPane1.setPreferredSize(new Dimension(300, 250));
container.add(scrollPane1);

textArea1.addKeyListener(new KeyListener()
{
public void keyPressed( KeyEvent e)
{
int key=e.getKeyCode();

}//end of keypressed

public void keyTyped( KeyEvent e)
{

textArray[arrayIndex++] = e.getKeyChar(); // Add text typed by user to array

parser = new Parsing1();
// Convert to String
String word = new String(textArray);
word = word.trim();

if(parser.isInclude(word)) {

textArea1.append("append");
}
}//end keyTyped

public void keyReleased( KeyEvent e)
{
int keyreleased = e.getKeyCode();
}
});//end for textarealistener

}//end init
public void itemStateChanged(ItemEvent e) {

}//end of item state
}

class Parsing1 {

public boolean isInclude(String text) {
if("test".equals(text))
return true;
return false;
}//end isInclude
}
---------------------end TesttextArea.java............................................
--------------------begin test.html-----------------------------------
<html>
<head>
<title>web based c editor</title>
</head>
<body BGCOLOR=WHITE>
<applet CODE="TesttextArea.class" WIDTH=800 HEIGHT=600>
</applet>
</body>
</html>
-----------------------end test.html------------------------

ok..when u input string "test" on textarea, suppose u'll hav a new appended text "testappend"..but what actually happen are "tesappends"...
i dont get a clue yet..sorie..:)

 

Posts:1,995
Registered: 12/10/00
Re: JTextArea +appending texts  
Jul 21, 2004 12:57 AM (reply 3 of 7)



 
That's because the document is notified and changed after your listener was notified. A possible solution would be move the code from keyTyped into keyReleased notification or enqueue your append request after document notification with
          SwingUtilities.invokeLater(new Runnable(){            public void run(){              textArea1.append("append");            }          });


BTW, don't be afraid to use [code][/code] tags when posting code, they are really helpful

HTH
Mike
 

Posts:9
Registered: 7/20/04
Re: JTextArea +appending texts  
Jul 21, 2004 1:52 AM (reply 4 of 7)



 
bellyripper..

uhh..thanx god it works.both code works just fine.but argg..when i using multiple key let say [shift+3],the events seem doesn't recornize it.ok..once again,i post back my code...
import javax.swing.*;import java.awt.*;import javax.swing.JApplet;import java.awt.event.*; public class TesttextArea extends JApplet implements ItemListener {	char[] textArray=new char[255];	int arrayIndex=0;	Parsing1 parser = new Parsing1();	JTextArea textArea1, textArea2;	public void init() {		// Get the applet's GUI component display area		Container container = getContentPane();		container.setBackground(Color.pink);		container.setLayout(new FlowLayout()); 		// Text area 1		textArea1 = new JTextArea(); 		JScrollPane scrollPane1 = new JScrollPane(textArea1);		scrollPane1.setPreferredSize(new Dimension(300, 250));		container.add(scrollPane1);								textArea1.addKeyListener(new KeyListener()		{		   public void keyPressed( KeyEvent e)		   {			  int key=e.getKeyCode();			  		   }//end of keypressed	    		   public void keyTyped( KeyEvent e)		   { 				   }//end keyTyped  		   public void keyReleased( KeyEvent e)		   {			textArray[arrayIndex++] = e.getKeyChar();    // Add text typed by user to array					parser = new Parsing1();   		// Convert to String		String word = new String(textArray);		word = word.trim();			 		if(parser.isInclude(word)) {			//SwingUtilities.invokeLater(new Runnable(){			//		  public void run(){			//			textArea1.append("append");			//		  }			//		});			textArea1.append(" append"); 		}		   }		});//end for textarealistener	 		}//end init	public void itemStateChanged(ItemEvent e) { 	  }//end of item state} class Parsing1 { 	public boolean isInclude(String text) {		  if("#test".equals(text))			 return true;		  return false;	   }//end isInclude	}

so..the diffrent where is # key at function isInclude.when u put # inside the textarea,the funtion return false.but i guest it should return true rite..:)...
ohh..thanx for the solution bellyripper. but i need to capture # key also.thanx a lot..emm..:)..thanx also for code tags..yeah its really help...:)

 

Posts:9
Registered: 7/20/04
Re: JTextArea +appending texts  
Jul 21, 2004 7:36 PM (reply 5 of 7)



 
hi again....

emm..seem i knew what actually happen when i used shift key. when i release the shift key,seem like the system reply by putting sign '?' for the shift key release events.so how could i delete/remove the ? sign.sample. when we are using 2 combination of key [e.g: #] we should use keys [shift + 3].but on my system the sign of key represence as [#?] not [#].so how to remode the ? sign.thanx for ur times..:)
 

Posts:24,517
Registered: 98-02-27
Re: JTextArea +appending texts  
Jul 21, 2004 8:54 PM (reply 6 of 7)



 
Well, I haven't really followed the thread and don't really understand what you are trying to do but I'm going to suggest that you look at using a [url http://java.sun.com/docs/books/tutorial/uiswing/events/documentlistener.html]Document Listener[/url]. The DocumentListener is invoked whenever any change is made to the textArea.

Your current code is using a KeyListerner. What happens if the user "pastes" some text into your text area? A DocumentListener will support both "typed" and "pasted" text.
 

Posts:9
Registered: 7/20/04
Re: JTextArea +appending texts  
Jul 23, 2004 4:23 AM (reply 7 of 7)



 
ok..its worked now..emmm bellyripper thanxq so much. i just ignore the SHIFT key by tracing the SHIFT KEY code. so when i release a SHIFT key, im not add the char to my array char.:)...ohh to camirkr.thanx for u info..:)
 
This topic has 7 replies on 1 page.