Java IP Address Finder – Track and Trace

Free Java courses with 37 real-time projects - Learn Java

The code has been provided to represent a Java application which enables users to enter the URL and find its corresponding IP address. To create a window with labels, text fields and buttons, the application provides a GUI (Graphical User Interface) that uses Swing’s library. The application retrieves the IP address of the URL entered and displays it in a message dialog when the user clicks on the button.

About Java IP Address Finder

The ‘InetAddress’ class is used by the IP Address Finder application to retrieve the IP address linked to a web address that the user has entered. The programme runs a DNS lookup to translate the supplied web address into its matching IP address using the ‘InetAddress’ class and the ‘getByName()’ function. This project is helpful in a variety of situations when knowing the IP address is crucial, such as debugging network connections or programmatically accessing network-related data.

Prerequisites For IP Address Finder Using Java

  • IDE Used: IntelliJ
  • Java 1.8 or above must be installed.

Download Java IP Address Finder Project

Please download the source code of Java IP Address Finder Project: Java IP Address Finder Project Code.

Steps to Create Java IP Address Finder Project:

1. Import Packages

2. IPAddressFinder Class

  • UserInterface() method
  • Constructor to IPAddressFinder class

3. Main() method

4. Test the IP Address Finder

1. Import Packages:

The required Java libraries like AWT, event, networking, and Swing components are imported.

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;

2. IPAddressFinder Class:

This class extends the JFrame class and has instance variables like ‘label’, ‘textField’ and ‘button’ for GUI components.

public class IPAddressFinder extends JFrame {
   private JLabel label;
   private JTextField textField;
   private JButton button;

UserInterface() method: You configure the window title, size, background colour, text font, alignment and layout in this method so that your GUI is set up. The components of the graphical user interface are also created and inserted into the frame.

public void UserInterface(){
   setTitle("DataFlair's IP Address Finder");
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   getContentPane().setBackground(Color.ORANGE);
   setSize(300, 150);
   setLocationRelativeTo(null);
   //Create GUI components
   label = new JLabel("Enter URL:");
   textField = new JTextField(20);
   textField.setForeground(Color.BLUE);
   button = new JButton("Locate IP");
   button.setForeground(Color.WHITE);
   button.setBackground(Color.darkGray);
   button.setBorderPainted(false);
   button.setOpaque(true);
   // Set font and alignment for label and button
   Font labelFont = new Font("Arial", Font.BOLD, 12);
   label.setFont(labelFont);
   label.setHorizontalAlignment(SwingConstants.CENTER);
   Font buttonFont = new Font("Arial", Font.PLAIN, 12);
   button.setFont(buttonFont);
   setLayout(new FlowLayout());
   add(label);
   add(textField);
   add(button);
}

Constructor to IPAddressFinder class: The ‘UserInterface’ method is called by IPAddressFinder’s constructor when creating a graphical user interface. In this method, using an anonymous inner class, an ActionListener is added to the button. The listener’s actionPerformed method will be called as soon as you click the button. The URL that a user has entered is retrieved in the text field within the ActionPerformed method. The InetAddress object representing the IP address for this URL can be obtained by using the InetAddress.getByName() method.

public IPAddressFinder() {
   UserInterface();
   button.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent evt) {
           String url = textField.getText();
           try {
               InetAddress add = InetAddress.getByName(url);
               String ip = add.getHostAddress();
               JOptionPane.showMessageDialog(null, "IP Address: " + ip);
           } catch (Exception e) {
               JOptionPane.showMessageDialog(null, "Error: " + e.getMessage());
           }
       }
   });
}

3. Main() method:

The instance of IPAddressFinder is created and is made visible by invoking setVisible(true) within the SwingUtilities.invokeLater() method. This ensures that the GUI is created and displayed on the Event Dispatch Thread (EDT).

public static void main(String[] args) {
   SwingUtilities.invokeLater(new Runnable() {
       public void run() {
           IPAddressFinder gui = new IPAddressFinder();
           gui.setVisible(true);
       }
   });
}

4. Test the IP Address Finder:

Run the program to find the IP Address for a URL.

a. Enter URL

java ip address finder output

b. IP Address

java ip address finder project output

Conclusion:

The IP Address Finder project shows how to use Java programming in a practical way to get the IP address linked to a website. This code indicates the implementation of an IP Address Finder application using Java Swing. The URL can be entered by the user, and the application will retrieve and display the corresponding IP address. This project offers a practical tool for network-related tasks and lays the groundwork for future research and development in the area of networking and Java-based IP address manipulation.

Your opinion matters
Please write your valuable feedback about DataFlair on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *