?
Solved

Button handling and display of data in JAVA

Posted on 2004-10-06
1
Medium Priority
?
256 Views
Last Modified: 2010-03-31
The objective of my clients server program is upon connecting to the server , client should get some weather data and it should display them ..It was sucessful in doing that.But, now I have added a button get weather info and tried to get the data which is sent by my server into the textboxes on client side.But, now I am unable to display the data.Can any one add the code  and do it.

My server code:
------------------------------------------------------------
Server..
--------------------
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Server extends JFrame
{


     private JTextField textConnection;
     private JLabel labelConnection;
     Container container = null;


     public Server()

     {

           container = getContentPane() ;
           container.setLayout(new FlowLayout());

           labelConnection = new JLabel("Connection ") ;
           container.add(labelConnection );
           textConnection = new JTextField(15) ;
           container.add(textConnection);
           textConnection.setText("Disconnected");

          setSize ( 200,400) ;
          setVisible(true);

          try
          {
               //creating server socket binding at port # 3000
               ServerSocket server=new ServerSocket(3000);
               System.out.println("Server binded at "+((server.getInetAddress()).getLocalHost()).getHostAddress()+":3000");
               System.out.println("Run the Client");
               //ready to accept client request
               Socket socket=server.accept();
               textConnection.cut();
               textConnection.setText("Connected\n");


               //opening the input stream to read data from client connection
               BufferedReader in= new BufferedReader(new InputStreamReader(socket.getInputStream()));
               System.out.println(in.readLine());
               //using output stream responsing data
               DataOutputStream out=new DataOutputStream(socket.getOutputStream());
               while (true)
               {
                  for (int i=0; i<8; i++)
                  {
                      int s =(int)(Math.random()*10);
                      out.writeInt((s*10)+20);
                  }
                  out.flush();
               }
               //closing the in & out streams
                  //out.close
            //   in.close();
          }
          catch(Exception err)
          {
               System.err.println("* err"+err);
               textConnection.cut();
               textConnection.setText("Disconnected\n");
          }

     }

     public static void main(String a[])
     {
          new Server();
     }

}

Client
----------------------------------------------------------------------
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Client extends JFrame

{
     private JTextField textZipcode,textsky,texttemp,texthumidity,textwindspeed,textbarometer,textdew,textheat,textvisibility;
     private JLabel labelZipcode,labelsky,labeltemp,labelhumidity,labelwindspeed,labelbarometer,labeldew,labelheat,labelvisibility;
      private JButton buttonWeather;


     public Client()
     {
                    Container container = getContentPane() ;
                    container.setLayout(new FlowLayout());


                    labelZipcode = new JLabel("ZipCode ") ;
                     container.add(labelZipcode );
                     textZipcode = new JTextField(15) ;
                    container.add(textZipcode);


                    buttonWeather = new JButton(" Get Weather Info ");
                  container.add(buttonWeather) ;


                     labelsky = new JLabel("Sky ") ;
                     container.add(labelsky );
                     textsky = new JTextField(15) ;
                    container.add(textsky);


                    labeltemp = new JLabel("Temp ") ;
                    container.add(labeltemp );
                    texttemp = new JTextField(15) ;
                    container.add(texttemp );



                    labelhumidity = new JLabel("Humidity ") ;
                    container.add(labelhumidity );
                    texthumidity = new JTextField(15) ;
                    container.add(texthumidity );

                    labelwindspeed = new JLabel("Wind speed ") ;
                    container.add(labelwindspeed );
                    textwindspeed = new JTextField(15) ;
                    container.add(textwindspeed );

                    labelbarometer = new JLabel("Barometer ") ;
                    container.add(labelbarometer );
                    textbarometer = new JTextField(15) ;
                    container.add(textbarometer );

                    labeldew = new JLabel("Dew ") ;
                    container.add(labeldew );
                    textdew = new JTextField(15) ;
                    container.add(textdew );

                    labelheat = new JLabel("Heat ") ;
                    container.add(labelheat );
                    textheat = new JTextField(15) ;
                    container.add(textheat );

                    labelvisibility = new JLabel("Visibillity ") ;
                    container.add(labelvisibility );
                    textvisibility = new JTextField(15) ;
                    container.add(textvisibility );



                    setSize ( 200,600) ;
                    setVisible(true);


// Event handler for handing the button events
          ButtonHandler handler = new ButtonHandler();
          buttonWeather.addActionListener( handler );

     }

          // Button Click Event Handler
           class ButtonHandler implements ActionListener
           {
                public void actionPerformed(ActionEvent event)
                {
                     // After pressing show Status, varios checks and the final message is built
                     if (event.getSource() == buttonWeather)
                     {
                              try
          {
               //ceating the socket to connect to server running on same machine binded on port no 3000
               Socket client=new Socket("localhost",3000);
               System.out.println("Client connected ");

               //getting the o/p stream of that connection
               PrintStream out=new PrintStream(client.getOutputStream());
               //sending the message to server
               out.print("Hello from client\n");
               out.flush();
               //reading the response using input stream
               DataInputStream in= new DataInputStream(client.getInputStream());
               while (true)
               {
                       if(in.readInt()>20 && in.readInt()<40)
                  textsky.setText("Sky Condition: "+"Clear");
                         if(in.readInt()>40 && in.readInt()<80)
                      textsky.setText("Sky Condition: "+"Partly Cloudy");
                           else
                      textsky.setText("Sky Condition: "+"Cloudy");
                  texttemp.setText("Temperature: "+in.readInt());
                  texthumidity.setText("Humidity: "+in.readInt());
                  textwindspeed.setText("Wind Speed: "+in.readInt());
                  textbarometer.setText("Barometer: "+in.readInt());
                  textdew.setText("Dewpoint: "+in.readInt());
                  textheat.setText("Heat Index: "+in.readInt());
                  textvisibility.setText("Visibility: "+in.readInt());
                  Thread.sleep(5000);
               }

               //closing the streams
               //in.close();
               //out.close();

          }
          catch(Exception err)
          {
               System.err.println("* err"+err);
          }
                     }

               }

          }

     public static void main(String a[])
     {
          new Client();
     }

}
0
Comment
Question by:prashanth_gurijala
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 2

