Solved

Unable to Capture typed text in JTextArea (Java Swing)

Posted on 2014-11-01
2
158 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

828 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