1.11M Members

text field monitoring

 
0
 
import java.awt.*;  

02 import javax.swing.*;  

03 import java.util.Random;  

04 import java.awt.event.*;  

05 import java.awt.Color;  

06 import java.awt.Graphics;  

07 public class actualstuff extends JFrame  

08 {  

09     private JTextField textField1;  

10     public static int index=0;  

11     public static Graphics gx;  

12     public actualstuff()  

13     {  

14         Container container=getContentPane();  

15         container.setLayout(new FlowLayout());  

16         textField1=new JTextField(15);  

17         container.add(textField1);  

18         setSize(325, 100);  

19         setVisible(true);  

20         TextFieldHandler handler=new TextFieldHandler();  

21         textField1.addActionListener(handler);  

22     }  

23     public static void main(String args[])  

24     {  

25         actualstuff application=new actualstuff();  

26         application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

27     }  

28     private class TextFieldHandler implements ActionListener  

29     {  

30         public void actionPerformed(ActionEvent event)  

31         {  

32             String string="";  

33             if (event.getSource()==textField1)  

34             {  

35                 index=1;  

36             }  

37         }  

38     }  

I want to make a text field and monitor it for when enter is pressed. when I change public static void main to public void init, it works properly, but it does not work with main, and i want this not to be an applet.

eventually, i will add a loop that runs until index is changed to 1 (starts at 0), and index is changed when enter is hit from the text field. Where should that that loop be placed?

 
0
 

Can you post code without the numbers at the start of each line?

public void init,

That should not compile.

Can you explain what you are talking about? main() and applet ???

If you are getting compiler error messages, please copy the full text and paste them here.

 
0
 

refer this code which will alert when press Enter

import com.sun.corba.se.impl.protocol.giopmsgheaders.KeyAddr;
import java.awt.*;
import javax.swing.*;
import java.util.Random;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;

public class actualstuff extends JFrame {

    private JTextField textField1;
    public static int index = 0;
    public static Graphics gx;

    public actualstuff() {
        Container container = getContentPane();
        container.setLayout(new FlowLayout());
        textField1 = new JTextField(15);
        container.add(textField1);
        setSize(325, 100);
        setVisible(true);
        TextFieldHandler handler = new TextFieldHandler();
        TextFieldKeyHandler fieldKeyHandler = new TextFieldKeyHandler();
        textField1.addActionListener(handler);
        textField1.addKeyListener(fieldKeyHandler);
    }

    public static void main(String args[]) {
        actualstuff application = new actualstuff();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private class TextFieldHandler implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            String string = "";
            if (event.getSource() == textField1) {
                index = 1;
            }
        }
    }

    private class TextFieldKeyHandler extends KeyAdapter {

        public void keyPressed(KeyEvent e) {

            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                JOptionPane.showMessageDialog(null, "Hiiii " + textField1.getText());
            }
        }
    }
}
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: