?
Solved

To add event on text  field

Posted on 2009-12-25
5
Medium Priority
?
282 Views
Last Modified: 2012-05-08
Hi ,
I have created this screen (code in attachment )and I want to know  how can I add event on the test field ... in other words : I need to take any url typed  in text space and make connection to it

Thanks  in advance

package URL_reader;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Gui{
  JTextField urlTxtField;
  JLabel label ;
  public static void main(String[] args){
    Gui gui = new Gui();
  }

  public Gui(){
    JFrame frame = new JFrame("URL Reader");
    Container contentPane = frame.getContentPane();
    urlTxtField = new JTextField(20);
    JPanel panel = new JPanel();
    panel.add(urlTxtField);
    frame.add(panel, BorderLayout.CENTER);
    contentPane.add(new JButton("Read"));
    contentPane.setLayout(new FlowLayout());
    contentPane.add(new JLabel("This space to write the URL"));
    frame.setSize(310, 110);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
  }
}

Open in new window

0
Comment
Question by:obad62
  • 2
  • 2
5 Comments
 
LVL 83

Assisted Solution

by:leakim971
leakim971 earned 400 total points
ID: 26122604
Hello obad62,

You need to add addKeyListener for example to your urlTxtField : urlTxtField.addKeyListener(what2dowhenkeypressed);

Example here : http://www.java2s.com/Tutorial/Java/0240__Swing/ListeningtoJTextFieldEventswithanKeyListener.htm

Regards
0
 

Author Comment

by:obad62
ID: 26123241
Thank u leakim971  for your comment

I did this screen which is in an attachment  :

I creat two class first one to create  and second one to make  connection but the problem in call statment which is in first class

Regards
first class :
package URL_reader;

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class Gui extends Applet
                          implements ActionListener
{
        private TextField URLText = null ;
        private Button URLButton = null;

        public Gui()
        {
        }

        public void init()
        {
                add(new Label("URL"));
                URLText = new TextField(30);
                add(URLText);
                URLButton = new Button("Go");
                add(URLButton);

                URLButton.addActionListener(this);
        }

        public void actionPerformed(ActionEvent e)
        {
                String actionCommand = e.getActionCommand();
                if (e.getSource() instanceof Button)
                        if (actionCommand.equals("Go"))
                        {
                        	connection.conn();
                                System.out.println("Go");
                                try
                                {
                                        AppletContext context = getAppletContext();
                                        URL url = new URL(URLText.getText());
                                        context.showDocument(url);
                                }
                                catch(Exception ex)
                                {
                                        showStatus("Error "+ex);
                                }
                        }
        }
        

        public static void main(String[] args){
            Gui gui = new Gui();
          }
}


*******************************************
second class :
package URL_reader;

import java.net.*;
import java.io.*;

public class connection {
    public static  void conn() throws Exception {
        URL yahoo = new URL("http://www.yahoo.com/");
        URLConnection yc = yahoo.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
 public static void main (String[] args){
 }
}

Open in new window

0
 
LVL 3

Accepted Solution

by:
khdani earned 1600 total points
ID: 26123950
hi obad62,
first you need to set the action command for the button,
so after you initialize the button add:

URLButton.setActionCommand("Go");

to use your connection method, change it so it will receive a URL object argument,
and pass it the URL object, instead of declaring local.
0
 

Author Comment

by:obad62
ID: 26124064
thanks khdami ,
Actually , I did that and now it's work but as you see it connects just to the url that is in second code I want to pass the URL that entered in text field to another call to open it .
0
 
LVL 3

Assisted Solution

by:khdani
khdani earned 1600 total points
ID: 26124190
change your  'conn' method to
public static void conn(URL url)
{
URLConnection connection = url.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                connection.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);
        in.close();
}

and pass the url
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month16 days, 23 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question