Solved

Unable to Capture typed text in JTextArea (Java Swing)

Posted on 2014-11-01
2
155 Views
Last Modified: 2014-11-02
This is really a weird problem. I can type anything in the JTextArea, but when I apply the getText() method
I get a blank string returned every time.

I need to capture the typed text in a String that I can convert into an ArrayList<String> , when the user
clicks the button.

The code:
package addyournote;

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

public class AddYourNote extends JFrame {
	
	// Variables declaration 
	private JLabel lblTypeYourNote;
	private JScrollPane scrollPane1;
	private JTextArea txtaTypeSpace;
	private JButton jbtnAddNote;
	// End of variables declaration 
	
	public AddYourNote() {  // Constructor for creating JFrame as Child Window   
	                try {
	                    // Set the look and feel for the current OS (Windows) Scheme
	                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	                            initComponents();
	                                } catch (Exception e) {
	                                        e.printStackTrace();
	                                }
	                             initComponents();     
	                               this.setResizable(false);
	}

	private void jbtnAddNote_ActionPerformed(ActionEvent e) {
                
            String txtaTypeSpaceContents = txtaTypeSpace.getText();
            System.out.println("txtaTypeSpaceContents=" + txtaTypeSpaceContents); 
          //  this.dispose();
	}

	private void initComponents() {
		// Component initialization 
		lblTypeYourNote = new JLabel();
		scrollPane1 = new JScrollPane();
		txtaTypeSpace = new JTextArea();
                
		jbtnAddNote = new JButton();

		//======== this ========
		Container contentPane = getContentPane();
		contentPane.setLayout(null);

		//---- lblTypeYourNote ----
		lblTypeYourNote.setText("Type Your Note Here:");
		lblTypeYourNote.setFont(new Font("Tahoma", Font.BOLD, 14));
		contentPane.add(lblTypeYourNote);
		lblTypeYourNote.setBounds(35, 5, 280, 25);

		//======== scrollPane1 ========
		{
			scrollPane1.setViewportView(txtaTypeSpace);
		}
		contentPane.add(scrollPane1);
		scrollPane1.setBounds(10, 35, 415, 245);

		//---- jbtnAddNote ----
		jbtnAddNote.setText("Add Note");
		jbtnAddNote.setFont(new Font("Tahoma", Font.BOLD, 12));
		jbtnAddNote.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jbtnAddNote_ActionPerformed(e);
			}
		});
		contentPane.add(jbtnAddNote);
		jbtnAddNote.setBounds(125, 290, 190, jbtnAddNote.getPreferredSize().height);

		{ // compute preferred size
			Dimension preferredSize = new Dimension();
			for(int i = 0; i < contentPane.getComponentCount(); i++) {
				Rectangle bounds = contentPane.getComponent(i).getBounds();
				preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
				preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
			}
			Insets insets = contentPane.getInsets();
			preferredSize.width += insets.right;
			preferredSize.height += insets.bottom;
			contentPane.setMinimumSize(preferredSize);
			contentPane.setPreferredSize(preferredSize);
		}
		pack();
		setLocationRelativeTo(getOwner());
	    // End of component initialization 
	}
        
    public static void main(String[] args) {
           AddYourNote addYourNote = new AddYourNote();
           addYourNote.setVisible(true);
       }

}

Open in new window


What am I doing wrong here?

Thanks for your help.
AddYourNote.png
0
Comment
Question by:AttilaB
2 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 500 total points
Comment Utility
Hi AttilaB,

Looks like you're calling initComponents() twice, so the components that show up in the frame aren't the same ones that end up in the member variables. The code works as you'd expect it to if you remove one of those calls.
0
 

Author Closing Comment

by:AttilaB
Comment Utility
Thanks. I am looking and I am not seeing...
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Enhanced For loop vs ForEach Loop 4 54
triangle challenge 4 75
maven java path setting 5 49
eclipse java  build path 6 17
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now