need help with Gui textfields and checking when finished entering

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 533 Views - Last Post: 02 February 2017 - 08:47 AM Rate Topic: -----

#1 Dillonw18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-October 14

need help with Gui textfields and checking when finished entering

Posted 01 February 2017 - 11:25 AM

Hi I am developing a project for college which will simulate a network of Pcs and routers (so like a basic version of Packet tracer).
I have a problem changing the details of object using textfields in my code. I have two codes written shown below. networkingexample and UiPc; The problem I have is that the program runs before I change anyting and any changes made in the UiPc constructor never get reflected in the networkingexample code. I tried using a button to try trigger the update but it wouldnt work. I also used a while loop until the button was pressed but doing this never gave me the chance of clicking the button. Is there something I am missing or need to change? thanks for any help.
import java.awt.*; // this is used to access the GridLayout class
import java.awt.event.*; //this is used to access the MouseAdaptor, MouseEvent, and MouseListener class
import javax.swing.*;
public class networkExample extends JComponent {
  private static final int WIDTH = 400;
  private static final int HEIGHT = 300;
  private int PCHeight=40;
  private int PCWidth=41;
  private int RHeight=50;
  private int RWidth=40;
  int y=20,x=30;
  private JButton pc1;
  private JButton pc2;
  ImageIcon image;
  private DragImage i;
  private PCButtonHandler pcbHandler;
  public networkExample() {
    JFrame frame=new JFrame("NETWORK EXAMPLE");
    ImageIcon image = new ImageIcon("pc.png");
    pc1 =new JButton(image);
  //////////////////////////////////////////////////////////
    pc1.setOpaque(false);
    pc1.setContentAreaFilled(false);
    pc1.setBorderPainted(false);
    pc1.setBounds(y,x,PCHeight,PCWidth);
  ////////////////////////////////////////////////////////
    pcbHandler=new PCButtonHandler();
    pc1.addActionListener(pcbHandler);
  ///////////////////////////////////////////////////////
    frame.setTitle("GUI example");
    Container pane= frame.getContentPane();
    pane.setLayout(null);
    pane.setBackground(Color.WHITE);
    pane.add(pc1);
    frame.setSize(WIDTH, HEIGHT);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  } 
  public class PCButtonHandler implements ActionListener
  {
    String values[];
    boolean pressed=false;
    int value[];
    public void actionPerformed(ActionEvent e)
    {
      int ip[]={-1,-1,-1,-1},dg[]={-1,-1,-1,-1},sm[]={-1,-1,-1,-1};
      PcNic nic=new PcNic(ip);
      PC pc1=new PC("PC1");
      pc1.setPCNic(nic);
      pc1.printPc();
      UiPC i=new UiPC(pc1,nic);
      pc1.printPc();
      //create pc object
    }
  }
  public static void main(String[] args){
    networkExample net=new networkExample();
  }
}


