Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

getText() method not working for TextFields and JTextFields

807600Jun 15 2007 — edited Jun 16 2007
Hey folks, I originally posted about this in the Swing forum but I have since reproduced this issue with an awt textfield so my thinking is its a more general problem for me. I have searched the forums here, the netbeans users mailing list and googled to see if anyone else was seeing this problem but nothing coming up. I have only been playing with Java for about a week so I am hoping there is a simple explanation that I am missing.

I have created a test applet that illustrates the problem which I am including the source for below, it contains just 2 JTextFields and a JButton, with an action event handler for the button.

The problem is that the text defined in the two textfield components is not being returned via getText() when I run this applet in a web browser or java's appletviewer -- it does work in the Netbeans 5.5.1 IDE that I use ( I use jdk-1.6.0_01 ).

---

/*
* AndyTest.java
*
* Created on June 15, 2007, 10:33 AM
*/
import java.awt.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.*;

/**
*
* @author Andrew
*/
public class AndyTest extends javax.swing.JApplet {
public AndyTest(){
init();
}

public static void main(String[] args){
AndyTest telnetApp = new AndyTest();

JFrame myWindow = new JFrame("TMC Telnet Client");
myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myWindow.setContentPane( telnetApp );//add to the window
myWindow.pack();
myWindow.setVisible(true);

}
/** Initializes the applet AndyTest */
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}

/** This method is called from within the init() method to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jUsername = new javax.swing.JTextField();
jPassword = new javax.swing.JTextField();
jLoginButton = new javax.swing.JButton();

jLoginButton.setText("Login");
jLoginButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jLoginButtonActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(102, 102, 102)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jPassword, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jUsername, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLoginButton)
.addGap(36, 36, 36)))
.addContainerGap(111, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(84, 84, 84)
.addComponent(jUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLoginButton)
.addContainerGap(101, Short.MAX_VALUE))
);
}// </editor-fold>

private void jLoginButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println( "COMPONENTS- Username: " + jUsername.getText() + " Password: " + jPassword.getText() );
}


String sUsername = new String();
String sPassword = new String();

// Variables declaration - do not modify
private javax.swing.JButton jLoginButton;
private javax.swing.JTextField jPassword;
private javax.swing.JTextField jUsername;
// End of variables declaration

}


---

I have tried several event handlers on the textfields but getText() doesn't return anything in each case when the applet is run in a browser or appletviewer.

I would greatly appreciate any help in figuring this out.

Thanks,
Andy
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 14 2007
Added on Jun 15 2007
2 comments
1,674 views