例 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");
}
}
上記のコードは、ユーザーが最初に入力したときのもので、2 回目はサーバーの IP アドレスを再入力する必要はありません。
ここで私の問題:
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());
}
上記のコードは、NIC のリストを取得してコンボボックスで選択するものですが、以前に選択したものを自動的に選択するようにしたいと考えています。デバイスのデータ型が PcapIf であるため、例 1は使用できません。