import java.awt.*; // this is used to access the GridLayout class
import java.awt.event.*; //this is used to access the MouseAdaptor, MouseEvent, and MouseListener class
import javax.swing.*;
public class UiPC extends JFrame{
  int ip[]={0,0,0,0},subnet[]={0,0,0,0},gateway[]={0,0,0,0};
  String status,pcName;
  public  boolean ENTERPRESSED = false;
  private JLabel ipL,subnetMaskL,defaultGatewayL;
  private JTextField ipTF,sMTF,dGTF;
  private JButton pingbutton=new JButton("Ping");
  private JButton enterbutton=new JButton("Enter");
  private EnterButtonHandler enterHandler;
  private PingButtonHandler pingHandler;
  private JFrame frame=new JFrame("PC");
  public UiPC(PC a,PcNic B)/>/>{   
    ipL= new JLabel("Enter the ip address: ", SwingConstants.CENTER);
    subnetMaskL=new JLabel("Enter the subnet Mask: ", SwingConstants.CENTER);
    defaultGatewayL=new JLabel("Enter the default Gateway: ", SwingConstants.CENTER);
    pingHandler=new PingButtonHandler();
    enterHandler=new EnterButtonHandler();
    ipTF=new JTextField(10);
    ipTF.setMaximumSize(new Dimension(500,20));
    sMTF=new JTextField(10);
    sMTF.setPreferredSize(new Dimension(300,20));
    sMTF.setMaximumSize(new Dimension(500,20));
    dGTF=new JTextField(10);
    dGTF.setPreferredSize(new Dimension(300,20));
    dGTF.setMaximumSize(new Dimension(500,20));
    Container pane= frame.getContentPane();
    frame.setLayout(new BoxLayout(pane,BoxLayout.PAGE_AXIS));
    pane.add(ipL);
    pane.add(ipTF);
    pane.add(subnetMaskL);
    pane.add(sMTF);
    pane.add(defaultGatewayL);
    pane.add(dGTF);
    enterbutton.addActionListener(enterHandler);
    pane.add(enterbutton);
    pingbutton.addActionListener(pingHandler);
    pane.add(pingbutton);
    frame.setMinimumSize(new Dimension(300,200));
    frame.setPreferredSize(new Dimension(600,200));
    frame.setMaximumSize(new Dimension(660,200));
    frame.setVisible(true);
    a.setName("PC 1");
 /* for( int i=0;i<500000000;i++);
  if(ENTERPRESSED==true){
    System.out.println("button pressesd");
  }
  else{
    System.out.println("button not pressesd");
  }*/
    b.setIp(ip);
    b.setName("PC1 NIC");
    b.setSubnetMask(subnet);
    b.getMacAddress();
    b.setDefaultGateway(gateway);
    b.setStatus("0n");
    a.setPCNic(B)/>/>;
  }
  public class PingButtonHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        
    }
  }
  public class EnterButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent o){
      String values="1.1.1.1",IPV[]={"1","1","1","1"},DGV[]={"1","1","1","1"},SMV[]={"1","1","1","1"};
      int value[]={0,0,0,0},smvalue[]={0,0,0,0},dgvalue[]={0,0,0,0};
      values=ipTF.getText();
      IPV=values.split("\\.");
      if(IPV.length==4){
        for (int i=0;i<IPV.length;i++){
          value[i]=Integer.parseInt(IPV[i]);
        }
      }
      ip=value;
      //-----------------------------------------------------------------------------
      values="1.1.1.1";
      values=sMTF.getText();
      SMV=values.split("\\.");
      if(SMV.length==4){
        for (int i=0;i<SMV.length;i++){
          smvalue[i]=Integer.parseInt(SMV[i]);
        }
      }
      subnet=smvalue;
      //--------------------------------------------------------------------
      values="1.1.1.1";
      values=dGTF.getText();
      DGV=values.split("\\.");
      if(DGV.length==4){
        for (int i=0;i<DGV.length;i++){
          dgvalue[i]=Integer.parseInt(DGV[i]);
        }
      }
      gateway=dgvalue;
      ENTERPRESSED=true;
    }
  }
  public static void main(String[] args){
    networkExample net=new networkExample();
  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: need help with Gui textfields and checking when finished entering

#2 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: need help with Gui textfields and checking when finished entering

Posted 01 February 2017 - 12:05 PM

Quote

the program runs before I change anyting

There are missing classes so I can not compile and test the code.

A GUI program needs to be driven by user events like button presses. Normally the program shows a GUI and waits for the user's input before "running".

Note: Single letter variable names makes the code hard to read and understand. Variable names should describe the data that they contain.

Please change all the names of single letter variables to something with meaning.

Can you make a small, simple program that compiles, executes and shows the problem?

This post has been edited by NormR: 01 February 2017 - 12:37 PM

Was This Post Helpful? 0
  • +
  • -

#3 Dillonw18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-October 14

Re: need help with Gui textfields and checking when finished entering

Posted 01 February 2017 - 04:43 PM

HI thanks for the reply, i will include the missing classes, with regards to the the single letter variables i just wanted to try get something to work.
import java.util.Arrays;
public class PC{
  String name;
  int a[]={0,0,0,0};//used to initialize PCNic's defaut gateway
  PcNic nic1=new PcNic(a);
  public Packet message;
  public ArpTable arptable1=new ArpTable();
  public Router gateway=null;
  public PC(String Name){
    this.name=Name;
  }
  //setters--------------------------------------------------------------------------------------------------------------
  public void setName(String n){
   this.name=n;
  }
  public void setPCNic(PcNic x){
    nic1=x;
  }
  //getters-------------------------------------------------------------------------------------------------------------
  public String getName(){
    return name;
  }
  public int ping(int []destIp,int []sourceIp,int []destMask,int []sourceMask,String sourceMac){
    int result=0,good=0;
    //0=fail 1=passed
    int checkNetwork1[]={0,0,0,0},checkNetwork2[]={0,0,0,0};
    //check if destIp is on sourceIp network
    for(int i=0;i<destIp.length;i++){
      checkNetwork1[i]=destIp[i]&destMask[i];//check destination ip network 
      checkNetwork2[i]=sourceIp[i]&sourceMask[i];//check source ip network
    }
    String s1=Arrays.toString(checkNetwork1);
    s1=s1.replaceAll("\\[|\\]| |\\s", "");
    s1=s1.replaceAll(",",".");
    String s2=Arrays.toString(checkNetwork2);
    s2=s2.replaceAll("\\[|\\]| |\\s", "");
    s2=s2.replaceAll(",",".");
    int check=(s1.compareTo(s2));//compare networks
    if(check==0){// if equal then continue ping process
      System.out.println("Destination is on the same Network");
      good=1;
      result=1;
    }
    else{
      System.out.println("Destination is not on the same Network");
      good=0;
      result=0;
      }       
       return result;
      }
  // send messages-----------------------------------------------------------------------------------------------------
  public String sendArprequestFrame(String sourceM,String sourceIp,String destIp){
    String PCMac;
    message=new ArpPacket(2,1,sourceM,"00.00.00.00.00.00",6,4);
    message.setprotocolNum("0x806");
    message.setSourceIp(sourceIp);
    message.setDestIp(destIp);
    PCMac= nic1.setArpReqFrame("FF.FF.FF.FF.FF.FF",sourceM,message);
    return PCMac;
  }
  public String printArpRqFrame(){
    return String.format(nic1.printArpReqFrame());
  }
  public String sendArpreplyFrame(String sourceM,String destM,String sourceIp,String destIp){
    String PCMac;
    message=new ArpPacket(2,2,sourceM,destM,6,4);
    message.setprotocolNum("0x806");
    message.setSourceIp(sourceIp);
    message.setDestIp(destIp);
    PCMac=nic1.setArpRepFrame(destM,sourceM,message);
    return PCMac;
  } 
  public String printArpReply(){
    return String.format(nic1.printArpRepFrame());
  }
  public void sendIprequestFrame(String sourceM,String destM,String sourceIp,String destIp){
    message=new IpPacket();
    message.setprotocolNum("0x800");
    message.setSourceIp(sourceIp);
    message.setDestIp(destIp);
    nic1.setIpReqFrame(destM,sourceM,message);
  }
  public String printIpRq(){
    return String.format(nic1.printIpreqFrame());
  }     
  public void sendIpreplyFrame(String sourceM,String destM,String sourceIp,String destIp){
    message=new IpPacket();
    message.setprotocolNum("0x800");
    message.setSourceIp(sourceIp);
    message.setDestIp(destIp);
    nic1.setIpRepFrame(destM,sourceM,message);
  }
  public String printIpReply(){
    return String.format(nic1.printIprepFrame());
  }
  //methods for other objects---------------------------------------------------------------------------------------------------------
  public void setAssociatedRouter(Router x){
    gateway=x;
  }
  public Router getAssociatedRouter(){
    return gateway;
  }
  public int searchArpTable(String destIpx){   
    int checkArp=0;
    checkArp=arptable1.searchTable(destIpx);
    return checkArp;
     }
  public void setArpTableEntry(String ipx,String Macx){
    arptable1.storeEntry(ipx,Macx);
  }
  public void printArpTable(){
    arptable1.printArpTable();
  }
//---------------------------------------------------------------------------------------------------------------------
  public void printPc(){
    System.out.println(name+"'s details are");
    nic1.printPcNic();
   /* if(gateway!=null)
      System.out.println("associated router's details:");
      gateway.printRouter();*/
  }
  public static void main( String[] args ){
    int ip[]={2222,2222,-1,-1},subnet[]={-1,-1,-1,-1},ip2[]={2222,2222,-1,-1};
    String mac;
    PcNic nic=new PcNic(ip);
    PC pc1=new PC("pc1");
    // 
    nic.setSubnetMask(subnet);
    mac=nic.getMacAddress();
    nic.setStatus("ON");
    ip=nic.getIp();
    pc1.setPCNic(nic);
    pc1.printPc();
    nic.setIp(ip2);
    pc1.printPc();
  }
}


import javax.swing.JOptionPane;
public class PcNic extends Nic{
 static int a[]={0,0,0,0};
  //attributes
  private  int defaultGateway[]={0,0,0,0};
  //constructor
  public PcNic(int dg[]){
    super("wrong","wrong",a,a,"off");
    this.defaultGateway=dg;
  }
  //setters-------------------------
  public void setDefaultGateway(int DG[]){
    this.defaultGateway=DG;   
     for(int i=0;i<subnetMask.length;i++){
      if (subnetMask.length != 4 || subnetMask[i] < 0 || subnetMask[i]>255)
    this.defaultGateway=checkDefaultGateway(defaultGateway);
     }
  }
  //getters-------------------------
  public int[] getDefaultGateway(){
    return defaultGateway;
  }
  //checkers------------------------
  private int[] checkDefaultGateway(int testip[]){
   String AddressStr="wrong",values[]={"0","0","0","0"};
   do{
     AddressStr=JOptionPane.showInputDialog("enter default gateway, format: 0-255.0-255.0-255.0-255");
     values=AddressStr.split("\\.");
     if(values.length==4){
       for (int i=0;i<values.length;i++){
         testip[i]=Integer.parseInt(values[i]); 
       }
       for (int a=0;a<values.length;a++){
         while(testip[a]<0||testip[a]>255){
           JOptionPane.showMessageDialog(null,"each entry must be between 0-255","Error",JOptionPane.ERROR_MESSAGE);//
           AddressStr=JOptionPane.showInputDialog("enter default gateway, format: 0-255.0-255.0-255.0-255");
           values=AddressStr.split("\\.");
           if(values.length==4){
             for (int i=0;i<values.length;i++){
               testip[i]=Integer.parseInt(values[i]);
             }
           }
         }
       }
     }
     else//output error message if entered ip is not in the correct format
       JOptionPane.showMessageDialog(null,"ip address must have correct format.","Error",JOptionPane.ERROR_MESSAGE);
   }while(values.length!=4);// keep asking until entered correctly   
   return testip;
 }
  //print methods ------------------
  public void printPcNic(){
   super.printNic();    
   System.out.print("\nDefault Gateway:");
   for(int i=0;i<defaultGateway.length;i++){
     if(i<defaultGateway.length-1)
       System.out.print(defaultGateway[i]+".");
     else
       System.out.print(defaultGateway[i]);  
   }
   System.out.print("\n");
  }
   public static void main( String[] args ){
     int ip[]={192,0,0,0},subnet[]={0,0,0,0},g[]={0,0,0,0};
    String mac;
      PcNic nic=new PcNic(ip);
      nic.setName("PC1 NIC");
      nic.setIp(ip);
      nic.setSubnetMask(subnet);
      mac=nic.getMacAddress();
      nic.setDefaultGateway(g);
      nic.setStatus("0n");
      nic.printPcNic();
  }
}


if you run the networkingexample class thats the one i was trying to make work, you may also need the pc image which i have attached.

View PostDillonw18, on 01 February 2017 - 04:41 PM, said:

View PostNormR, on 01 February 2017 - 12:05 PM, said:

Quote

the program runs before I change anyting

There are missing classes so I can not compile and test the code.

A GUI program needs to be driven by user events like button presses. Normally the program shows a GUI and waits for the user's input before "running".

Note: Single letter variable names makes the code hard to read and understand. Variable names should describe the data that they contain.

Please change all the names of single letter variables to something with meaning.

Can you make a small, simple program that compiles, executes and shows the problem?

HI thanks for the reply, i will include the missing classes, with regards to the the single letter variables i just wanted to try get something to work.
import java.util.Arrays;
public class PC{
  String name;
  int a[]={0,0,0,0};//used to initialize PCNic's defaut gateway
  PcNic nic1=new PcNic(a);
  public Packet message;
  public ArpTable arptable1=new ArpTable();
  public Router gateway=null;
  public PC(String Name){
    this.name=Name;
  }
  //setters--------------------------------------------------------------------------------------------------------------
  public void setName(String n){
   this.name=n;
  }
  public void setPCNic(PcNic x){
    nic1=x;
  }
  //getters-------------------------------------------------------------------------------------------------------------
  public String getName(){
    return name;
  }
  public int ping(int []destIp,int []sourceIp,int []destMask,int []sourceMask,String sourceMac){
    int result=0,good=0;
    //0=fail 1=passed
    int checkNetwork1[]={0,0,0,0},checkNetwork2[]={0,0,0,0};
    //check if destIp is on sourceIp network
    for(int i=0;i<destIp.length;i++){
      checkNetwork1[i]=destIp[i]&destMask[i];//check destination ip network 
      checkNetwork2[i]=sourceIp[i]&sourceMask[i];//check source ip network
    }
    String s1=Arrays.toString(checkNetwork1);
    s1=s1.replaceAll("\\[|\\]| |\\s", "");
    s1=s1.replaceAll(",",".");
    String s2=Arrays.toString(checkNetwork2);
    s2=s2.replaceAll("\\[|\\]| |\\s", "");
    s2=s2.replaceAll(",",".");
    int check=(s1.compareTo(s2));//compare networks
    if(check==0){// if equal then continue ping process
      System.out.println("Destination is on the same Network");
      good=1;
      result=1;
    }
    else{
      System.out.println("Destination is not on the same Network");
      good=0;
      result=0;
      }       
       return result;
      }
  // send messages-----------------------------------------------------------------------------------------------------
  public String sendArprequestFrame(String sourceM,String sourceIp,String destIp){
    String PCMac;
    message=new ArpPacket(2,1,sourceM,"00.00.00.00.00.00",6,4);
    message.setprotocolNum("0x806");
    message.setSourceIp(sourceIp);
    message.setDestIp(destIp);
    PCMac= nic1.setArpReqFrame("FF.FF.FF.FF.FF.FF",sourceM,message);
    return PCMac;
  }
  public String printArpRqFrame(){
    return String.format(nic1.printArpReqFrame());
  }
  public String sendArpreplyFrame(String sourceM,String destM,String sourceIp,String destIp){
    String PCMac;
    message=new ArpPacket(2,2,sourceM,destM,6,4);
    message.setprotocolNum("0x806");
    message.setSourceIp(sourceIp);
    message.setDestIp(destIp);
    PCMac=nic1.setArpRepFrame(destM,sourceM,message);
    return PCMac;
  } 
  public String printArpReply(){
    return String.format(nic1.printArpRepFrame());
  }
  public void sendIprequestFrame(String sourceM,String destM,String sourceIp,String destIp){
    message=new IpPacket();
    message.setprotocolNum("0x800");
    message.setSourceIp(sourceIp);
    message.setDestIp(destIp);
    nic1.setIpReqFrame(destM,sourceM,message);
  }
  public String printIpRq(){
    return String.format(nic1.printIpreqFrame());
  }     
  public void sendIpreplyFrame(String sourceM,String destM,String sourceIp,String destIp){
    message=new IpPacket();
    message.setprotocolNum("0x800");
    message.setSourceIp(sourceIp);
    message.setDestIp(destIp);
    nic1.setIpRepFrame(destM,sourceM,message);
  }
  public String printIpReply(){
    return String.format(nic1.printIprepFrame());
  }
  //methods for other objects---------------------------------------------------------------------------------------------------------
  public void setAssociatedRouter(Router x){
    gateway=x;
  }
  public Router getAssociatedRouter(){
    return gateway;
  }
  public int searchArpTable(String destIpx){   
    int checkArp=0;
    checkArp=arptable1.searchTable(destIpx);
    return checkArp;
     }
  public void setArpTableEntry(String ipx,String Macx){
    arptable1.storeEntry(ipx,Macx);
  }
  public void printArpTable(){
    arptable1.printArpTable();
  }
//---------------------------------------------------------------------------------------------------------------------
  public void printPc(){
    System.out.println(name+"'s details are");
    nic1.printPcNic();
   /* if(gateway!=null)
      System.out.println("associated router's details:");
      gateway.printRouter();*/
  }
  public static void main( String[] args ){
    int ip[]={2222,2222,-1,-1},subnet[]={-1,-1,-1,-1},ip2[]={2222,2222,-1,-1};
    String mac;
    PcNic nic=new PcNic(ip);
    PC pc1=new PC("pc1");
    // 
    nic.setSubnetMask(subnet);
    mac=nic.getMacAddress();
    nic.setStatus("ON");
    ip=nic.getIp();
    pc1.setPCNic(nic);
    pc1.printPc();
    nic.setIp(ip2);
    pc1.printPc();
  }
}


import javax.swing.JOptionPane;
public class PcNic extends Nic{
 static int a[]={0,0,0,0};
  //attributes
  private  int defaultGateway[]={0,0,0,0};
  //constructor
  public PcNic(int dg[]){
    super("wrong","wrong",a,a,"off");
    this.defaultGateway=dg;
  }
  //setters-------------------------
  public void setDefaultGateway(int DG[]){
    this.defaultGateway=DG;   
     for(int i=0;i<subnetMask.length;i++){
      if (subnetMask.length != 4 || subnetMask[i] < 0 || subnetMask[i]>255)
    this.defaultGateway=checkDefaultGateway(defaultGateway);
     }
  }
  //getters-------------------------
  public int[] getDefaultGateway(){
    return defaultGateway;
  }
  //checkers------------------------
  private int[] checkDefaultGateway(int testip[]){
   String AddressStr="wrong",values[]={"0","0","0","0"};
   do{
     AddressStr=JOptionPane.showInputDialog("enter default gateway, format: 0-255.0-255.0-255.0-255");
     values=AddressStr.split("\\.");
     if(values.length==4){
       for (int i=0;i<values.length;i++){
         testip[i]=Integer.parseInt(values[i]); 
       }
       for (int a=0;a<values.length;a++){
         while(testip[a]<0||testip[a]>255){
           JOptionPane.showMessageDialog(null,"each entry must be between 0-255","Error",JOptionPane.ERROR_MESSAGE);//
           AddressStr=JOptionPane.showInputDialog("enter default gateway, format: 0-255.0-255.0-255.0-255");
           values=AddressStr.split("\\.");
           if(values.length==4){
             for (int i=0;i<values.length;i++){
               testip[i]=Integer.parseInt(values[i]);
             }
           }
         }
       }
     }
     else//output error message if entered ip is not in the correct format
       JOptionPane.showMessageDialog(null,"ip address must have correct format.","Error",JOptionPane.ERROR_MESSAGE);
   }while(values.length!=4);// keep asking until entered correctly   
   return testip;
 }
  //print methods ------------------
  public void printPcNic(){
   super.printNic();    
   System.out.print("\nDefault Gateway:");
   for(int i=0;i<defaultGateway.length;i++){
     if(i<defaultGateway.length-1)
       System.out.print(defaultGateway[i]+".");
     else
       System.out.print(defaultGateway[i]);  
   }
   System.out.print("\n");
  }
   public static void main( String[] args ){
     int ip[]={192,0,0,0},subnet[]={0,0,0,0},g[]={0,0,0,0};
    String mac;
      PcNic nic=new PcNic(ip);
      nic.setName("PC1 NIC");
      nic.setIp(ip);
      nic.setSubnetMask(subnet);
      mac=nic.getMacAddress();
      nic.setDefaultGateway(g);
      nic.setStatus("0n");
      nic.printPcNic();
  }
}


if you run the networkingexample class thats the one i was trying to make work, you may also need the pc image which i have attached.

actually this is also a nic class which you also might need.
import javax.swing.JOptionPane;
import java.util.Random;
public class Nic{
 //Association---------------------------------------------
  private Frame message;
//Attributes------------------------------------------------
  String name="wrong",macAddress="wrong",status="OFF";
  int ipAddress[]={0,0,0,0},subnetMask[]={0,0,0,0};
// Constructors -----------------------------------------
  public Nic(String n,String mA,int []ip,int sM[],String status){
    this.name=n;
    this.macAddress=mA;
    this.ipAddress=ip;
    this.subnetMask=sM;
    this.status=status;
  }
//Setters --------------------------------------------------
  public void setName(String n){
    this.name=n;
  }
  public void setIp(int ipx[]){
    this.ipAddress=ipx;
    for(int i=0;i<ipAddress.length;i++){
      if (ipAddress.length != 4 || ipAddress[i] < 0 || ipAddress[i]>255)
        this.ipAddress=checkIpAddress(ipAddress);
    }
  }
  public void setSubnetMask(int maskx[]){
    this.subnetMask=maskx;
     for(int i=0;i<subnetMask.length;i++){
      if (subnetMask.length != 4 || subnetMask[i] < 0 || subnetMask[i]>255)
    this.subnetMask=checkSubnetMask(subnetMask);
     }
  }
 private void generateMacAddress(){
   Random rand =new Random();
   byte[] mac=new byte[6];
   rand.nextBytes(mac);
   mac[0] = (byte)(mac[0] & (byte)254);  //zeroing last 2 bytes to make it unicast and locally adminstrated
   StringBuilder sb = new StringBuilder(mac.length * 2);
   for(byte b: mac){
     if(sb.length() > 0)
       sb.append(":");
     sb.append(String.format("%02x", b ));
   }
   macAddress= sb.toString();
 }
  public void setStatus(String statx){
    this.status=statx;
  }
//Getters----------------------------------------------------
 public String getName(){
   return name;
 }
 public int[] getIp(){
   return ipAddress;
  }
 public int[] getSubnetMask(){
   return subnetMask;
  } 
 public String getMacAddress(){
   generateMacAddress();
   return macAddress;
  }
 public String getStatus(){
   return status;
 }
//checkers----------------------------------------------------
 private int[] checkIpAddress(int testip[]){
   String AddressStr="wrong",values[]={"0","0","0","0"};
   do{
     AddressStr=JOptionPane.showInputDialog("enter ip Address, format: 0-255.0-255.0-255.0-255");
     values=AddressStr.split("\\.");
     if(values.length==4){
       for (int i=0;i<values.length;i++){
         testip[i]=Integer.parseInt(values[i]); 
       }
       for (int a=0;a<values.length;a++){
         while(testip[a]<0||testip[a]>255){
           JOptionPane.showMessageDialog(null,"each entry must be between 0-255","Error",JOptionPane.ERROR_MESSAGE);//
           AddressStr=JOptionPane.showInputDialog("enter ip Address, format: 0-255.0-255.0-255.0-255");
           values=AddressStr.split("\\.");
           if(values.length==4){
             for (int i=0;i<values.length;i++){
               testip[i]=Integer.parseInt(values[i]);
             }
           }
         }
       }
     }
     else//output error message if entered ip is not in the correct format
       JOptionPane.showMessageDialog(null,"ip address must have correct format.","Error",JOptionPane.ERROR_MESSAGE);
   }while(values.length!=4);// keep asking until entered correctly   
   return testip;
 }
 private int[] checkSubnetMask(int testip[]){
   String AddressStr="wrong",values[]={"0","0","0","0"};
   do{
     AddressStr=JOptionPane.showInputDialog("enter Subnet Mask format: 0-255.0-255.0-255.0-255");
     values=AddressStr.split("\\.");
     if(values.length==4){
       for (int i=0;i<values.length;i++){
         testip[i]=Integer.parseInt(values[i]); 
       }
       for (int a=0;a<values.length;a++){
         while(testip[a]<0||testip[a]>255){
           JOptionPane.showMessageDialog(null,"each entry must be between 0-255","Error",JOptionPane.ERROR_MESSAGE);//
           AddressStr=JOptionPane.showInputDialog("enter Subnet Mask format: 0-255.0-255.0-255.0-255");
           values=AddressStr.split("\\.");
           if(values.length==4){
             for (int i=0;i<values.length;i++){
               testip[i]=Integer.parseInt(values[i]);
             }
           }
         }
       }
     }
     else//output error message if entered ip is not in the correct format
       JOptionPane.showMessageDialog(null,"ip address must have correct format.","Error",JOptionPane.ERROR_MESSAGE);
   }while(values.length!=4);// keep asking until entered correctly   
   return testip;
 }

//print methods -------------------------------------------------------------------------------------------------------
  public void printNic(){
    System.out.println(name+"'s"+" details are:");
    System.out.print("Ip Address:");
      for(int i=0;i<ipAddress.length;i++){
     if(i<ipAddress.length-1)
       System.out.print(ipAddress[i]+".");
     else
       System.out.print(ipAddress[i]);    
   }
    System.out.print("\nSubnet Mask:");
   for(int i=0;i<ipAddress.length;i++){
     if(i<ipAddress.length-1)
       System.out.print(subnetMask[i]+".");
     else
       System.out.print(subnetMask[i]);  
   }
   System.out.println("\nMac Address:"+macAddress);
    System.out.println(name+"'s"+" status is:"+status);
  }
   //methods for frame---------------------------------------------------------------------------------------------------
 public String setArpReqFrame(String DA,String macAddress,Packet Data){//fill arp reply frame
   message=new Frame(DA,macAddress,"0x806",Data);
    return macAddress;//return source address.
  }
  public String printArpReqFrame(){
    return String.format(message.toString());
  }
  public String setArpRepFrame(String DA,String macAddress,Packet Data){//fill arp reply frame
   message=new Frame(DA,macAddress,"0x806",Data);
    return macAddress;
  }
  public String printArpRepFrame(){//prints ArpReply frame
    return String.format(message.toString());
  }
  public void setIpReqFrame(String DA,String macAddress,Packet Data){//fill ip request frame
    message=new Frame(DA,macAddress,"0x800",Data);
  }
  public String printIpreqFrame(){
    return String.format(message.toString());
  }  
  public void setIpRepFrame(String DA,String macAddress,Packet Data){//fill arp reply frame
    message=new Frame(DA,macAddress,"0x800",Data);
  }
  public String printIprepFrame(){
    return String.format(message.toString());
  }
  
  public static void main( String[] args ){
    int ip[]={2222,2222,-1,-1},subnet[]={-1,-1,-1,-1};
    String mac;
      Nic nic=new Nic("Nic 1","1",ip,subnet,"off");
      nic.setIp(ip);
     // ip=nic.getIp();
      nic.setSubnetMask(subnet);
      mac=nic.getMacAddress();
      nic.setStatus("ON");
      nic.printNic();
  }
 }

Attached image(s)

  • Attached Image

This post has been edited by andrewsw: 01 February 2017 - 04:55 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6403
  • View blog
  • Posts: 25,880
  • Joined: 12-December 12

Re: need help with Gui textfields and checking when finished entering

Posted 01 February 2017 - 04:54 PM

Note that you don't have to quote the previous post in full, there is a Reply button further down the page, or use the Fast Reply box.
Was This Post Helpful? 0
  • +
  • -

#5 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: need help with Gui textfields and checking when finished entering

Posted 01 February 2017 - 05:21 PM

Still there are missing classes and the program is getting much bigger that it needs to be for testing (already over 570 lines).

Can you make a small, simple program that compiles, executes and shows the problem? There does not need to be any network code for testing that the code runs before it gets user input.

This post has been edited by NormR: 01 February 2017 - 05:24 PM

Was This Post Helpful? 0
  • +
  • -

#6 Dillonw18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-October 14

Re: need help with Gui textfields and checking when finished entering

Posted 01 February 2017 - 05:33 PM

I'll try make one tomorrow, it's already where i live,
Was This Post Helpful? 0
  • +
  • -

#7 Dillonw18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-October 14

Re: need help with Gui textfields and checking when finished entering

Posted 02 February 2017 - 02:09 AM

Ok So I have tried shorten down the codes needed to demonstrate my problem. What i need to happen is when I run the networking example code and click on the PC in the window, it creates a pcNic object which is passed into the constructor of the UiPc code.
This UiPc code has textfields which should change the ip address,subnet mask and default gateway of the PcNic object passed in.But this never happens.
import javax.swing.JOptionPane;
import java.util.Random;
public class Nic{
//Attributes------------------------------------------------
  String name="wrong",macAddress="wrong",status="OFF";
  int ipAddress[]={0,0,0,0},subnetMask[]={0,0,0,0};
// Constructors -----------------------------------------
  public Nic(String n,String mA,int []ip,int sM[],String status){
    this.name=n;
    this.macAddress=mA;
    this.ipAddress=ip;
    this.subnetMask=sM;
    this.status=status;
  }
//Setters --------------------------------------------------
  public void setName(String n){
    this.name=n;
  }
  public void setIp(int ipx[]){
    this.ipAddress=ipx;
  }
  public void setSubnetMask(int maskx[]){
    this.subnetMask=maskx;
  }
 private void generateMacAddress(){
   Random rand =new Random();
   byte[] mac=new byte[6];
   rand.nextBytes(mac);
   mac[0] = (byte)(mac[0] & (byte)254);  //zeroing last 2 bytes to make it unicast and locally adminstrated
   StringBuilder sb = new StringBuilder(mac.length * 2);
   for(byte b: mac){
     if(sb.length() > 0)
       sb.append(":");
     sb.append(String.format("%02x", b ));
   }
   macAddress= sb.toString();
 }
  public void setStatus(String statx){
    this.status=statx;
  }
//Getters----------------------------------------------------
 public String getName(){
   return name;
 }
 public int[] getIp(){
   return ipAddress;
  }
 public int[] getSubnetMask(){
   return subnetMask;
  } 
 public String getMacAddress(){
   generateMacAddress();
   return macAddress;
  }
 public String getStatus(){
   return status;
 }
//print methods -------------------------------------------------------------------------------------------------------
  public void printNic(){
    System.out.println(name+"'s"+" details are:");
    System.out.print("Ip Address:");
      for(int i=0;i<ipAddress.length;i++){
     if(i<ipAddress.length-1)
       System.out.print(ipAddress[i]+".");
     else
       System.out.print(ipAddress[i]);    
   }
    System.out.print("\nSubnet Mask:");
   for(int i=0;i<ipAddress.length;i++){
     if(i<ipAddress.length-1)
       System.out.print(subnetMask[i]+".");
     else
       System.out.print(subnetMask[i]);  
   }
   System.out.println("\nMac Address:"+macAddress);
    System.out.println(name+"'s"+" status is:"+status);
  }
 }


import javax.swing.JOptionPane;
public class PcNic extends Nic{
 static int a[]={0,0,0,0};
  //attributes
  private  int defaultGateway[]={0,0,0,0};
  //constructor
  public PcNic(int dg[]){
    super("wrong","wrong",a,a,"off");
    this.defaultGateway=dg;
  }
  //setters-------------------------
  public void setDefaultGateway(int DG[]){
    this.defaultGateway=DG;   
  }
  //getters-------------------------
  public int[] getDefaultGateway(){
    return defaultGateway;
  }
  //print methods ------------------
  public void printPcNic(){
   super.printNic();    
   System.out.print("\nDefault Gateway:");
   for(int i=0;i<defaultGateway.length;i++){
     if(i<defaultGateway.length-1)
       System.out.print(defaultGateway[i]+".");
     else
       System.out.print(defaultGateway[i]);  
   }
   System.out.print("\n");
  }
}


import java.awt.*; // this is used to access the GridLayout class
import java.awt.event.*; //this is used to access the MouseAdaptor, MouseEvent, and MouseListener class
import javax.swing.*;
public class networkExample extends JComponent {
  private static final int WIDTH = 400;
  private static final int HEIGHT = 300;
  private int PCHeight=40;
  private int PCWidth=41;
  private int RHeight=50;
  private int RWidth=40;
  int y=20,x=30;
  private JButton pc1;
  private JButton pc2;
  ImageIcon image;
  private DragImage i;
  private PCButtonHandler pcbHandler;
  public networkExample() {
    JFrame frame=new JFrame("NETWORK EXAMPLE");
    ImageIcon image = new ImageIcon("pc.png");
    pc1 =new JButton(image);
  //////////////////////////////////////////////////////////
    pc1.setOpaque(false);
    pc1.setContentAreaFilled(false);
    pc1.setBorderPainted(false);
    pc1.setBounds(y,x,PCHeight,PCWidth);
  ////////////////////////////////////////////////////////
    pcbHandler=new PCButtonHandler();
    pc1.addActionListener(pcbHandler);
  ///////////////////////////////////////////////////////
    frame.setTitle("GUI example");
    Container pane= frame.getContentPane();
    pane.setLayout(null);
    pane.setBackground(Color.WHITE);
    pane.add(pc1);
    frame.setSize(WIDTH, HEIGHT);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  } 
  public class PCButtonHandler implements ActionListener
  {
    String values[];
    boolean pressed=false;
    int value[];
    public void actionPerformed(ActionEvent e)
    {
      int ip[]={-1,-1,-1,-1},dg[]={-1,-1,-1,-1},sm[]={-1,-1,-1,-1};
      PcNic nic=new PcNic(ip);
      PC pc1=new PC("PC1");
      pc1.setPCNic(nic);
      pc1.printPc();
      UiPC i=new UiPC(nic);
      pc1.printPc();
      //create pc object
    }
  }
  public static void main(String[] args){
    networkExample net=new networkExample();
  }
}


import java.awt.*; // this is used to access the GridLayout class
import java.awt.event.*; //this is used to access the MouseAdaptor, MouseEvent, and MouseListener class
import javax.swing.*;
public class UiPC extends JFrame{
  int ip[]={0,0,0,0},subnet[]={0,0,0,0},gateway[]={0,0,0,0};
  String status,pcName;
  public  boolean ENTERPRESSED = false;
  private JLabel ipL,subnetMaskL,defaultGatewayL;
  private JTextField ipTF,sMTF,dGTF;
  private JButton pingbutton=new JButton("Ping");
  private JButton enterbutton=new JButton("Enter");
  private EnterButtonHandler enterHandler;
  private PingButtonHandler pingHandler;
  private JFrame frame=new JFrame("PC");
  public UiPC(PcNic B)/>{   
    ipL= new JLabel("Enter the ip address: ", SwingConstants.CENTER);
    subnetMaskL=new JLabel("Enter the subnet Mask: ", SwingConstants.CENTER);
    defaultGatewayL=new JLabel("Enter the default Gateway: ", SwingConstants.CENTER);
    pingHandler=new PingButtonHandler();
    enterHandler=new EnterButtonHandler();
    ipTF=new JTextField(10);
    ipTF.setMaximumSize(new Dimension(500,20));
    sMTF=new JTextField(10);
    sMTF.setPreferredSize(new Dimension(300,20));
    sMTF.setMaximumSize(new Dimension(500,20));
    dGTF=new JTextField(10);
    dGTF.setPreferredSize(new Dimension(300,20));
    dGTF.setMaximumSize(new Dimension(500,20));
    Container pane= frame.getContentPane();
    frame.setLayout(new BoxLayout(pane,BoxLayout.PAGE_AXIS));
    pane.add(ipL);
    pane.add(ipTF);
    pane.add(subnetMaskL);
    pane.add(sMTF);
    pane.add(defaultGatewayL);
    pane.add(dGTF);
    enterbutton.addActionListener(enterHandler);
    pane.add(enterbutton);
    pingbutton.addActionListener(pingHandler);
    pane.add(pingbutton);
    frame.setMinimumSize(new Dimension(300,200));
    frame.setPreferredSize(new Dimension(600,200));
    frame.setMaximumSize(new Dimension(660,200));
    frame.setVisible(true);
 /* for( int i=0;i<500000000;i++);
  if(ENTERPRESSED==true){
    System.out.println("button pressesd");
  }
  else{
    System.out.println("button not pressesd");
  }*/
    b.setIp(ip);
    b.setName("PC1 NIC");
    b.setSubnetMask(subnet);
    b.getMacAddress();
    b.setDefaultGateway(gateway);
    b.setStatus("0n");
  }
  public class PingButtonHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        
    }
  }
  public class EnterButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent o){
      String values="1.1.1.1",IPV[]={"1","1","1","1"},DGV[]={"1","1","1","1"},SMV[]={"1","1","1","1"};
      int value[]={0,0,0,0},smvalue[]={0,0,0,0},dgvalue[]={0,0,0,0};
      values=ipTF.getText();
      IPV=values.split("\\.");
      if(IPV.length==4){
        for (int i=0;i<IPV.length;i++){
          value[i]=Integer.parseInt(IPV[i]);
        }
      }
      ip=value;
      //-----------------------------------------------------------------------------
      values="1.1.1.1";
      values=sMTF.getText();
      SMV=values.split("\\.");
      if(SMV.length==4){
        for (int i=0;i<SMV.length;i++){
          smvalue[i]=Integer.parseInt(SMV[i]);
        }
      }
      subnet=smvalue;
      //--------------------------------------------------------------------
      values="1.1.1.1";
      values=dGTF.getText();
      DGV=values.split("\\.");
      if(DGV.length==4){
        for (int i=0;i<DGV.length;i++){
          dgvalue[i]=Integer.parseInt(DGV[i]);
        }
      }
      gateway=dgvalue;
      ENTERPRESSED=true;
    }
  }
  public static void main(String[] args){
    networkExample net=new networkExample();
  }
}


Was This Post Helpful? 0
  • +
  • -

#8 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: need help with Gui textfields and checking when finished entering

Posted 02 February 2017 - 04:51 AM

There are still missing classes: PC

Quote

when I run the networking example code and click on the PC in the window, it creates a pcNic object which is passed into the constructor of the UiPc code.
This UiPc code has textfields which should change the ip address,subnet mask and default gateway of the PcNic object passed in.But this never happens.

What does happen when the code is compiled and executed?
Where is the code that is supposed to do the changes?

This post has been edited by NormR: 02 February 2017 - 05:05 AM

Was This Post Helpful? 0
  • +
  • -

#9 Dillonw18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-October 14

Re: need help with Gui textfields and checking when finished entering

Posted 02 February 2017 - 05:11 AM

ok sorry I have it shortened down now.
import java.util.Arrays;
public class PC{
  String name;
  int a[]={0,0,0,0};//used to initialize PCNic's defaut gateway
  PcNic nic1=new PcNic(a);
  public PC(String Name){
    this.name=Name;
  }
  //setters--------------------------------------------------------------------------------------------------------------
  public void setName(String n){
   this.name=n;
  }
  public void setPCNic(PcNic x){
    nic1=x;
  }
  //getters-------------------------------------------------------------------------------------------------------------
  public String getName(){
    return name;
  }
  public int ping(int []destIp,int []sourceIp,int []destMask,int []sourceMask,String sourceMac){
    int result=0,good=0;
    //0=fail 1=passed
    int checkNetwork1[]={0,0,0,0},checkNetwork2[]={0,0,0,0};
    //check if destIp is on sourceIp network
    for(int i=0;i<destIp.length;i++){
      checkNetwork1[i]=destIp[i]&destMask[i];//check destination ip network 
      checkNetwork2[i]=sourceIp[i]&sourceMask[i];//check source ip network
    }
    String s1=Arrays.toString(checkNetwork1);
    s1=s1.replaceAll("\\[|\\]| |\\s", "");
    s1=s1.replaceAll(",",".");
    String s2=Arrays.toString(checkNetwork2);
    s2=s2.replaceAll("\\[|\\]| |\\s", "");
    s2=s2.replaceAll(",",".");
    int check=(s1.compareTo(s2));//compare networks
    if(check==0){// if equal then continue ping process
      System.out.println("Destination is on the same Network");
      good=1;
      result=1;
    }
    else{
      System.out.println("Destination is not on the same Network");
      good=0;
      result=0;
      }       
       return result;
      }
  // send messages-----------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
  public void printPc(){
    System.out.println(name+"'s details are");
    nic1.printPcNic();
   /* if(gateway!=null)
      System.out.println("associated router's details:");
      gateway.printRouter();*/
  }
  public static void main( String[] args ){
    int ip[]={2222,2222,-1,-1},subnet[]={-1,-1,-1,-1},ip2[]={2222,2222,-1,-1};
    String mac;
    PcNic nic=new PcNic(ip);
    PC pc1=new PC("pc1");
    // 
    nic.setSubnetMask(subnet);
    mac=nic.getMacAddress();
    nic.setStatus("ON");
    ip=nic.getIp();
    pc1.setPCNic(nic);
    pc1.printPc();
    nic.setIp(ip2);
    pc1.printPc();
  }
}


