I recently wrote some code from a code snippet from my java programming textbook as a way to pass the time, and since it works fine in console, I assumed that it will work fine in a GUI application.
The code:
import java.net.*;
import java.util.*;
import java.io.*;
import java.nio.*;
public class IPAddress {
public void getInterfaces (){
try {
Enumeration e = NetworkInterface.getNetworkInterfaces();
while(e.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) e.nextElement();
System.out.println("Net interface: "+ni.getName());
Enumeration e2 = ni.getInetAddresses();
while (e2.hasMoreElements()){
InetAddress ip = (InetAddress) e2.nextElement();
System.out.println("IP address: "+ ip.toString());
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
IPAddress ip = new IPAddress();
ip.getInterfaces();
}
}
and the results :
--------------------Configuration: <Default>--------------------
Net interface: lo
IP address: /0:0:0:0:0:0:0:1
IP address: /127.0.0.1
Net interface: net0
Net interface: net1
Net interface: net2
Net interface: net3
Net interface: ppp0
Net interface: eth0
Net interface: eth1
Net interface: eth2
Net interface: ppp1
Net interface: net4
IP address: /fe80:0:0:0:283a:9e84:370a:1d01%11
Net interface: eth3
IP address: /fe80:0:0:0:719a:1ec:80cd:c5d1%12
(it shows up to eth14)
when I wrote a GUI version of it, however, I found that it only shows the last interface (eth14) so I'm quite confused since I hardly modified it. Here's the code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
import java.io.*;
import java.nio.*;
public class GuiIPAddress extends JFrame implements ActionListener
{
JButton btnDisplay;
JTextArea textResult;
public static void main(String args[])
{
JFrame f = new GuiIPAddress();
f.setTitle("Network Interface Viewer");
f.setSize(400, 250);
f.setVisible(true);
}
GuiIPAddress()
{
Container contentPane;
contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
textResult = new JTextArea();
textResult.setColumns(30);
textResult.setRows(10);
textResult.setBorder(BorderFactory.createLineBorder(Color.black));
textResult.setEditable(false);
contentPane.add(textResult);
btnDisplay = new JButton("List all Network Interfaces");
contentPane.add(btnDisplay);
btnDisplay.addActionListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event)
{
try {
Enumeration en = NetworkInterface.getNetworkInterfaces();
while(en.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) en.nextElement();
textResult.setText("Net interface: "+ni.getName());
Enumeration en2 = ni.getInetAddresses();
while (en2.hasMoreElements()){
InetAddress ip = (InetAddress) en2.nextElement();
textResult.setText("IP address: "+ ip.toString());
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
The result is: http://img535.imageshack.us/i/javaproblem.jpg/
Maybe you can figure it out, and sorry if the post is a bit too long.