Question: i havei) aJTextField and one button. when place the cursor or enter anything in the test field then only button should be enabled.by default the button should be disabled.

Ads

 
 
 

Share on Google+Share on Google+

raveen
i havei) aJTextField and one button. when place the cursor or enter anything in the test field then only button should be enabled.by default the button should be disabled.
1 Answer(s)      4 years and 4 months ago
Posted in : Swing AWT

i havei) aJTextField and one button. when place the cursor or enter anything in the test field then only button should be enabled.by default the button should be disabled.

how to add listeners for it. thx advance.

Ads
View Answers

May 31, 2013 at 5:09 PM


hi friend,

you can add listener by implementing the listener interfaces using implements keyword in Java.

Following code snippet may help you understand how can you add listeners in your classes.

package net.roseindia.swingExample;

import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.JLabel; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener;

public class TextFiledAndButtonListener implements MouseListener, MouseMotionListener, KeyListener{

JFrame frame;
JButton button;
JTextField textField;
JLabel label;

public void createUI()
{
    frame = new JFrame("Text Field Listener");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(null);
    frame.setSize(400,300);

    label = new JLabel("Enter Text Here ");
    label.setBounds(10,30,100, 20);

    textField = new JTextField();
    textField.setBounds(110, 30, 100, 20);      
    textField.addMouseMotionListener(this);
    textField.addMouseListener(this);       
    textField.addKeyListener(this);

    button = new JButton("Click Here");
    button.setEnabled(false);
    button.setBounds(220,30,100,20);
    frame.add(label);
    frame.add(textField);
    frame.add(button);

    frame.setVisible(true);     
}
@Override
public void mouseDragged(MouseEvent e) {        
}

@Override
public void mouseMoved(MouseEvent e) {
    button.setEnabled(true);        
}

@Override
public void keyTyped(KeyEvent e) {
    button.setEnabled(true);
}

@Override
public void keyPressed(KeyEvent e) {
    System.out.println("Key is Pressed");       
}

@Override
public void keyReleased(KeyEvent e) {
    System.out.println("Key is Released");      
}

public static void main(String args[])
{
    TextFiledAndButtonListener ta = new TextFiledAndButtonListener();
    ta.createUI();
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {       
}
@Override
public void mouseEntered(MouseEvent e) {        

}
@Override
public void mouseExited(MouseEvent e) {     
    button.setEnabled(false);       
}

}


Ads









Related Tutorials/Questions & Answers:
Tutorials   
Java Spring Hibernate Struts Training Change JPanel background color from mouse entered/exit event JTable Pagination Jcombobox JAVA SWING how can i display a pdf file in a jtextarea Crop Image Code in Applet Crop Image Code in Applet Image Manipulation in Swing AWT Applet Ho to get virtual key code. how to show set of images continously using JScrollpane in java how to show set of images continously using JScrollpane in java How to connect on screen keyboard to system. How to connect on screen keyboard to system. query java client server program for playing video file(stored in folder in the same workspace) using swings java client server program for playing video file(stored in folder in the same workspace) using swings java client server program for playing video file(stored in folder in the same workspace) using swings java client server program for playing video file(stored in folder in the same workspace) using swings Download. how to open one Jframe from main method call Urgent requirement display shapes i am getting an error in adding a menu item to a menu. I am trying to develop a Image Sliding application but unfortunately its not working... Can any one help me in this Sliding window protocol how to update the value of jslider how to update the value of jslider how to update the value of jslider how to update the value of jslider how to update the value of jslider Jbutton in JTable cells program code java textfield problem swing retrieve data from database in java swing form using prev n next buttons retrieve data from database in java swing form using prev n next buttons How to close a frame on action event of a button from a different class ? searching the data swings for webnms Jpanel allignment pass data from java frame to file code for traffic control system simulation Bank Management System how to maKE Jcombox editable after saveing value init. i havei) aJTextField and one button. when place the cursor or enter anything in the test field then only button should be enabled.by default the button should be disabled. i havei) a jtable with few cloumns and few rows andii) one button. when place the cursor in any of the table's column cell. i need to enable the button.by default the button should be disabled. PDF Comparator i have JTextfield and JComboBox. there are several values in combobox.when i select a value from combobox how to make textfiled as a combobox. issue on jcombobox

Ads

 
Advertisement null

Ads