Was This Post Helpful? 0
  • +
  • -

#10 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: need help with Gui textfields and checking when finished entering

Posted 02 February 2017 - 05:14 AM

You missed these two questions I asked:
What does happen when the code is compiled and executed?
Add some print statements to show where the execution goes.

Where is the code that is supposed to do the changes that are missing?
Was This Post Helpful? 0
  • +
  • -

#11 Dillonw18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-October 14

Re: need help with Gui textfields and checking when finished entering

Posted 02 February 2017 - 05:45 AM

Ok I have comments added where I think things should happen. so when you run the networking example code a pc and pcNic object are passed into the UiPc constructor. these are the objects which should be changed. when you see the textfields and enter for example 192.168.1.1 into each field and hit enter I want the PC and PcNic objects' details to change when the enter button is pressed. but I dont fully know how to get it working correctly.
import java.awt.*; // this is used to access the GridLayout class
import java.awt.event.*; //this is used to access the MouseAdaptor, MouseEvent, and MouseListener class
import javax.swing.*;
public class UiPC extends JFrame{
  int ip[]={0,0,0,0},subnet[]={0,0,0,0},gateway[]={0,0,0,0};
  String status,pcName;
  public  boolean ENTERPRESSED = false;
  private JLabel ipL,subnetMaskL,defaultGatewayL;
  private JTextField ipTF,sMTF,dGTF;
  private JButton pingbutton=new JButton("Ping");
  private JButton enterbutton=new JButton("Enter");
  private EnterButtonHandler enterHandler;
  private PingButtonHandler pingHandler;
  private JFrame frame=new JFrame("PC");
  public UiPC(PC a,PcNic B)/>{   
    ipL= new JLabel("Enter the ip address: ", SwingConstants.CENTER);
    subnetMaskL=new JLabel("Enter the subnet Mask: ", SwingConstants.CENTER);
    defaultGatewayL=new JLabel("Enter the default Gateway: ", SwingConstants.CENTER);
    pingHandler=new PingButtonHandler();
    enterHandler=new EnterButtonHandler();
    ipTF=new JTextField(10);
    ipTF.setMaximumSize(new Dimension(500,20));
    sMTF=new JTextField(10);
    sMTF.setPreferredSize(new Dimension(300,20));
    sMTF.setMaximumSize(new Dimension(500,20));
    dGTF=new JTextField(10);
    dGTF.setPreferredSize(new Dimension(300,20));
    dGTF.setMaximumSize(new Dimension(500,20));
    Container pane= frame.getContentPane();
    frame.setLayout(new BoxLayout(pane,BoxLayout.PAGE_AXIS));
    pane.add(ipL);
    pane.add(ipTF);
    pane.add(subnetMaskL);
    pane.add(sMTF);
    pane.add(defaultGatewayL);
    pane.add(dGTF);
    enterbutton.addActionListener(enterHandler);
    pane.add(enterbutton);
    pingbutton.addActionListener(pingHandler);
    pane.add(pingbutton);
    frame.setMinimumSize(new Dimension(300,200));
    frame.setPreferredSize(new Dimension(600,200));
    frame.setMaximumSize(new Dimension(660,200));
    frame.setVisible(true);
 /*
  * CAN I WAIT HERE UNTIL enter Button pressed.
  * 
  *the enter button handler class should change the details when enter button clicked
  */
    b.setIp(ip);// HOW DO I WAIT FOR PERSON TO CHANGE DETAILS.
    b.setName("PC1 NIC");
    b.setSubnetMask(subnet);
    b.getMacAddress();
    b.setDefaultGateway(gateway);
    b.setStatus("0n");
    a.setPCNic(B)/>;
  }
  public class PingButtonHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        
    }
  }
  public class EnterButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent o){
      String values="1.1.1.1",IPV[]={"1","1","1","1"},DGV[]={"1","1","1","1"},SMV[]={"1","1","1","1"};
      int value[]={0,0,0,0},smvalue[]={0,0,0,0},dgvalue[]={0,0,0,0};
      values=ipTF.getText();
      IPV=values.split("\\.");
      if(IPV.length==4){
        for (int i=0;i<IPV.length;i++){
          value[i]=Integer.parseInt(IPV[i]);
        }
      }
      ip=value;
      //-----------------------------------------------------------------------------
      values="1.1.1.1";
      values=sMTF.getText();
      SMV=values.split("\\.");
      if(SMV.length==4){
        for (int i=0;i<SMV.length;i++){
          smvalue[i]=Integer.parseInt(SMV[i]);
        }
      }
      subnet=smvalue;
      //--------------------------------------------------------------------
      values="1.1.1.1";
      values=dGTF.getText();
      DGV=values.split("\\.");
      if(DGV.length==4){
        for (int i=0;i<DGV.length;i++){
          dgvalue[i]=Integer.parseInt(DGV[i]);
        }
      }
      gateway=dgvalue;
      ENTERPRESSED=true;
    }
  }
  public static void main(String[] args){
    networkExample net=new networkExample();
  }
}


