Skip to content

02_graphical_user_interface__gui__frames_.md

Arnab Nandy edited this page Oct 25, 2025 · 1 revision

Chapter 2: Graphical User Interface (GUI) Frames

In the last chapter, we learned that the Main Application Orchestrator acts like a control room. It's the first thing you see, it checks your security, and then it directs you to the right tool for the job. But what are those tools?

Imagine you've been sent to a specific workstation in that control room. This workstation has buttons, screens, and places for you to type information. This is where the real work of setting up network services happens, but in a way that's easy to understand, even if you're not an expert in complex console commands.

In shellnetbuilder, these individual workstations are called Graphical User Interface (GUI) Frames. They are the interactive windows and forms you see and use to configure your network.

What Problem Do GUI Frames Solve?

The biggest problem GUI Frames solve is making complex network setup tasks easy and accessible. Without them, you'd have to remember and type many tricky commands into a black-and-white terminal, which is slow, error-prone, and not very friendly.

GUI Frames are like filling out a smart digital form instead of writing a detailed letter by hand. They guide you, show you choices, and make sure you provide all the necessary information.

Let's revisit a use case and see how a GUI Frame helps:

Use Case: Setting up an NFS Client

  1. You want to set up your computer (client) to access shared files from another computer (NFS server).
  2. From the shellnetbuilder's main menu, you select "NFS Client Setup".
  3. A new window, the NFS Client Setup Frame, appears.
  4. This window asks you for specific information, like the server's IP address and the path to the shared folder.
  5. You type in this information into easy-to-use boxes.
  6. You click a "Save" or "Apply" button.
  7. The GUI Frame takes your input and tells the backend scripts what to do, setting up the NFS client for you!

What is a GUI Frame?

A GUI Frame is essentially a window on your screen. In Java (the programming language shellnetbuilder uses), these are often JFrame objects. Think of it as a blank canvas where we can place various interactive elements:

  • Labels (like jLabel1): Text that tells you what something is (e.g., "Server IP *").
  • Text Fields (like jTextField1): Boxes where you can type in information (e.g., an IP address).
  • Buttons (like jButton1): Clickable areas that perform an action (e.g., "Save", "Close").
  • Combo Boxes (like jComboBox1): Drop-down menus to select from predefined options.
  • Text Areas (like jTextArea1): Larger boxes to display messages or status updates.

Each GUI Frame in shellnetbuilder is designed for a specific task. For instance, the dhcpframe is for DHCP setup, and nfsclient is for NFS client configuration.

How GUI Frames Work: A Step-by-Step Scenario

Let's trace what happens when you decide to set up an NFS Client:

sequenceDiagram
    participant User as User
    participant MainOrchestrator as Main Orchestrator (mainwindow.java)
    participant NFSClientFrame as NFS Client Setup Frame (nfsclient.java)
    participant BackendScript as Configuration Script (nfs-client.sh)

    User->>MainOrchestrator: Clicks "NFS Client Setup"
    MainOrchestrator->>NFSClientFrame: Launches new `nfsclient` window
    NFSClientFrame->>User: Displays NFS Client setup form
    User->>NFSClientFrame: Enters Server IP, Shared Path, Mount Path
    User->>NFSClientFrame: Clicks "Save" button
    NFSClientFrame->>BackendScript: Sends configuration data (via temporary file)
    BackendScript-->>NFSClientFrame: Returns status/confirmation
    NFSClientFrame->>User: Displays "Configuration Complete!" message
Loading

Explanation of the Flow:

  1. User Initiates: You start by clicking a button in the main application window.
  2. Frame Appears: The Main Orchestrator then launches the specific GUI frame for "NFS Client Setup" (our nfsclient.java in this example). This new window appears on your screen.
  3. User Interacts: You, the user, interact directly with this new frame. You type in the server's IP address, the path on the server you want to access, and where on your local machine you want to see it (mount path).
  4. Action Triggered: When you're done, you click a button, like "Save".
  5. Data to Backend: The nfsclient frame takes all the information you typed in and prepares it for a special script (nfs-client.sh). It writes this information into a temporary file. (We'll learn more about this "Secure Script Execution" in Chapter 3: Secure Script Execution Layer and "File-based Communication" in Chapter 6: File-based Inter-process Communication (IPC)).
  6. Status Update: The script runs and sends back a message (again, usually via a temporary file), which the nfsclient frame then displays in a "Status Window" for you to see.

Code Spotlight: nfsclient.java

The nfsclient.java file is a prime example of a GUI Frame. It's built using Java Swing, a toolkit for creating graphical user interfaces.

1. The Frame's Entry Point

Just like mainwindow.java, each frame has a main method that allows it to be run as a standalone window (though typically launched by mainwindow):

// ... (imports and other code) ...
public class nfsclient extends javax.swing.JFrame {

    public nfsclient() {
        initComponents(); // This calls the method to build all the visual parts!
    }

    public static void main(String args[]) {
        // This sets up the look and feel (e.g., "Nimbus")
        // ... (look and feel setup - similar to mainwindow.java) ...

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new nfsclient().setVisible(true); // Creates and shows the NFS client window
            }
        });
    }
    // ... (variables declaration) ...
}

What this code does: The main method is the starting point. new nfsclient().setVisible(true) creates our NFS client window and makes it visible. The initComponents() method (called in the constructor) is where all the buttons, text fields, and labels are actually created and placed on the window.

2. Building the Visuals (initComponents)

The initComponents() method is automatically generated by Java GUI builders (like NetBeans) and is responsible for creating and arranging all the elements you see in the window.

Let's look at a very simplified example of how some of these parts are defined:

// ... (inside initComponents method) ...
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();     // Our main title for the window
        jLabel2 = new javax.swing.JLabel();     // Label for "Server IP *"
        jTextField1 = new javax.swing.JTextField(); // Box for user to type Server IP
        jButton1 = new javax.swing.JButton();   // The "Save" button
        jTextArea1 = new javax.swing.JTextArea(); // Area to show status messages

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Network Builder V0.1| NFS Client"); // Title bar text

        jLabel1.setFont(new java.awt.Font("Century Schoolbook L", 1, 24));
        jLabel1.setForeground(new java.awt.Color(50, 134, 220));
        jLabel1.setText("NFS Client Setup"); // Setting the text for the main title

        jLabel2.setFont(new java.awt.Font("Century Schoolbook L", 1, 18));
        jLabel2.setText("Server IP *"); // Setting text for an input label

        jTextField1.setFont(new java.awt.Font("Century Schoolbook L", 1, 18)); // Set font for input box

        jButton1.setBackground(new java.awt.Color(64, 224, 105));
        jButton1.setFont(new java.awt.Font("Century Schoolbook L", 1, 18));
        jButton1.setText("Save"); // Setting text for the Save button

        jTextArea1.setEditable(false); // Make the status window read-only
        jTextArea1.setColumns(20);
        // ... more setup for other components (jTextField2, jTextField3, other JLabels) ...
        // ... extensive layout code (GroupLayout) is omitted for simplicity ...
    }
// ...

What this code does: This is where the visual components are born! Each new javax.swing.JLabel(), new javax.swing.JTextField(), new javax.swing.JButton(), and new javax.swing.JTextArea() creates a part of the window. Properties like setFont, setForeground, and setText define how they look and what text they display. The full initComponents method would also contain complex layout code to arrange these elements, but we've simplified it to focus on the elements themselves.

3. Responding to Actions (actionPerformed)

The "magic" happens when you click a button. Each button has an "Action Listener" that tells the program what to do when clicked. For our "Save" button, it collects your input and triggers the backend operations.

// ... (inside jButton1's addActionListener method) ...
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
            	String s_ip = new String(jTextField1.getText()); // Get text from Server IP field
		        String s_path = new String(jTextField2.getText()); // Get text from Shared Path field
		        String c_path = new String(jTextField3.getText()); // Get text from Mount Path field
		        try{
                	    if(s_ip.equals("")|| s_path.equals("")|| c_path.equals(""))
			            {
                            // If any field is empty, show an error message
				            JLabel errorFields = new JLabel("<HTML><FONT COLOR = Red>All Field is Mandetory.</FONT></HTML>");	
				            JOptionPane.showMessageDialog(null,errorFields); 
				            jTextField1.setText(""); // Clear fields
				            jTextField2.setText("");
				            jTextField3.setText("");
			            }
			            else
			            {
                            // If fields are filled, save data to a log file
				            BufferedWriter fileOut = new BufferedWriter(
                                new FileWriter("/etc/Tools/cnfsk.log"));
				            fileOut.write(jTextField1.getText()); 
				            fileOut.newLine();
				            fileOut.write(jTextField2.getText()); 
				            fileOut.newLine();
				            fileOut.write(jTextField3.getText()); 
				            fileOut.newLine();
				            fileOut.close();
				            
                            // Clear input fields after saving
				            jTextField1.setText("");
				            jTextField2.setText("");
				            jTextField3.setText("");

                            // Call a shell script with user input (More in next chapter!)
				            String unixCommand = "bash nfs-client.sh "+s_ip+" "+s_path+" "+c_path; 
				            runShellScript(unixCommand); 
                            
                            // Read and display status from a message file
				            String str2;
				            BufferedReader br1=new BufferedReader(
                                new FileReader("/etc/Tools/cnfsk.msg"));
				            while ((str2 =br1.readLine())!= null) 
				            {
					            jTextArea1.append(str2 + "\n"); // Display output in the status window
				            }
			            }
		        } catch(Exception e) {
		            // Handle any errors during file operations or script execution
		        }
            }
// ...

What this code does: When you click the "Save" button (jButton1), this actionPerformed method executes.

  1. It first grabs the text you entered into the jTextField1, jTextField2, and jTextField3 boxes using getText().
  2. It checks if any fields are empty and shows an error if they are.
  3. If all looks good, it writes the configuration details to a special log file (/etc/Tools/cnfsk.log). This is a form of File-based Inter-process Communication (IPC).
  4. Then, it clears the input fields.
  5. Crucially, it constructs a unixCommand using your input and calls runShellScript(unixCommand). This is how the GUI tells the Linux system to perform the NFS client setup. We'll explore runShellScript and the scripts themselves in Chapter 3: Secure Script Execution Layer and Chapter 5: Service Lifecycle Management Scripts.
  6. Finally, it reads a message from another file (/etc/Tools/cnfsk.msg) and displays it in the jTextArea1 (our status window) to show you the result of the operation.

You'll notice similar patterns in other frames like wlp.java, nfsserver.java, dhcpframe.java, and natframe.java. They all follow the same principle: get user input, trigger a backend script, and display feedback.

Summary and What's Next

In this chapter, we explored the Graphical User Interface (GUI) Frames of shellnetbuilder. We learned that they are the user-friendly windows, forms, and control panels that allow you to interact with the powerful network configuration tools without needing to type complex commands. Each frame is tailored for a specific task, collects your input, and then communicates with the underlying system to perform the desired action.

We also saw how these frames are built in Java using components like labels, text fields, and buttons, and how they respond to your clicks by gathering information and triggering backend operations.

Now that we understand how user input is gathered and actions are initiated from these friendly windows, the next step is to understand how shellnetbuilder securely executes the powerful backend commands and scripts based on your input.

Chapter 3: Secure Script Execution Layer


References: [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23], [24], [25]