3
Contributors
41
Replies
42
Views
4 Years
Discussion Span
Last Post by lena1990
0

this is the code for defining the socket

private void button1MouseClicked(java.awt.event.MouseEvent evt) {                                     
        // TODO add your handling code here:
        try{
             this.textField2.setText(textField1.getText());
      Socket d=new Socket("127.0.0.1",3000);
 this.textField2.setText("hello");

}
catch(Exception e){
}

there is no error message apper but the next print statement does not apper

0

the next print statement does not apper

Where is the print statement you are talking about?

If you define a variable inside of a method (like Socket d) when the method exits, the variable's definition will be gone.

0

the statement that i talk about is

this.textField2.setText("hello");

when i click on the button the print on the text2Field2

0

Are you saying that the contents of textField2 is not set to the String: "Hello"?
Is that statement executed? Add a println just before the call to setText to see if the statement is executed by printing out a message.

How can you see the contents of textField2? Is that variable being shown in a GUI?

0

the print before the defenition of the socket is appered but the print after the defenition of the socket doesnot appered

0

Is there a ServerSocket waiting at that address for a connection?

Can you write a small simple program that compiles, executes and shows your problem?

Edited by NormR1: n/a

0

the print statement before the defenition of the socket appered but the print statement after the defenition of the socket doesnot appered

0

yes there is a serversocket waiting at the address you can try to create an applet with a jbutton and but the following code

try{
            // this.textField2.setText(textField1.getText());
      Socket d=new Socket("127.0.0.1",3000);
 this.textField2.setText("hello");

}
catch(Exception et){
    
}

and you will see what is my problem

0

Is there a ServerSocket waiting at that address for a connection?

Can you write a small simple program that compiles, executes and shows your problem?

You have not posted enough code to show what is causing the problem.

0

Can you write a small simple program that compiles, executes and shows your problem?

You have not posted enough code to show what is causing the problem.

0

first thank you for your help if you try to create an applet and jbutton and by click on the button you will define a socket

private void button1MouseClicked(java.awt.event.MouseEvent evt) {                                     
        // TODO add your handling code here:
        try{
            // this.textField2.setText(textField1.getText());
      Socket d=new Socket("127.0.0.1",3000);
 this.textField2.setText("hello");

}
catch(Exception et){
    
}
    }

and you will see what the problem is

0

If you want me to test your code, please make a small simple program that compiles, executes and shows your problem?

What you posted will not compile or execute.


You should add a call to the printStackTrace method in the catch block so that you will get the full text of any error messages. The code that you posted ignores any exceptions.

Add: et.printStackTrace() inside of the catch block.

Edited by NormR1: n/a

0
package testa;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.DatagramSocket;
import java.net.Socket;

/**
 *
 * @author ali
 */
public class NewJApplet extends javax.swing.JApplet {

    /**
     * Initializes the applet NewJApplet
     */
    @Override
    public void init() {
        System.out.println("you entered");
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the applet
         */
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {

                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * This method is called from within the init() method to initialize the
     * form. WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        textField1 = new java.awt.TextField();
        button1 = new java.awt.Button();
        textField2 = new java.awt.TextField();

        setBackground(new java.awt.Color(255, 153, 255));

        jLabel1.setText("الاسم الرباعي للطالب");
        jLabel1.setName("label1");

        textField1.setName("text1");
        textField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textField1ActionPerformed(evt);
            }
        });
        textField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textField1KeyReleased(evt);
            }
        });

        button1.setLabel("button1");
        button1.setName("button1");
        button1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                button1MouseClicked(evt);
            }
        });
        button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button1ActionPerformed(evt);
            }
        });

        textField2.setName("text2");
        textField2.setText("text2");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(31, 31, 31)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(51, 51, 51)
                        .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(68, 68, 68)
                        .addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20)
                .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(78, Short.MAX_VALUE))
        );
    }// </editor-fold>                        

    private void textField1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
    }                                          

    private void button1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
try{
     /*   Socket s=new Socket("127.0.0.1",3000); 
     DataInputStream   in=new DataInputStream(s.getInputStream());
 DataOutputStream out=new DataOutputStream(s.getOutputStream());
 out.writeUTF("hello");
 this.jLabel1.setText("hello");
*/
}
catch(Exception et){
    
}
    }                                       

    private void button1MouseClicked(java.awt.event.MouseEvent evt) {                                     
        // TODO add your handling code here:
        try{
            // this.textField2.setText(textField1.getText());
      Socket d=new Socket("127.0.0.1",3000);
 this.textField2.setText("hello");

}
catch(Exception et){
    
}
    }                                    

    private void textField1KeyReleased(java.awt.event.KeyEvent evt) {                                       
this.textField2.setText(textField1.getText());        // TODO add your handling code here:
    }                                      

    // Variables declaration - do not modify                     
    private java.awt.Button button1;
    private javax.swing.JLabel jLabel1;
    private java.awt.TextField textField1;
    private java.awt.TextField textField2;
    // End of variables declaration                   
}
0

Do you get any error messages printed when you add the printStackTrace method?

For example you need one at line 171

Edited by NormR1: n/a

0

If you do not get an exception at the Socket statement, then the println statement after the Socket statement should print out a message.
So either the Socket statement throws an exception and the printStackTrace() statement in the catch block prints out a message or the println statement after the Socket statement prints out a message.
One or the other MUST execute and print out a message.

Edited by NormR1: n/a

0

when i run the applet without displaying it on web browser it is working but now it doesnot run when i display it in web browser so how can i make it run

0

One of the browser's menu items or options may do it.
Or go to the OS's Control panel, click on Java and find a setting somewhere there.

Edited by NormR1: n/a

This article has been dead for over six months. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.