import java.awt.*; // this is used to access the GridLayout class
import java.awt.event.*; //this is used to access the MouseAdaptor, MouseEvent, and MouseListener class
import javax.swing.*;
public class networkExample extends JComponent {
  private static final int WIDTH = 400;
  private static final int HEIGHT = 300;
  private int PCHeight=40;
  private int PCWidth=41;
  private int RHeight=50;
  private int RWidth=40;
  int y=20,x=30;
  private JButton pc1;
  private JButton pc2;
  ImageIcon image;
  private DragImage i;
  private PCButtonHandler pcbHandler;
  public networkExample() {
    JFrame frame=new JFrame("NETWORK EXAMPLE");
    ImageIcon image = new ImageIcon("pc.png");
    pc1 =new JButton(image);
  //////////////////////////////////////////////////////////
    pc1.setOpaque(false);
    pc1.setContentAreaFilled(false);
    pc1.setBorderPainted(false);
    pc1.setBounds(y,x,PCHeight,PCWidth);
  ////////////////////////////////////////////////////////
    pcbHandler=new PCButtonHandler();
    pc1.addActionListener(pcbHandler);
  ///////////////////////////////////////////////////////
    frame.setTitle("GUI example");
    Container pane= frame.getContentPane();
    pane.setLayout(null);
    pane.setBackground(Color.WHITE);
    pane.add(pc1);
    frame.setSize(WIDTH, HEIGHT);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  } 
  public class PCButtonHandler implements ActionListener
  {
    String values[];
    boolean pressed=false;
    int value[];
    public void actionPerformed(ActionEvent e)
    {
      int ip[]={-1,-1,-1,-1},dg[]={-1,-1,-1,-1},sm[]={-1,-1,-1,-1};
      PcNic nic=new PcNic(ip);
      PC pc1=new PC("PC1");
      pc1.setPCNic(nic);
      pc1.printPc();
      UiPC i=new UiPC(pc1,nic);
      // PC SHOULD NOW BE UPDATED WITH CHANGED DETAILS.
      pc1.printPc();
      //create pc object
    }
  }
  public static void main(String[] args){
    networkExample net=new networkExample();
  }
}


