Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 165
  • Last Modified:

Unable to Capture typed text in JTextArea (Java Swing)

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
AttilaB
Asked:
AttilaB
1 Solution
 
CPColinCommented:
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
 
AttilaBAuthor Commented:
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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now