Accepted Solution

by:
ThummalaRaghuveer earned 2000 total points
ID: 12244922
May be this code at Client will do


import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Client extends JFrame

{
      private JTextField textZipcode, textsky, texttemp, texthumidity,
                  textwindspeed, textbarometer, textdew, textheat, textvisibility;

      private JLabel labelZipcode, labelsky, labeltemp, labelhumidity,
                  labelwindspeed, labelbarometer, labeldew, labelheat,
                  labelvisibility;

      private JButton buttonWeather;
      DataInputStream in = null;
      private boolean getWeather = false;

      public Client() {
            Container container = getContentPane();
            container.setLayout(new FlowLayout());

            labelZipcode = new JLabel("ZipCode ");
            container.add(labelZipcode);
            textZipcode = new JTextField(15);
            container.add(textZipcode);

            buttonWeather = new JButton(" Get Weather Info ");
            container.add(buttonWeather);

            labelsky = new JLabel("Sky ");
            container.add(labelsky);
            textsky = new JTextField(15);
            container.add(textsky);

            labeltemp = new JLabel("Temp ");
            container.add(labeltemp);
            texttemp = new JTextField(15);
            container.add(texttemp);

            labelhumidity = new JLabel("Humidity ");
            container.add(labelhumidity);
            texthumidity = new JTextField(15);
            container.add(texthumidity);

            labelwindspeed = new JLabel("Wind speed ");
            container.add(labelwindspeed);
            textwindspeed = new JTextField(15);
            container.add(textwindspeed);

            labelbarometer = new JLabel("Barometer ");
            container.add(labelbarometer);
            textbarometer = new JTextField(15);
            container.add(textbarometer);

            labeldew = new JLabel("Dew ");
            container.add(labeldew);
            textdew = new JTextField(15);
            container.add(textdew);

            labelheat = new JLabel("Heat ");
            container.add(labelheat);
            textheat = new JTextField(15);
            container.add(textheat);

            labelvisibility = new JLabel("Visibillity ");
            container.add(labelvisibility);
            textvisibility = new JTextField(15);
            container.add(textvisibility);

            setSize(200, 600);
            setVisible(true);

            // Event handler for handing the button events
            ButtonHandler handler = new ButtonHandler();
            buttonWeather.addActionListener(handler);

      }
      
      public void getWeather() throws IOException {
            while (true) {
                  if(getWeather) {
                  int t1 = in.readInt();
                  int t2 = in.readInt();
                  int t3 = in.readInt();
                  int t4 = in.readInt();
                  System.out.println("Received "+t1+" "+t2+" "+t3+" "+t4);
                  if (t1 > 20 && t2 < 40){
                        
                        System.out.println("in 1");
                        textsky.setText("Sky Condition: " + "Clear");
                  }
                  if (t3 > 40 && t4 < 80) {
                        textsky
                                    .setText("Sky Condition: "
                                                + "Partly Cloudy");
                        System.out.println("in 2");
                  }
                  else{
                        textsky.setText("Sky Condition: " + "Cloudy");
                        System.out.println("in 3");
                  }
                  texttemp.cut();
                  texttemp.setText("Temperature: " + in.readInt()+"\n");
                  //texttemp.setText("Temperature:");
                  System.out.println("In last");
                  texthumidity.setText("Humidity: " + in.readInt());
                  textwindspeed.setText("Wind Speed: " + in.readInt());
                  textbarometer.setText("Barometer: " + in.readInt());
                  textdew.setText("Dewpoint: " + in.readInt());
                  textheat.setText("Heat Index: " + in.readInt());
                  textvisibility.setText("Visibility: " + in.readInt());
                  try {
                        Thread.sleep(5000);
                  } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                  }
                  }
            }
      }

      // Button Click Event Handler
      class ButtonHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                  // After pressing show Status, varios checks and the final message
                  // is built
                  if (event.getSource() == buttonWeather) {
                        try {
                              //ceating the socket to connect to server running on same
                              // machine binded on port no 3000
                              Socket client = new Socket("localhost", 3000);
                              System.out.println("Client connected ");

                              //getting the o/p stream of that connection
                              PrintStream out = new PrintStream(client.getOutputStream());
                              //sending the message to server
                              out.print("Hello from client\n");
                              out.flush();
                              //reading the response using input stream
                              in = new DataInputStream(client
                                          .getInputStream());
                              //System.out.println("haha.."+in.readInt());
                              


                              //closing the streams
                              //in.close();
                              //out.close();
                              getWeather = true;

                        } catch (Exception err) {
                              System.err.println("* err" + err);
                        }
                  }

            }

      }

      public static void main(String a[]) {
            Client abc = new Client();
            try {
                  abc.getWeather();
            } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }

      }

}
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

801 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