Solved

Unable to Capture typed text in JTextArea (Java Swing)

Posted on 2014-11-01
2
156 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
ID: 40417853
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
ID: 40418156
Thanks. I am looking and I am not seeing...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

911 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

20 Experts available now in Live!

Get 1:1 Help Now