To create a text area using swing
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: To create a text area using swing

  1. #1
    Join Date
    Mar 2012
    Posts
    4

    To create a text area using swing

    In the attached file i have given the code to create a text area when a buton is clicked along with a scrollbar .in the output i can see the button but it does not work so i cannot see the text area over which it will be written "button clicked. I have used the concept of interface and inner class.please see the code and help me out.
    Attached Files Attached Files

  2. #2
    Join Date
    Nov 2011
    Posts
    189

    Re: To create a text area using swing

    I cant view .java files on the phone but i suspect you haven't used a validate(); call.. This kind of operation is not thread safe so I'm not surprised it doesent work. But here's a walkaround: use textarea.setPrefferedSize(new Dimension(1,1)); for the not viewable text area and then call the same method with the parameters 450,450(viewable text area), for example.. To add a scrollbar you just do add(new JScrollPane(textArea));

  3. #3
    Join Date
    Mar 2012
    Posts
    4

    Re: To create a text area using swing

    thank you cens

  4. #4
    Join Date
    Mar 2012
    Posts
    4

    Re: To create a text area using swing

    it is still not working please help me.

  5. #5
    Join Date
    Mar 2012
    Posts
    4

    Re: To create a text area using swing

    it is still not working

  6. #6
    Join Date
    Nov 2011
    Posts
    189

    Re: To create a text area using swing

    try this

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JScrollPane.*;
    import java.awt.event.ActionListener.*;
    import javax.swing.JTextArea.*;
    import javax.swing.*;
    
    public class TextArea implements ActionListener
    {
    	
    	JTextArea text;
    	public static void main(String [] args)
    	{
    		TextArea gui=new TextArea();
    		gui.go();
    	}
    	public void go(){
    		JFrame frame;
    		frame = new JFrame();
    		JPanel panel = new JPanel();
    		JButton button = new JButton("JUST CLICK IT");
    		button.addActionListener(this);
    		
    		text=new JTextArea();
    		text.setLineWrap(true);
    		text.setPrefferedSize(new Dimension(1,1));
                    //pay attention here.. Is panel where "text" should be added?
                    panel.add(new JScrollPane(text));
    		
    		frame.getContentPane().add(BorderLayout.CENTER, panel);
    		frame.getContentPane().add(BorderLayout.SOUTH, button);
    		
    		
    		JScrollPane scroller=new  JScrollPane();
    		panel.add(scroller);
    		scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    		scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    		
    		
    	
    		panel.add(scroller);
    		
    		frame.setSize(350,300);
    		frame.setVisible(true);
    		}
    		public void actionPerformed(ActionEvent ev){
    		                                                 text.setPrefferedSize(new Dimension(350,350));
    				                                 text.append("button clicked\n");
    		}
    		}


    is this the way you did it?



    also..you forgot to add "text" to the panel
    Last edited by cens; March 8th, 2012 at 08:29 AM.

  7. #7
    Join Date
    Nov 2011
    Posts
    189

    Re: To create a text area using swing

    Anything new?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center