Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Unable to Capture typed text in JTextArea (Java Swing)

Posted on 2014-11-01
2
Medium Priority
?
161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

705 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