Skip to Main Content

Java SE (Java Platform, Standard Edition)

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!

Setting JTextArea text within a JScrollPane

843805Dec 16 2006 — edited Dec 17 2006
Hey all. I'm attempting to use the setText() method of JTextArea to set the text of various areas located in various scroll panes. The general procedure I'm following is to initialize a new JTextArea and JScrollPane within an initializeGui function, set the properties of the JTextArea, then add it to the scroll pane using .getViewport().add(). After that, a separate function goes through all my form fields loading them with whatever data type is appropriate, in the case of the text areas I'm trying to use setText() to set a string. However, after using setText() the text area is displaying nothing. Here's some code for example (workerPane is using a layout manager exclusive to the environment I'm working in, but I don't believe it's the problem).
workerLabel = new JLabel("Customer Address:");
		JScrollPane sp = new JScrollPane();		
		JTextArea ta = new JTextArea();		
		//set ta's properties to whatever is needed
		
		ta.addKeyListener(new java.awt.event.KeyAdapter(){
		
			public void keyTyped(KeyEvent e){
			
				JTextArea text = (JTextArea)e.getSource();				
				String txt = text.getText();				
				int nameLength = 240;
				
				if ( txt.length() >= nameLength ){
				
					text.setText ( txt.substring( 0, nameLength-1 ) );					
					Toolkit.getDefaultToolkit().beep();				
				}			
			}		
		});
		
		sp.getViewport().add(ta);		
		workerPane.add("3.1.right.top",workerLabel);
		workerPane.add("3.2.left.center",custAddScrollPane);
After each area has been initialized, another function is called to load the data into it. Here are some methods I've tried that haven't worked.
//basic, but leaves an empty field.
ta.setText(formProperties[2].getStringValue());
			    
 //this results in two NullPointerException warning windows popping
ta=(JTextArea)sp.getViewport().getView();
 if(formProperties[2].getStringValue() != null)	    ta.setText(formProperties[2].getStringValue());
sp.getViewport().removeAll();
sp.getViewport().add(ta);
Can anyone point me in the right direction as to how to set my textarea's text?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 14 2007
Added on Dec 16 2006
2 comments
404 views