Let’s Find Your IP Address using Java
This project implements an IP Address Finder application in Java, featuring a user-friendly GUI interface for website input and IP address retrieval. Utilizing Java’s networking features, the application fetches and displays the IP address and hostname of specified websites. It is designed for cross-platform compatibility, ensuring functionality on various operating systems with Java installed.
Java IP Address Finder Functionalities
- User-friendly GUI Interface: Provides a graphical user interface using Java Swing for website input.
- IP Address Retrieval: Implements functionality to retrieve and display a given website’s IP address and hostname.
- Support for Error Handling: Displays messages indicating successful or failed retrieval operations.
- Built using Java: Ensures compatibility across different operating systems (Windows, macOS, Linux).
Prerequisites for Java IP Address Finder
- IDE Used: (You can use Eclipse IDE).
- Ensure Java Development Kit (JDK) is installed on the machine.
- Basic knowledge of Java.
- GUI Handling: Understanding of Java Swing classes for building GUI applications.
- Networking: Familiarity with Java’s networking classes like ‘InetAddress’ for handling IP addresses.
Steps to Implement IP Address Finder using Java
Import packages:
First, import the necessary Java packages to display the GUI and handle network operations.
- gui.java
// Importing necessary libraries for the GUI and network functionality
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
- Javax.swing.: This package provides a set of “lightweight” (all-Java language) components that, to the maximum degree possible, work the same on all platforms.
- java.awt.*: This package contains classes for creating user interfaces and for painting graphics and images.
- java.awt.event.*: This package provides interfaces and classes for dealing with different types of events fired by AWT components.
- java.net.*: imports all classes from the java.net package, which provides networking capabilities in Java.
GUI and IP Address Finder Logic:
- Fields
static JButton button1, button2;
static JTextField textField;
static JLabel l1, l2, l3, l4, l5;
- button1 and button2 are JButton objects, representing clickable buttons.
- textField is a JTextField object for text input.
- l1, l2, l3, l4, and l5 are JLabel objects for displaying static and dynamic text.
2. JFrame Setup
JFrame f = new JFrame("IP Address Finder");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(100, 100, 800, 600);
Container c = f.getContentPane();
c.setLayout(null);
- Created a JFrame object f with the title “IP Address Finder”.
- Used setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) to ensure the application exits when the frame is closed.
- Positioned the frame at coordinates (100, 100) with a width of 800 pixels and a height of 600 pixels using setBounds(100, 100, 800, 600).
- Obtained the frame’s container using getContentPane().
- Set absolute positioning for components within the container using setLayout(null).
3. Font Setup
// Define fonts for labels and buttons
Font F1 = new Font("Arial", Font.BOLD, 24);
Font F2 = new Font("Arial", Font.BOLD, 30);
Two font objects, F1 and F2, are defined by different sizes.
4. JLabel for Host Name, Host Name Result, IP Address, IP Address Result, Enter Website
l1 = new JLabel("Host Name: ");
l1.setBounds(20, 20, 250, 40);
l1.setFont(F1);
l4 = new JLabel("");
l4.setBounds(280, 20, 350, 40);
l4.setBackground(Color.YELLOW);
l4.setFont(F1);
l4.setOpaque(true);
l2 = new JLabel("IP Address: ");
l2.setBounds(20, 100, 250, 40);
l2.setFont(F1);
l5 = new JLabel("");
l5.setBounds(280, 100, 350, 40);
l5.setBackground(Color.YELLOW);
l5.setFont(F1);
l5.setOpaque(true); // Make background color visible
l3 = new JLabel("Enter Website: ");
l3.setBounds(20, 180, 250, 40);
l3.setFont(F1);
This application uses JLabels to provide descriptive text and display results to the user. These JLabels provide a straightforward and user-friendly interface, making it easy for users to understand where to input data and where to view the results.
5. JTextField for Website Input
// Create and set properties for the text field where the user will enter a website
textField = new JTextField();
textField.setBounds(280, 180, 300, 40);
textField.setFont(F1);
textField is a JTextField for entering the website, positioned at (280, 180) with a width of 300 and height of 40; the font is set to F1.
6. JButton for Click
// Create and set properties for the Click button
button1 = new JButton("Click");
button1.setBounds(150, 250, 200, 50);
button1.setFont(F2);
button1.setBackground(Color.green);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e1) {
try {
InetAddress
ip=InetAddress.getByName(textField.getText());
l4.setText(ip.getHostName()); // Display host name
l5.setText(ip.getHostAddress()); // Display IP address
} catch (Exception e) {
System.out.println(e); // Print exception to console
}
}
});
Button Setup:
- Created a JButton button1 labeled “Click”.
- Positioned button1 at coordinates (150, 250) with a size of 200x50 pixels.
- Applied F2 font to button1 and set its background color to green.
- Added an ActionListener to button1 to handle button clicks.
Functionality:
- Implemented ActionListener to handle button clicks.
- Retrieves the text entered in textField (presumably where the website URL is input).
- Uses InetAddress.getByName(textField.getText()) to obtain the InetAddress object for the entered website.
- Allows users to easily fetch and display the IP address and host name of the specified website by clicking button1.
7. JButton for Reset
// Create and set properties for the Reset button
button2 = new JButton("Reset");
button2.setBounds(480, 250, 200, 50);
button2.setFont(F2);
button2.setBackground(Color.green);
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
l4.setText(""); // Clear host name label
l5.setText(""); // Clear IP address label
textField.setText(""); // Clear text field
}
});
The button2 JButton is labeled “Reset” and is positioned at coordinates (480, 250) with a size of 200x50 pixels. It uses the F2 font and has a green background color. An ActionListener is added to handle button clicks. When the button is clicked, it clears the text in the l4 and l5 labels, as well as the textField, effectively resetting the interface for new input.
8. Adding Components to Container
c.add(l1);
c.add(l4);
c.add(l2);
c.add(l5);
c.add(l3);
c.add(textField);
c.add(button1);
c.add(button2);
Main Method Logic
public class ip_finder {
public static void main(String[] args) {
gui ipfinder = new gui();
ipfinder.createAndShowGUI();
}
}
The ip_finder class contains the main method, which serves as the entry point of the application. Within the main method, an instance of the gui class is created. This instance, ipfinder, calls the createAndShowGUI method, which sets up and displays the graphical user interface (GUI) for the IP address finder application, handling all the logic for retrieving and displaying IP addresses and host names.
Java IP Address Finder Output
Conclusion
Yay! We have successfully created a GUI for an IP Address Finder application in .
This Java project features text labels, input fields, and buttons for user interaction. The Click button retrieves and displays the IP address and hostname for the entered website, utilizing Java’s networking capabilities. The Reset button efficiently clears all input and output fields, ensuring a clean interface for new entries.
This application provides a practical demonstration of Java GUI programming, enhancing understanding of event handling and user interface design.