habilitar y deshabilitar botones con setEnabled()
Hola a todos mi problema es que necesito deshabiltar un boton, y hasta que el usuario introdusca la contraseña correcta se devera habilitar de nuevo el boton, estoy usando botton.setEnabled(true), pero no se habilita el boton se queda deshablitado; aqui esta el codigo que estoy utilizando.
Necesito ayuda ya no se que mas hacer Gracias a todos.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.net.MalformedURLException;
public class login11 extends Applet
implements ActionListener {
URLWindow urlWindow;
TextField password, mensajes,otro;
Label l;
String txt=\"asd\";
String tex1=\"\";
Button button;
public void init() {
l = new Label(\"Introduzca password:\");
password = new TextField(\"\",10);
mensajes = new TextField(\"\",40);
otro=new TextField(\"\",20);
password.setEchoCharacter(\\'*\\');
mensajes.setEditable( false );
add(l);
add(password);
add(mensajes);
add(otro);
Button button = new Button(\"Comenzar\");
button.addActionListener(this);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
accion1();}});
add(button);
button.setEnabled(true);
urlWindow = new URLWindow(getAppletContext());
urlWindow.pack();
}
public boolean action ( Event e, Object a )
{
AppletContext appletContext;
if (e.target==password) { mensajes.setText(\"e.target=List\"); }
tex1=password.getText();
if(tex1.equals(\"asd\")){
otro.setText(tex1);
mensajes.setText(\"ganastexxxxxxxxxxxxxxxxx\");
button.setEnabled(e);
//accion1();
return true;}
return true;
}
public void accion1(){
button.setEnabled(true);
}
public void destroy() {
urlWindow.setVisible(false);
urlWindow = null;
}
public void actionPerformed(ActionEvent event) {
urlWindow.setVisible(true);
}
}
class URLWindow extends Frame
implements ActionListener {
TextField urlField;
Choice choice;
AppletContext appletContext;
public URLWindow(AppletContext appletContext) {
super(\"Show a Document!\");
this.appletContext = appletContext;
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gridBag);
Label label1 = new Label(\"La pagina principal es:\",
Label.RIGHT);
gridBag.setConstraints(label1, c);
add(label1);
urlField = new TextField(\"http://127.0.0.1/inf.php\", 40);
urlField.addActionListener(this);
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
gridBag.setConstraints(urlField, c);
add(urlField);
Label label2 = new Label(\"Donde deseas iniciar el SIC:\",
Label.RIGHT);
c.gridwidth = 1;
c.weightx = 0.0;
gridBag.setConstraints(label2, c);
add(label2);
choice = new Choice();
choice.addItem(\"Selecciona\"); //don\\'t specify
//choice.addItem(\"My Personal Window\"); //a window named
//\"My Personal Window\"
choice.addItem(\"Pagina Nueva\"); //a new, unnamed window
//choice.addItem(\"_self\");
choice.addItem(\"En esta ventana\");
//choice.addItem(\"_top\"); //the Frame that contained this
//applet
c.fill = GridBagConstraints.NONE;
c.gridwidth = GridBagConstraints.REMAINDER;
c.anchor = GridBagConstraints.WEST;
gridBag.setConstraints(choice, c);
add(choice);
Button button = new Button(\"Iniciar SIC\");
button.addActionListener(this);
c.weighty = 1.0;
c.ipadx = 10;
c.ipady = 10;
c.insets = new Insets(5,0,0,0);
c.anchor = GridBagConstraints.SOUTH;
gridBag.setConstraints(button, c);
add(button);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
setVisible(false);
}
});
}
public void actionPerformed(ActionEvent event) {
String urlString = urlField.getText();
URL url = null;
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
System.err.println(\"Malformed URL: \" + urlString);
}
if (url != null) {
if (choice.getSelectedIndex() == 0) {
appletContext.showDocument(url);
} else {
appletContext.showDocument(url,
choice.getSelectedItem());
}
}
}
}
Necesito ayuda ya no se que mas hacer Gracias a todos.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.net.MalformedURLException;
public class login11 extends Applet
implements ActionListener {
URLWindow urlWindow;
TextField password, mensajes,otro;
Label l;
String txt=\"asd\";
String tex1=\"\";
Button button;
public void init() {
l = new Label(\"Introduzca password:\");
password = new TextField(\"\",10);
mensajes = new TextField(\"\",40);
otro=new TextField(\"\",20);
password.setEchoCharacter(\\'*\\');
mensajes.setEditable( false );
add(l);
add(password);
add(mensajes);
add(otro);
Button button = new Button(\"Comenzar\");
button.addActionListener(this);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
accion1();}});
add(button);
button.setEnabled(true);
urlWindow = new URLWindow(getAppletContext());
urlWindow.pack();
}
public boolean action ( Event e, Object a )
{
AppletContext appletContext;
if (e.target==password) { mensajes.setText(\"e.target=List\"); }
tex1=password.getText();
if(tex1.equals(\"asd\")){
otro.setText(tex1);
mensajes.setText(\"ganastexxxxxxxxxxxxxxxxx\");
button.setEnabled(e);
//accion1();
return true;}
return true;
}
public void accion1(){
button.setEnabled(true);
}
public void destroy() {
urlWindow.setVisible(false);
urlWindow = null;
}
public void actionPerformed(ActionEvent event) {
urlWindow.setVisible(true);
}
}
class URLWindow extends Frame
implements ActionListener {
TextField urlField;
Choice choice;
AppletContext appletContext;
public URLWindow(AppletContext appletContext) {
super(\"Show a Document!\");
this.appletContext = appletContext;
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gridBag);
Label label1 = new Label(\"La pagina principal es:\",
Label.RIGHT);
gridBag.setConstraints(label1, c);
add(label1);
urlField = new TextField(\"http://127.0.0.1/inf.php\", 40);
urlField.addActionListener(this);
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
gridBag.setConstraints(urlField, c);
add(urlField);
Label label2 = new Label(\"Donde deseas iniciar el SIC:\",
Label.RIGHT);
c.gridwidth = 1;
c.weightx = 0.0;
gridBag.setConstraints(label2, c);
add(label2);
choice = new Choice();
choice.addItem(\"Selecciona\"); //don\\'t specify
//choice.addItem(\"My Personal Window\"); //a window named
//\"My Personal Window\"
choice.addItem(\"Pagina Nueva\"); //a new, unnamed window
//choice.addItem(\"_self\");
choice.addItem(\"En esta ventana\");
//choice.addItem(\"_top\"); //the Frame that contained this
//applet
c.fill = GridBagConstraints.NONE;
c.gridwidth = GridBagConstraints.REMAINDER;
c.anchor = GridBagConstraints.WEST;
gridBag.setConstraints(choice, c);
add(choice);
Button button = new Button(\"Iniciar SIC\");
button.addActionListener(this);
c.weighty = 1.0;
c.ipadx = 10;
c.ipady = 10;
c.insets = new Insets(5,0,0,0);
c.anchor = GridBagConstraints.SOUTH;
gridBag.setConstraints(button, c);
add(button);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
setVisible(false);
}
});
}
public void actionPerformed(ActionEvent event) {
String urlString = urlField.getText();
URL url = null;
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
System.err.println(\"Malformed URL: \" + urlString);
}
if (url != null) {
if (choice.getSelectedIndex() == 0) {
appletContext.showDocument(url);
} else {
appletContext.showDocument(url,
choice.getSelectedItem());
}
}
}
}
¿Por qué no utilizas jascript? Te he hecho un modelo que te puede servir. Date cuenta que en validacionServlet debe comprobar que es correcta la validación y volveríamos a esta misma JSP.
En accesoServlet nos iríamos directamente a otra JSP.
<html>
<%
Boolean correcto = (Boolean)session.getAttribute();
boolean bCorrecto = correcto.booleanValue();
String usuario = request.getParameter("usuario");
String password = request.getParameter("password");
%>
<head>
<script language="javascript">
jsCorrecto = "<%=bCorrecto%>";
function inicializarValores() {
if (jsCorrecto) {
document.passForm.usuario.value = "<%=usuario%>";
document.passForm.password.value = "<%=password%>";
document.passForm.boton.disabled = "true";
}
}
function comprobar() {
alert(document.passForm.password.value);
if (document.passForm.usuario.value != "" && document.passForm.password.value != "") {
document.passForm.action = "servidor.validacionServlet";
document.passForm.submit();
}
}
function enviarJSP() {
document.location.href = "servidor.accesoServlet";
}
</script>
</head>
<body onLoad="inicializarValores();">
<form name="passForm" method="Post" action="servidor.nombreServlet"><br>
<input type="text" name="usuario" value="" onChange="comprobar();"><br>
<input type="text" name="password" value="" onChange="comprobar();"><br>
<input type="button" name="boton" value="Pulsar" onClick="enviarJSP()" disabled>
</form>
</body>
</html>
En accesoServlet nos iríamos directamente a otra JSP.
<html>
<%
Boolean correcto = (Boolean)session.getAttribute();
boolean bCorrecto = correcto.booleanValue();
String usuario = request.getParameter("usuario");
String password = request.getParameter("password");
%>
<head>
<script language="javascript">
jsCorrecto = "<%=bCorrecto%>";
function inicializarValores() {
if (jsCorrecto) {
document.passForm.usuario.value = "<%=usuario%>";
document.passForm.password.value = "<%=password%>";
document.passForm.boton.disabled = "true";
}
}
function comprobar() {
alert(document.passForm.password.value);
if (document.passForm.usuario.value != "" && document.passForm.password.value != "") {
document.passForm.action = "servidor.validacionServlet";
document.passForm.submit();
}
}
function enviarJSP() {
document.location.href = "servidor.accesoServlet";
}
</script>
</head>
<body onLoad="inicializarValores();">
<form name="passForm" method="Post" action="servidor.nombreServlet"><br>
<input type="text" name="usuario" value="" onChange="comprobar();"><br>
<input type="text" name="password" value="" onChange="comprobar();"><br>
<input type="button" name="boton" value="Pulsar" onClick="enviarJSP()" disabled>
</form>
</body>
</html>