The Artima Developer Community
Sponsored Link

Java Answers Forum
Listener Problem(Not working well)

1 reply on 1 page. Most recent reply: Jul 3, 2002 1:32 AM by Trung

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
DAYO

Posts: 22
Nickname: done
Registered: Jun, 2002

Listener Problem(Not working well) Posted: Jul 2, 2002 6:26 PM
Reply to this message Reply
Advertisement
I have the following piece of code.
I want B1.setEnabled(false) as long as all the textfield are empty and B1.setEnabled(true) when any of the testfield has something in it.
If i type in one textfield it sets B1.setEnabled(true)
but when i type & delete in another textfield it sets
B1.setEnabled(false).
I want B1.setEnabled(false) ONLY WHEN all the text
are empty.
Any ideas.
Here is the code i've used:
public construcorname()
{ T1=new JTextField[4];
testbhandler tt1=new testbhandler();
for(tf=0;tf<T1.length;tf++)
{ T1[tf]=new JTextField();
T1[tf].getDocument().addDocumentListener(new MyDocumentListener());
}
}
class MyDocumentListener implements DocumentListener
{ public void insertUpdate(DocumentEvent e)
{ B1.setEnabled(true);
}
public void removeUpdate(DocumentEvent e)
{ B1.setEnabled(false);
}
public void changedUpdate(DocumentEvent e)
{
//Plain text components don't fire these events.
}
}


Trung

Posts: 9
Nickname: chitrung
Registered: Jun, 2002

Re: Listener Problem(Not working well) Posted: Jul 3, 2002 1:32 AM
Reply to this message Reply
hi,
may be this help you...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
 
public class frame {
	JFrame frame = new JFrame("Test");
	Container c = frame.getContentPane();
	JPanel panel = new JPanel();
	JTextField textfield = new JTextField(20);
	JButton button = new JButton("Button");
 
	public void openFrame() {
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		c.add(panel,BorderLayout.CENTER);
		panel.setLayout(new BorderLayout());
		panel.add(textfield, BorderLayout.NORTH);
		panel.add(button, BorderLayout.CENTER);
				
		textfield.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent e) {
				if (textfield.getCaretPosition() == 0 && textfield.getText().equals("")) {
					button.setEnabled(false);
				}else button.setEnabled(true);
			}
		});
	
		button.setEnabled(false);
		frame.pack();
		frame.show();
	}
		
	public static void main(String args[]) {
		frame newframe = new frame();
		newframe.openFrame();
	}
}

Flat View: This topic has 1 reply on 1 page
Topic: File and Textfield Selection Previous Topic   Next Topic Topic: Download web Page from java application

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use