The PC and PCNIc class stay the same from before.

Ok I have comments added where I think things should happen. so when you run the networking example code a pc and pcNic object are passed into the UiPc constructor. these are the objects which should be changed. when you see the textfields and enter for example 192.168.1.1 into each field and hit enter I want the PC and PcNic objects' details to change when the enter button is pressed. but I dont fully know how to get it working correctly.
import java.awt.*; // this is used to access the GridLayout class
import java.awt.event.*; //this is used to access the MouseAdaptor, MouseEvent, and MouseListener class
import javax.swing.*;
public class UiPC extends JFrame{
  int ip[]={0,0,0,0},subnet[]={0,0,0,0},gateway[]={0,0,0,0};
  String status,pcName;
  public  boolean ENTERPRESSED = false;
  private JLabel ipL,subnetMaskL,defaultGatewayL;
  private JTextField ipTF,sMTF,dGTF;
  private JButton pingbutton=new JButton("Ping");
  private JButton enterbutton=new JButton("Enter");
  private EnterButtonHandler enterHandler;
  private PingButtonHandler pingHandler;
  private JFrame frame=new JFrame("PC");
  public UiPC(PC a,PcNic B)/>{   
    ipL= new JLabel("Enter the ip address: ", SwingConstants.CENTER);
    subnetMaskL=new JLabel("Enter the subnet Mask: ", SwingConstants.CENTER);
    defaultGatewayL=new JLabel("Enter the default Gateway: ", SwingConstants.CENTER);
    pingHandler=new PingButtonHandler();
    enterHandler=new EnterButtonHandler();
    ipTF=new JTextField(10);
    ipTF.setMaximumSize(new Dimension(500,20));
    sMTF=new JTextField(10);
    sMTF.setPreferredSize(new Dimension(300,20));
    sMTF.setMaximumSize(new Dimension(500,20));
    dGTF=new JTextField(10);
    dGTF.setPreferredSize(new Dimension(300,20));
    dGTF.setMaximumSize(new Dimension(500,20));
    Container pane= frame.getContentPane();
    frame.setLayout(new BoxLayout(pane,BoxLayout.PAGE_AXIS));
    pane.add(ipL);
    pane.add(ipTF);
    pane.add(subnetMaskL);
    pane.add(sMTF);
    pane.add(defaultGatewayL);
    pane.add(dGTF);
    enterbutton.addActionListener(enterHandler);
    pane.add(enterbutton);
    pingbutton.addActionListener(pingHandler);
    pane.add(pingbutton);
    frame.setMinimumSize(new Dimension(300,200));
    frame.setPreferredSize(new Dimension(600,200));
    frame.setMaximumSize(new Dimension(660,200));
    frame.setVisible(true);
 /*
  * CAN I WAIT HERE UNTIL enter Button pressed.
  * 
  *the enter button handler class should change the details when enter button clicked
  */
    b.setIp(ip);// HOW DO I WAIT FOR PERSON TO CHANGE DETAILS.
    b.setName("PC1 NIC");
    b.setSubnetMask(subnet);
    b.getMacAddress();
    b.setDefaultGateway(gateway);
    b.setStatus("0n");
    a.setPCNic(B)/>;
  }
  public class PingButtonHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        
    }
  }
  public class EnterButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent o){
      String values="1.1.1.1",IPV[]={"1","1","1","1"},DGV[]={"1","1","1","1"},SMV[]={"1","1","1","1"};
      int value[]={0,0,0,0},smvalue[]={0,0,0,0},dgvalue[]={0,0,0,0};
      values=ipTF.getText();
      IPV=values.split("\\.");
      if(IPV.length==4){
        for (int i=0;i<IPV.length;i++){
          value[i]=Integer.parseInt(IPV[i]);
        }
      }
      ip=value;
      //-----------------------------------------------------------------------------
      values="1.1.1.1";
      values=sMTF.getText();
      SMV=values.split("\\.");
      if(SMV.length==4){
        for (int i=0;i<SMV.length;i++){
          smvalue[i]=Integer.parseInt(SMV[i]);
        }
      }
      subnet=smvalue;
      //--------------------------------------------------------------------
      values="1.1.1.1";
      values=dGTF.getText();
      DGV=values.split("\\.");
      if(DGV.length==4){
        for (int i=0;i<DGV.length;i++){
          dgvalue[i]=Integer.parseInt(DGV[i]);
        }
      }
      gateway=dgvalue;
      ENTERPRESSED=true;
    }
  }
  public static void main(String[] args){
    networkExample net=new networkExample();
  }
}


