1

Example 1:

String serverIP=null;
    try {
        File file = new File("serverIP.txt");
        if(file.exists()){
            serverIP = new Scanner(new File("serverIP.txt")).useDelimiter("\\Z").next();
            rmi = (ServerInterface) Naming.lookup("//"+ serverIP +":" +"2323" + "/server");
        }else{
            PrintWriter out = new PrintWriter("serverIP.txt");
            serverIP = JOptionPane.showInputDialog ("Please enter Server IP Address: "); 
            out.println(serverIP);
            out.close();
            rmi = (ServerInterface) Naming.lookup("//"+ serverIP +":" +"2323" + "/server");
        }
    }

The above code is while the user had entered for the first time, then the second time no need to reenter the server IP address again since it had saved and it will look into the file.

My problem here:

private static PcapIf device;
private static List<PcapIf> alldevs = new ArrayList<PcapIf>();

public static void getNIC(){
    int r = Pcap.findAllDevs(alldevs, errbuf);
    if (r == Pcap.NOT_OK || alldevs.isEmpty()) {
        System.err.printf("Can't read list of devices, error is %s\n",
                errbuf.toString());
    }

    //make the device name readable inside combobox
    String[] deviceStrings = new String[getDevices().size()];
    for (int i = 0; i < deviceStrings.length; i++) {
        String deviceString = getDevices().get(i).toString();
        deviceStrings[i] = deviceString.substring(deviceString
                .indexOf("desc=")
                + "desc=".length(), deviceString.indexOf(">"));
    }

    //choose NIC
    //Starting from here i using the same way as 'EXAMPLE 1' but it cant
    devicesCB = new JComboBox(deviceStrings);
    devicesCB.setEditable(true);
    devicesCB.setSelectedIndex(0);
    JOptionPane.showMessageDialog( null, devicesCB, "Please select a NIC", JOptionPane.QUESTION_MESSAGE);
    device = alldevs.get(devicesCB.getSelectedIndex());
}

The above code is to get the list of NIC and chosing with combobox, now I want to make the it to automatically choose the previous choice that I had made. I can't use the Example 1 since the device datatype is PcapIf.

0 Answers0