Jump to content

Recommended Posts

Posted

Ola!

Estou a escrever um programa que usa uma JTextArea onde se escreve e quando se clica no enter, supostamente apaga tudo mas da-me sempre um erro. Em baixo vai um exemplo de como eu uso isto.

Eu já tentei :

writeArea.replaceRange("",0,doc.getLength());

writeArea.setText("");

doc.remove(0,doc.getLength());

VER LINHA 74

Não consigo pensar em mais nada para resolver o problema!

Alguem me pode ajudar por favor? Obrigado

import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;



public class exemplo extends JPanel implements DocumentListener, KeyListener 
{


public int lastPos=0;
public static String text="";
public static boolean enterPressed=false;
    
    public static JPanel leftPane;
    public static JPanel rightPane;
    public static JPanel mainPane;
    	
    public JTextArea chatArea, writeArea;
       	

    public exemplo() 
     {	
     	setLayout(new BorderLayout()); 

        chatArea = new JTextArea("");  	
        chatArea.setEditable(false); 														  
        chatArea.setLineWrap(true);				
        chatArea.setWrapStyleWord(true);	
        	
        
        JScrollPane areaScrollPane = new JScrollPane(chatArea);
        areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
        areaScrollPane.setPreferredSize(new Dimension(550, 400)); 
        areaScrollPane.setBorder(BorderFactory.createCompoundBorder( 
                                BorderFactory.createEmptyBorder(5,5,5,5),      
                                areaScrollPane.getBorder()));
                                  
        writeArea = new JTextArea("");  	
        writeArea.setEditable(true); 														  
        writeArea.setFont(new Font("Arial", Font.PLAIN, 11)); 
        writeArea.setLineWrap(true);				
        writeArea.setWrapStyleWord(true);	
        writeArea.getDocument().addDocumentListener(this); // adds DocumentListener
        writeArea.getDocument().putProperty("name", "You");	
        writeArea.addKeyListener(this);
        
        JScrollPane writeScrollPane = new JScrollPane(writeArea);
        writeScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
        writeScrollPane.setPreferredSize(new Dimension(550, 100)); 
        writeScrollPane.setBorder(BorderFactory.createCompoundBorder( 
                                BorderFactory.createEmptyBorder(5,5,5,5),      
                                writeScrollPane.getBorder()));
        
        mainPane = new JPanel(new BorderLayout());
        mainPane.add(areaScrollPane,BorderLayout.PAGE_START);
        mainPane.add(writeScrollPane,BorderLayout.CENTER);

     }

public void insertUpdate(DocumentEvent e)  
{	
	try {
		Document doc = (Document)e.getDocument();
		text = doc.getText(0,doc.getLength());
		lastPos+=1;
			if(enterPressed)
			{
			chatArea.append("You: " + text);
    			chatArea.setCaretPosition(chatArea.getDocument().getLength());	 			
			writeArea.replaceRange("",0,doc.getLength()); // AQUI ESTÁ O PROBLEMA		
			lastPos=0;
	        text="";		       
			}
		}
	catch (BadLocationException ble) {
				ble.printStackTrace();
			}

	/*chatArea.setText("");
            writeArea.setText("");
            writeArea.requestFocusInWindow();*/
    }
    public void removeUpdate(DocumentEvent e) 
    {
    }
public void changedUpdate(DocumentEvent e) 
{
    }
    public void keyTyped(KeyEvent e)
    { 	    	     
    }
    public void keyPressed(KeyEvent e) 
    {   		
    	int keyCode = e.getKeyCode();
    	if (keyCode==10)
    		enterPressed=true;
    	else
    		enterPressed=false;			
    }
    public void keyReleased(KeyEvent e) 
    {
    }
   
private static void CreateAndShowGUI() 
    {
    	JFrame f = new JFrame ("Cliente");
 	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 	f.add(new exemplo());
 	f.add(mainPane);

 	f.pack();
        f.setVisible(true);
    }
    
    
public static void main (String[] args) 
{	
       CreateAndShowGUI();
    }
}

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
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.