import java.awt.*; // this is used to access the GridLayout class
import java.awt.event.*; //this is used to access the MouseAdaptor, MouseEvent, and MouseListener class
import javax.swing.*;
public class networkExample extends JComponent {
  private static final int WIDTH = 400;
  private static final int HEIGHT = 300;
  private int PCHeight=40;
  private int PCWidth=41;
  private int RHeight=50;
  private int RWidth=40;
  int y=20,x=30;
  private JButton pc1;
  private JButton pc2;
  ImageIcon image;
  private DragImage i;
  private PCButtonHandler pcbHandler;
  public networkExample() {
    JFrame frame=new JFrame("NETWORK EXAMPLE");
    ImageIcon image = new ImageIcon("pc.png");
    pc1 =new JButton(image);
  //////////////////////////////////////////////////////////
    pc1.setOpaque(false);
    pc1.setContentAreaFilled(false);
    pc1.setBorderPainted(false);
    pc1.setBounds(y,x,PCHeight,PCWidth);
  ////////////////////////////////////////////////////////
    pcbHandler=new PCButtonHandler();
    pc1.addActionListener(pcbHandler);
  ///////////////////////////////////////////////////////
    frame.setTitle("GUI example");
    Container pane= frame.getContentPane();
    pane.setLayout(null);
    pane.setBackground(Color.WHITE);
    pane.add(pc1);
    frame.setSize(WIDTH, HEIGHT);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  } 
  public class PCButtonHandler implements ActionListener
  {
    String values[];
    boolean pressed=false;
    int value[];
    public void actionPerformed(ActionEvent e)
    {
      int ip[]={-1,-1,-1,-1},dg[]={-1,-1,-1,-1},sm[]={-1,-1,-1,-1};
      PcNic nic=new PcNic(ip);
      PC pc1=new PC("PC1");
      pc1.setPCNic(nic);
      pc1.printPc();
      UiPC i=new UiPC(pc1,nic);
      // PC SHOULD NOW BE UPDATED WITH CHANGED DETAILS.
      pc1.printPc();
      //create pc object
    }
  }
  public static void main(String[] args){
    networkExample net=new networkExample();
  }
}


