Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Keep Size Of JTextArea constant?

I'm using an object of JTextArea in my application which deals with sending sms.

I've used a DocumentFilter so as to allow only 160 characters to be typed in the textarea but now, I want the size of the textarea to be constant. it goes on increasing if I keep writing on the same line without pressing 'enter' key or even when I keep on pressing only Enter key. I tried once using 'scrollbar' too but the problem remains same. Suggest me something over this. Below is my code. Please check it.

class Send_sms extends JPanel implements ActionListener,DocumentListener    
{     
    JButton send;  
    JTextArea smst;  
    JLabel title,limit;  
    JPanel mainp,titlep,sendp,wrap,titlewrap,blankp1,blankp2,sendwrap;   
    JScrollPane scroll;  
    Border br,blackbr;  
    Boolean flag = false;  
    PlainDocument plane;
    public static final int LINES = 4;  
    public static final int CHAR_PER_LINE = 40;     
       //character limit 160 for a sms  

    public Send_sms()
        {
        br = BorderFactory.createLineBorder(Color.RED);
        blackbr = BorderFactory.createEtchedBorder(EtchedBorder.RAISED,Color.DARK_GRAY,Color.GRAY);
        setBorder(blackbr);

                title = new JLabel("Enter the text you want to send!");
        title.setFont(new Font("",Font.BOLD,17));
        limit = new JLabel(""+charCount+" Characters");
        smst = new JTextArea(LINES,CHAR_PER_LINE);
        smst.setSize(100,100);
        plane = (PlainDocument)smst.getDocument();
        //adding DocumentSizeFilter 2 keep track of characters entered
        plane.setDocumentFilter(new DocumentSizeFilter(charCount));
        plane.addDocumentListener(this);
        send = new JButton("Send");
        send.setToolTipText("Click Here To Send SMS");
        send.addActionListener(this);

        //scroll = new JScrollPane(smst);
        //scroll.setPreferredSize(new Dimension(200,200));
        //scroll.setVerticalScrollBarPolicy(null);
        //scroll.setHorizontalScrollBarPolicy(null);
        smst.setBorder(br);

        blankp1 = new JPanel();
        blankp2 = new JPanel();
        titlep = new JPanel(new FlowLayout(FlowLayout.CENTER));
        titlewrap = new JPanel(new GridLayout(2,1));
        mainp = new JPanel(new BorderLayout());
        sendwrap = new JPanel(new GridLayout(3,1));
        sendp = new JPanel(new FlowLayout(FlowLayout.CENTER));
        wrap = new JPanel(new BorderLayout());

        titlep.add(title);
        titlewrap.add(titlep);
        titlewrap.add(blankp1);

        sendp.add(send);
        sendwrap.add(limit);
        sendwrap.add(blankp2);
        sendwrap.add(sendp);

        wrap.add(smst,BorderLayout.CENTER); 
        mainp.add(titlewrap,BorderLayout.NORTH);
        mainp.add(wrap,BorderLayout.CENTER);
        mainp.add(sendwrap,BorderLayout.SOUTH);
        add(mainp);


              }

    public void actionPerformed(ActionEvent e)
    {
        Vector<Vector<String>> info = new Vector<Vector<String>> ();
        Vector<String> numbers = new Vector<String>();
        if(e.getSource() == send)
        {
            //Call a function to send he message to all the clients using text
            //charCount = 165;
            String msg = smst.getText();
            if(msg.length() == 0)
                JOptionPane.showMessageDialog(null,"Please Enter Message","Error",JOptionPane.ERROR_MESSAGE);
            else
            {
            //  System.out.println("Message:"+msg);

                Viewdata frame = new Viewdata(msg);

                limit.setText(""+charCount+" Characters");
                charCount = 160;
              } 
        }
    }
    public void insertUpdate(DocumentEvent e)
    {
        System.out.println("The legth:(insert) "+e.getLength());
        for(int i = 0;i<e.getLength(); i++)
        {   
            if(charCount >0)
                charCount--;
            else
                break;
        }
        limit.setText(""+charCount+" Characters");

    }
    public void removeUpdate(DocumentEvent e)
    {
        //System.out.println("The legth(remove): "+e.getLength());
        for(int i = 0;i<e.getLength(); i++)
        {   

            charCount++;

        }
        limit.setText(""+charCount+" Characters");      
    }
    public void changedUpdate(DocumentEvent e)
    {
        //System.out.println("The legth(change): "+e.getLength());

    }   

}//end Send_sms
like image 608
Supereme Avatar asked Mar 16 '10 13:03

Supereme


People also ask

How set textArea size in Java?

i use setPreferredSize method to set the dimension of the Text Area.

Is JTextArea editable?

The JTextArea class provides a component that displays multiple lines of text and optionally allows the user to edit the text.

How do I write to JTextArea?

You can just use JTextArea. setText(String) and JTextArea. append(String) to do this.


1 Answers

Sound like you are creating the text area using

JTextArea textArea = new JTextArea();

When using this format the text area doesn't have a preferred size so it keeps on growing. If you use:

JTextArea textArea = new JTextArea(2, 30);
JScrollPane scrollPane = new JScrollPane( textArea );

Then the text area will have a preferred size of 2 rows and (roughly) 30 columns. As you type when you exceed the preferred width the horizontal scrollbar will appear. Or if you turn on wrapping, then the text will wrap and a vertical scrollbar will appear.

like image 125
camickr Avatar answered Oct 17 '22 03:10

camickr