event handling with button

Hi All,

I am trying to write an event handling on my button, but i seem dont know where to start from. My code are below:

import java.net.*;

import java.io.*;

public class ReadWrite

{

public static void main(String[] args) throws Exception {

URL myLocalhost = new URL("http://localhost/Write.pl");

URLConnection myURL = myLocalhost.openConnection();

BufferedReader myBR = new BufferedReader(

new InputStreamReader(

myURL.getInputStream()));

String inputLine;

while ((inputLine = myBR.readLine()) != null)

System.out.println(inputLine);

myBR.close();

}

}

--

I want to write a code with a button to run the above code.

Could you please advise?

Thank for any consideration you may give me.

[821 byte] By [Scrollbara] at [2007-10-2 17:06:55]
# 1

here's the code to read a file, modify for your url connection

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.io.*;

class Testing extends JFrame implements ActionListener

{

public Testing()

{

setLocation(400,300);

setSize(200,100);

setDefaultCloseOperation(EXIT_ON_CLOSE);

JPanel panel = new JPanel();

JButton btn = new JButton("Read/Write");

btn.addActionListener(this);

panel.add(btn);

getContentPane().add(panel);

}

public void actionPerformed(ActionEvent ae)

{

BufferedReader myBR = null;

try

{

myBR = new BufferedReader(new FileReader("Test.txt"));

String inputLine;

while ((inputLine = myBR.readLine()) != null)

{

System.out.println(inputLine);

}

myBR.close();

}

catch(Exception e){e.printStackTrace();}

}

public static void main(String args[]){new Testing().setVisible(true);}

}

Michael_Dunna at 2007-7-13 18:21:38 > top of Java-index,Security,Event Handling...