Java - Problemas para desactivar JTextField

 
Vista:
sin imagen de perfil
Val: 9
Ha aumentado 1 puesto en Java (en relación al último mes)
Gráfica de Java

Problemas para desactivar JTextField

Publicado por guille (14 intervenciones) el 29/06/2015 18:41:40
Buenas Tardes.
Seguramente haya alguna solución a este problema en el foro pero no he sido capaz de encontrarla.

Estoy aprendiendo a programar Java y he desarrollado un JFrame con un menú el cual llama a un JDialog que incluye Labels, TextFields y botones.

Deseo que al pulsar uno de los botones se deshabiliten (setEnabled) los JTextField.
Lo programé con un método de la clase del JDialog, pero no funciona.

Si por ejemplo reemplazo esa linea de codigo con un setEnabled para deshabilitar un botón, si funciona.

Desde ya muchas Gracias.
Saludos
Guillermo
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Problemas para desactivar JTextField

Publicado por omar (56 intervenciones) el 29/06/2015 22:56:03
Hola, para hacer un JTextField no Editable debes usar EdiTable (false).

JTextField texto=new JTextField ();
Public void actionPerfirmed (ActionEvent e){
If (e.getSource ()==boton){
Texto.setentaEditable (false)
}
}



Espero t sirva. Sino, pone el.código así t podemos dar una mano. Suerte!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 9
Ha aumentado 1 puesto en Java (en relación al último mes)
Gráfica de Java

Problemas para desactivar JTextField

Publicado por guille (14 intervenciones) el 29/06/2015 23:20:04
Muchas Gracias OMAR.

Adjunto el código y te explico un poco mejor.
Quisiera que al pulsar el boton Agregar (btnAgregar) se deshabiliten los Jtextbox llamando al metodo CambiodeEstado().

Probé con setEnable() y setEditable pero no funciona.
Si por ejemplo en vez de cambiar el estado del textbox intento cambiar el estado del button, si funciona.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
public class Formu extends JDialog implements ActionListener{
 
	private JMenuBar menuPrin;
	private JMenu Tiendas,Salir;
	private JMenuItem Tiendas_Tiendas,Salir_Salir;
	static JFrame formPrin;
 
	public Formu(){
		formPrin=new JFrame("Sistema de entregas");
		setLayout(null);
		this.getContentPane().setBackground(new Color(35,163,215));
		menuPrin=new JMenuBar();
		setJMenuBar(menuPrin);
 
		Tiendas=new JMenu("Tiendas");
		menuPrin.add(Tiendas);
 
		Tiendas_Tiendas=new JMenuItem("Tiendas");
		Tiendas_Tiendas.addActionListener(this);
		Tiendas.add(Tiendas_Tiendas);
 
		Salir=new JMenu("Salir");
		menuPrin.add(Salir);
 
		Salir_Salir=new JMenuItem("Salir");
		Salir_Salir.addActionListener(this);
		Salir.add(Salir_Salir);
	}
 
	public void actionPerformed(ActionEvent opcion) {
 
		if(opcion.getSource()==Salir_Salir){
			System.exit(0);
		}
		if(opcion.getSource()==Tiendas_Tiendas){
			tienda tie=new tienda();
			tie.tienda();
		}
	}
	public static void main(String[] ar){
		Formu formPrin=new Formu();
		formPrin.setBounds(10,10,800,650);
		formPrin.setVisible(true);
	}
 
// *** CLASE tienda ***
 
public class tienda extends JDialog implements ActionListener {
 
	private JButton btnAgregar,btnSalir;
	private JLabel lblID,lblNombre,lblDireccion,lblTelefono,lblMail,lblContacto1,lblContacto2,lblContacto3;
	public  JTextField txtID;
	private JTextField txtNombre;
	private JTextField txtDireccion;
	private JTextField txtTelefono;
	private JTextField txtMail;
	private JTextField txtContacto1;
	private JTextField txtContacto2;
	private JTextField txtContacto3;
 