The PC and PCNIc class stay the same from before.
Was This Post Helpful? 0
  • +
  • -

#12 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: need help with Gui textfields and checking when finished entering

Posted 02 February 2017 - 05:51 AM

Quote

I want the PC and PcNic objects' details to change when the enter button is pressed.

Have you traced the code to see when the details are changed? Does it happen before the enter button is press? It should happen afterwards.

Does the print out of the PC and PcNic objects' details happen before their values are changed?
Was This Post Helpful? 0
  • +
  • -

#13 Dillonw18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-October 14

Re: need help with Gui textfields and checking when finished entering

Posted 02 February 2017 - 06:08 AM

the local variables I used to hold the ip address, gateway and subnet mask change after I press the enter button. but the printing of the objects in the network example happen before I get to change or press anything. I dont know how to make the program wait for the button press. I did try use a while loop like while(ENTERPRESSED==false); before I set the values into the objects. but i never saw any buttons.
Was This Post Helpful? 0
  • +
  • -

#14 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: need help with Gui textfields and checking when finished entering

Posted 02 February 2017 - 06:26 AM

Quote

how to make the program wait for the button press.

GUI programs are driven by events. When a program starts, a GUI is shown and the code returns to the JVM to wait for user input. When the user creates a event like with a button press, a listener method is called to handle that event. The code in the listener is called on Swing's thread should be minimal to prevent the GUI from hanging. Normally the way a listener starts the execution of a long running task is to start a Thread that will execute the code for the task. That allows the listener to return the thread it is using to the JVM to keep the GUI responsive.

This post has been edited by NormR: 02 February 2017 - 06:27 AM

Was This Post Helpful? 0
  • +
  • -

#15 Dillonw18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-October 14

Re: need help with Gui textfields and checking when finished entering

Posted 02 February 2017 - 07:49 AM

Ok, see I have only ever done basic Java before this project so i never heard of threads and dont know yet how to use them, I will look it up and try use them. But I have another question where exactly do I put the thread? Is it in the constructor of the UiPc class before I update the objects or in the enterButtonHanlder class?

Ok, see I have only ever done basic Java before this project so i never heard of threads and dont know yet how to use them, I will look it up and try use them. But I have another question where exactly do I put the thread? Is it in the constructor of the UiPc class before I update the objects or in the enterButtonHanlder class?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2