Cycle problems while java

0

I need your help. Something strange happens in this part of the code. abc = is a created object that handles the graphical interface and the tcp connection. up there all right the abc object creates the graphical interface and manages the tcp connection. the interface has a connection button that when pressed creates the socket and establishes the connection with the server.

Inside the while this System.out.println (); which does not do anything is put there because if it is not it does not enter the if cycle when the connection is established. someone can explain this to me. Why does it work this way? Any way to improve it or solve it? package gui;

/ **  *  * @author Eliezer  * /

import java.awt.Color; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.BorderFactory; import javax.swing.border.Border; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.Dimension; import java.awt.Font; import java.awt.Dimension; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseListener;

import network.NetworkConector;

public class BindingControlConection extends JFrame {

private int numberDevice=0;
private Boolean conected=false;
private JPanel panelDevice[];
private JTextArea displayOfTrama[];
private JTextField fieldForSocket[];
private JButton btnConnect[], btnDisconnect[];
private JLabel lblIndicatorConection[], lblIpAddress[];
public NetworkConector socketDevice[];


public BindingControlConection(){

    this.setLayout(null);
    createPanelDevice(2);

    this.btnConnect[0].addMouseListener(new MouseAdapter(){
        public void mouseReleased(MouseEvent evt){
            Object  obj = evt.getSource();
            if (btnConnect[0] == obj){
                connectServer(); 
            }
        }
    });

    this.btnDisconnect[0].addMouseListener(new MouseAdapter(){
        public void mouseReleased(MouseEvent evt){
            Object  obj = evt.getSource();
            if (btnDisconnect[0] == obj){
                disconnectServer();
            }
        }
    });


}
private void connectServer(){


    String strSocket[] = fieldForSocket[0].getText().split(":");
    String ip = strSocket[0];
    int port = Integer.parseInt(strSocket[1]);
    socketDevice[0] = new NetworkConector(ip, port);
    socketDevice[0].setConection();
    System.out.println("ip: "+ip+"\nport: "+port);
    this.conected=true;
  /*  displayOfTrama[0].setText(socketDevice[0].getData());  
    System.out.println(socketDevice[0].getData());

*/
}

public static void main (String [] args) {

    BindingControlConection abc =  new BindingControlConection();
    abc.setEnabled(true);
    abc.setMinimumSize(new Dimension(280,320));
    abc.setVisible(true);
    abc.setLocationRelativeTo(null);
    //abc.setLayout(null);
    abc.setExtendedState(MAXIMIZED_BOTH);
    abc.setDefaultCloseOperation(EXIT_ON_CLOSE);

    while (true){

        System.out.println(); // Al quitar esta linea no entra en el condicional if

        if (abc.isConnected()){
            System.out.println("eni is conected)=");
            String data = abc.socketDevice[0].getData();
            System.out.println(data);
            System.out.println("imprimiendo data");
            abc.showData(data);
        }
    }

}

}

    
asked by jose.gb89 27.01.2018 в 17:07
source

1 answer

0

It does not connect because the field

this.conected=true;

It is set to true only when you execute the connectServer method, it is only called when you click on the btnConnect button, as it is set here

this.btnConnect[0].addMouseListener(new MouseAdapter(){...}

When executing the condition, the variable will be false and therefore does not enter. That block must be part of the connectServer or in a separate method invoked from that listener. Recommended to reorganize it better.

    
answered by 28.01.2018 в 04:30