	public void tienda(){
		JDialog JD=new JDialog(this,true);
		JD.setLayout(null);
		JD.setBounds(25, 25, 650, 500);
		JD.setTitle("TIENDAS");
 
 
		btnAgregar=new JButton("AGREGAR");
		btnAgregar.setBounds(new Rectangle(15, 350, 120, 35));
		btnAgregar.addActionListener(this);
		JD.add(btnAgregar);
 
		lblID=new JLabel(" ID TIENDA");
		lblID.setBounds(15,10,150,30);
		JD.add(lblID);
 
		lblNombre=new JLabel(" TIENDA");
		lblNombre.setBounds(15,45,150,30);
		JD.add(lblNombre);
 
		lblDireccion=new JLabel(" DIRECCIÓN");
		lblDireccion.setBounds(15,80,150,30);
		JD.add(lblDireccion);
 
		lblTelefono=new JLabel(" TELÉFONO");
		lblTelefono.setBounds(15,115,150,30);
		JD.add(lblTelefono);
 
		lblMail=new JLabel(" CORREO ELECT.");
		lblMail.setBounds(15,150,150,30);
		JD.add(lblMail);
 
		lblContacto1=new JLabel(" PERSONA DE CONTACTO");
		lblContacto1.setBounds(15,185,150,30);
		JD.add(lblContacto1);
 
		lblContacto2=new JLabel(" PERSONA DE CONTACTO");
		lblContacto2.setBounds(15,220,150,30);
		JD.add(lblContacto2);
 
		lblContacto3=new JLabel(" PERSONA DE CONTACTO");
		lblContacto3.setBounds(15,255,150,30);
		JD.add(lblContacto3);
 
		JTextField txtID=new JTextField();
		txtID.setBounds(170,10,100,25);
		JD.add(txtID);
 
		JTextField txtNombre=new JTextField();
		txtNombre.setBounds(170,45,250,25);
 
		JD.add(txtNombre);
 
		JTextField txtDireccion=new JTextField();
		txtDireccion.setBounds(170,80,310,25);
 
		JD.add(txtDireccion);
 
		JTextField txtTelefono=new JTextField();
		txtTelefono.setBounds(170,150,250,25);
		JD.add(txtTelefono);
 
		JTextField txtMail=new JTextField();
		txtMail.setBounds(170,115,310,25);
		JD.add(txtMail);
 
		JTextField txtContacto1=new JTextField();
		txtContacto1.setBounds(170,185,250,25);
		JD.add(txtContacto1);
 
		JTextField txtContacto2=new JTextField();
		txtContacto2.setBounds(170,220,250,25);
		JD.add(txtContacto2);
 
		JTextField txtContacto3=new JTextField();
		JD.add(txtContacto3);
		JD.setVisible(true);
 
	}
	public void CambiodeEstado(int a){
		if(a==1){
			txtID.setEditable(false);
		}
	}
	public void actionPerformed(ActionEvent op) {
		if(op.getSource()==btnSalir){
		}
		if(op.getSource()==btnAgregar){
			CambiodeEstado(1);
		}
	}
}
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Problemas para desactivar JTextField

Publicado por omar (56 intervenciones) el 30/06/2015 01:08:32
Hola, tu problema está aquí:

definiste un campo

public JTextField txtID;

y luego mas abajo lo volviste a definir y lo sobreescribiste, aca:

JTextField txtID=new JTextField();
txtID.setBounds(170,10,100,25);
JD.add(txtID);

quitando aqui JTextField y dejando:

txtID=new JTextField();
txtID.setBounds(170,10,100,25);
JD.add(txtID);

funciona perfecto....
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
sin imagen de perfil
Val: 9
Ha aumentado 1 puesto en Java (en relación al último mes)
Gráfica de Java

Problemas para desactivar JTextField

Publicado por guille (14 intervenciones) el 30/06/2015 13:44:57
Mil Gracias Omar.
Funciona de maravillas, ya molestaré en el futuro con otra consulta.

Saludos
Guillermo
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
sin imagen de perfil

Problemas para desactivar JTextField

Publicado por omar (56 intervenciones) el 30/06/2015 19:05:22
De nada! Saludos
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar