Results 1 to 8 of 8
  1. #1
    alex1988 is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default Filling an array from the return value of the function

    Java Code:
    public class DaytimeApplet extends Applet {
    
    
        Button refresh;
        String h="";
        private boolean DEBUG = false;
        String data[][]=(String[][])Array.newInstance(String.class, 2,2);
    
        public void init() {
    
            setLayout(new BorderLayout());
            Panel west = new Panel();
            west.setLayout(new GridLayout(1, 1));
            DaytimeApplet tab=new DaytimeApplet();
            west.add(tab);
            add("West", west);
        }
        public DaytimeApplet() {
    
            String[] columnNames = {"Node","Region","NodeType","Alarm","Date"};
            refresh();
            String[][] data = new String[2][2];
            final JTable table = new JTable(data, columnNames);
            table.setPreferredScrollableViewportSize(new Dimension(500, 70));
            table.setFillsViewportHeight(true);
    
    
            if (DEBUG)
                {
                    table.addMouseListener(new MouseAdapter()
                        {
                            public void mouseClicked(MouseEvent e)
                            {
                                printDebugData(table);
                            }
                        });
                }
    
            JScrollPane scrollPane = new JScrollPane(table);
            add(scrollPane);
    
    
    
        }
    
    
    private void refresh() {
            Arrays.fill(data[0],getDateUsingHttpText());
            Array.set(data[0],1,"Alex");
            Array.set(data[1],0,"Alex");
            Array.set(data[1],1,"Alex");
    
            }
    
     private String getDateUsingHttpText() {
    
     try
    
       {
    
                // Construct a URL referring to the servlet
                URL url = new URL(getCodeBase(), "./DaytimeServlet");
    
                // Create a com.oreilly.servlet.HttpMessage to communicate with that URL
                HttpMessage msg = new HttpMessage(url);
    
                // Send a GET message to the servlet, with no query string
                // Get the response as an InputStream
                InputStream in = msg.sendGetMessage();
    
                // Wrap the InputStream with a DataInputStream
          DataInputStream result =
              new DataInputStream(new BufferedInputStream(in));
    
          // Read the first line of the response, which should be
          // a string representation of the current time
          String date = result.readLine();
          // Close the InputStream
    
    
    
          in.close();
               return date;
    
    }
    
    catch (Exception e) {
        // If there was a problem, print to System.out
        // (typically the Java console) and return null
        e.printStackTrace();
        return null;
        }
    
    
    
    
    
        }

    Hi all,

    Sorry for posting a bit more of a code here .Bit i would like to seek your helo regards this.Let me try to explain my question as good as i can.I am trying to create a table applet which takes the column names and the two dimensional array as an input.The applet communicates with the servlet to fetch the values from the database.the function is getDateUsingHttpText().the function returns the date correctly from the servlet but i am unable to fill the array that i am supposed to provide input to the table applet.

    The array value is always showing null.Could you Please point out where i am going wrong ?

    Thanks in advance

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    can you please also post the import statements you used to run the code. thx.

  3. #3
    alex1988 is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default

    Java Code:
    import java.net.URL;
    import com.oreilly.servlet.HttpMessage;
    import java.applet.*;
    import java.awt.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    Thanks for the Reply.Here are the import Statements that i used for this program

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    thx. the following three statements caused errors while compiling

    1) String data[][]=(String[][])Array.newInstance(String.class, 2,2);
    Error: Array cannot be resolved
    Question: Which Array from which package should be used here?

    2) printDebugData(table);
    Error: The method printDebugData(JTable) is undefined for the type new MouseAdapter(){}
    Question: Which reference sould be used for calling the method printDebugData?

    3) Array.set(data[0],1,"Alex");
    Array.set(data[1],0,"Alex");
    Array.set(data[1],1,"Alex");
    Error: Array cannot be resolved
    Question: Which Array from which package should be used here?

    could you please answer the questions?

  5. #5
    alex1988 is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default

    Thanks for being in constant touch with me.

    i found two packages that could provide the package for the array.

    import java.lang.reflect.Field;
    import java.util.Arrays;


    But i still i am not able to get that array filled .

    I have rearranged my code without using array instances since i am not getting results.

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.io.*;
    import java.util.*;
    import java.net.URL;                     // New addition
    import com.oreilly.servlet.HttpMessage;  // A support class, shown later
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    
    
    
    public class DaytimeApplet extends Applet {
    
        TextField httpText, httpObject, socketText, socketObject, RMIObject;
    
    
    
        private boolean DEBUG = false;
    
    
    
        public void start() {
            refresh();
        }
    
    
        private void refresh()
    
        {
            //httpText.setText(getDateUsingHttpText());
        }
    
    
        public void init() {
    
    
            setLayout(new BorderLayout());
            Panel west = new Panel();
            west.setLayout(new GridLayout(1, 1));
            //        httpText=new TextField();
            String h=getDateUsingHttpText();
            String[] columnNames = {"Node","Region","NodeType","Alarm","Date"};
            String[][] data=new String[5][5];
            for(int i=0;i<5;i++)
                {
                    for(int j=0;j<5;j++)
                        {
                            data[i][j]=h;
                        }
    
                    //data[0][0].charAt(0);
    
                }
    
            final JTable table = new JTable(data, columnNames);
            table.setPreferredScrollableViewportSize(new Dimension(500, 70));
            table.setFillsViewportHeight(true);
    
            if (DEBUG)
                {
                    table.addMouseListener(new MouseAdapter()
                        {
                            public void mouseClicked(MouseEvent e)
                            {
                                printDebugData(table);
                            }
                        });
                }
    
            JScrollPane scrollPane = new JScrollPane(table);
            add(scrollPane);
            west.add(table);
            add("West", west);
    
    
        }
        private void printDebugData(JTable table)
    
        {
            int numRows = table.getRowCount();
            int numCols = table.getColumnCount();
            javax.swing.table.TableModel model = table.getModel();
            System.out.println("Value of data: ");
            for (int i=0; i < numRows; i++)
                {
                    System.out.print("    row " + i + ":");
                    for (int j=0; j < numCols; j++)
                        {
                            System.out.print("  " + model.getValueAt(i, j));
                        }
                    System.out.println();
                }
            System.out.println("--------------------------");
        }
    
    
        public String getDateUsingHttpText() {
    
            try {
                // Construct a URL referring to the servlet
                URL url = new URL(getCodeBase(), "./DaytimeServlet");
    
                // Create a com.oreilly.servlet.HttpMessage to communicate with that URL
                HttpMessage msg = new HttpMessage(url);
    
                // Send a GET message to the servlet, with no query string
                // Get the response as an InputStream
                InputStream in = msg.sendGetMessage();
    
                // Wrap the InputStream with a DataInputStream
          DataInputStream result =
              new DataInputStream(new BufferedInputStream(in));
    
          // Read the first line of the response, which should be
          // a string representation of the current time
          String date = result.readLine();
    
          // Close the InputStream
          in.close();
    
          // Return the retrieved time
          return date;
            }
            catch (Exception e) {
                // If there was a problem, print to System.out
                // (typically the Java console) and return null
                e.printStackTrace();
                return null;
            }
    
            }
    
        private String getDateUsingHttpObject() {
            // Retrieve the current time using an HTTP object-based connection
            return "unavailable";
        }
    
        private String getDateUsingSocketText() {
            // Retrieve the current time using a non-HTTP text-based socket
            // connection
            return "unavailable";
        }
    
        private String getDateUsingSocketObject() {
            // Retrieve the current time using a non-HTTP object-based socket
            // connection
            return "unavailable";
        }
    
        private String getDateUsingRMIObject() {
            // Retrieve the current time using RMI communication
            return "unavailable";
        }
    
    
    }
    And it is working !!! Thank you for guiding me !!!!!

    Will get to you back in case i find anything hard to solve .

  6. #6
    alex1988 is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default

    how to make this thread resolved ?

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by alex1988 View Post
    And it is working !!! Thank you for guiding me !!!!!

    ok, but can you tell from which server you get the date and time?

  8. #8
    alex1988 is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default

    sorry i left for the day thats why took time to reply.I am fetching it from a web server

Similar Threads

  1. return array
    By rfviki in forum New To Java
    Replies: 2
    Last Post: 11-03-2010, 02:05 PM
  2. return array problem
    By doha786 in forum New To Java
    Replies: 3
    Last Post: 03-30-2010, 06:08 PM
  3. getSession() Function Return Null value
    By dalchndr@gmail.com in forum Advanced Java
    Replies: 0
    Last Post: 11-04-2009, 08:24 AM
  4. how use string array while passing to a function
    By sks9s9 in forum New To Java
    Replies: 2
    Last Post: 02-03-2009, 01:44 PM
  5. Filling 2D Array
    By Nakira in forum New To Java
    Replies: 3
    Last Post: 11-12-2008, 01:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •