Thread: GUI issues
View Single Post
Old Jan 15th, 2007, 9:30 PM   #1
fresher
Newbie
 
Join Date: Dec 2006
Posts: 13
Rep Power: 0 fresher is on a distinguished road
GUI issues

Hi all, I have just implemented a GUI for my partially working code but Im having problems getting information to show on the GUI. any help will be appreciated please.

//UtilityPanel.java
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;

public class UtilityPanel extends JPanel
{
	public JPanel buttonJPanel, ipPanel, textPanel, middlePanel, mainPanel;	//Panel to hold buttons and Server IPAddress
	public JButton connect, disconnect, ping, hostInfo;
	public JLabel label2, label1;										//label field for IP address
	public JLabel label3, label4, label5, label6;										//labels for IP range
	public JTextField textOctet1, textOctet2, textOctet3, textOctet4, textOctet5;	//text fields for IP address range
	public JTextField textPing;									//Text field to enter ping address
	public String ipAddress, pingString, output;									//Holds IP Address for label
	public String octet1, octet2, octet3, octet4, octet5, combined;
	public JTextArea display;									//Text area for output commands
	public int i,a;												//Variables for IP Range


	public UtilityPanel()
	{
		//Create the main panel to contain the four sub panels

				 mainPanel = new JPanel();
				 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
				 mainPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
				 add(mainPanel);


		//Get local IPAddress

		try
			{
				InetAddress local = InetAddress.getLocalHost();
				ipAddress = local.getHostAddress();
					}
			  			catch (UnknownHostException e)
		 			{
						System.err.println( "Can't detect localhost " + e);
			}


		 //add ipPanel

		ipPanel = new JPanel();
		ipPanel.setSize(600,30);

		label1 = new JLabel( "Server IP Address:" );
		ipPanel.add( label1 );				//add label1 to panel

		label2 = new JLabel (ipAddress);
		ipPanel.add (label2);				//add label21 to panel

        //add buttonJPanel

		buttonJPanel = new JPanel();
		buttonJPanel.setSize(600,70);
		buttonJPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Control"),
        	BorderFactory.createEmptyBorder(5,5,5,5)));

		connect = new JButton( "Connect" );
		buttonJPanel.add(connect);			//add button to JFrame

		disconnect = new JButton( "Disconnect" );
		buttonJPanel.add(disconnect);			//add button to buttonJPanel

		hostInfo = new JButton(" Host Info" );
		buttonJPanel.add(hostInfo);			//add button to buttonJPanel

		ping = new JButton("Ping" );
			buttonJPanel.add(ping);			//add button to buttonJPanel

		textPing = new JTextField(10);
		buttonJPanel.add(textPing);

		middlePanel = new JPanel();
		middlePanel.setSize(600,300);

		middlePanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("IP Range"),
        	BorderFactory.createEmptyBorder(5,5,5,5)));

		textOctet1 = new JTextField("192",3);
		middlePanel.add(textOctet1);

		label3 = new JLabel(".");
		middlePanel.add(label3);

		textOctet2 = new JTextField("168",3);
		middlePanel.add(textOctet2);

		label4 = new JLabel(".");
		middlePanel.add(label4);

		textOctet3 = new JTextField("0",3);
		middlePanel.add(textOctet3);

		label5 = new JLabel(".");
		middlePanel.add(label5);

		textOctet4 = new JTextField("100",3);
		middlePanel.add(textOctet4);

		label6 = new JLabel("-");
		middlePanel.add(label6);

		textOctet5 = new JTextField("120",3);
		middlePanel.add(textOctet5);

		 //add textPanel

		textPanel = new JPanel();
		textPanel.setSize(600,200);
		textPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Current Connections"),
			BorderFactory.createEmptyBorder(5,5,5,5)));

		display = new JTextArea(18, 50);
		display.setEnabled(true);
		display.setLineWrap(true);
		display.setWrapStyleWord(true);
		JScrollPane scrollPane = new JScrollPane(display,
			JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
			JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	    textPanel.add(scrollPane);			//add display to textPanel


       	mainPanel.add(ipPanel);
        mainPanel.add(buttonJPanel, BorderLayout.NORTH);
		mainPanel.add(middlePanel,BorderLayout.CENTER);
		mainPanel.add(textPanel, BorderLayout.SOUTH);


		PingHandler pingHandler = new PingHandler();
		ping.addActionListener(pingHandler);

		ConnectHandler connectHandler = new ConnectHandler();
		connect.addActionListener(connectHandler);

		DisconnectHandler disconnectHandler = new DisconnectHandler();
		disconnect.addActionListener(disconnectHandler);


	}		//end public UtilityPanel


		public class PingHandler implements ActionListener
		{
			public void actionPerformed(ActionEvent event)
			{

						String ip = (textPing.getText());

						String pingResult = "";

						String pingCmd = "ping " + ip;

						try
						{
							Runtime r = Runtime.getRuntime();
							Process p = r.exec(pingCmd);

							BufferedReader in = new BufferedReader(new
							InputStreamReader(p.getInputStream()));
							String inputLine;

						while ((inputLine = in.readLine()) != null)
						{
							display.append(inputLine + "\n");
							pingResult += inputLine;
						}
						in.close();


						}
						catch (IOException e)
						{
					System.out.println(e);
						}
			}
		}

		public class ConnectHandler implements ActionListener
		{
			public void actionPerformed(ActionEvent event)
			{

				octet1 = (textOctet1.getText());
				octet2 = (textOctet2.getText());
				octet3 = (textOctet3.getText());
				octet4 = (textOctet4.getText());
				octet5 = (textOctet5.getText());

				int i = Integer.valueOf( octet4 ).intValue();
				int a = Integer.valueOf( octet5 ).intValue();

				while(i<=a)
				{

					try
					{
						InetAddress address = InetAddress.getByName(octet1 + "." + octet2 + "." + octet3+ "." + i);
						display.append("Address : " + address.getHostAddress() + "\tName: " + address.getHostName() + "\tReachable : " + address.isReachable(500) + "\n");
						i++;
					}


					catch (UnknownHostException e)
					{
						display.append("Cannot connect to : " + octet1 + "." + octet2 +"."+ octet3+"."  + i);
					}

					catch (IOException e)
					{
					display.append("Unable to reach address");

					}



				}

			}
		}

		public class DisconnectHandler implements ActionListener
		{
			public void actionPerformed(ActionEvent event)
			{
				//this.setDefaultCloseOperation(EXIT_ON_CLOSE);

			}

		}

} 	//end class UtilityPanel
//UtilityFrame.java
// UtilityFrame.java
import javax.swing.*;
import java.awt.*;

class UtilityFrame extends JFrame
	{
		//import Utility panel
		UtilityPanel ut;

		UtilityFrame()
		{
			super("Network Management Utility Tool");
			Container cp = getContentPane();
			cp.setLayout(new BorderLayout());

			ut = new UtilityPanel();


			cp.add(ut, BorderLayout.NORTH);

			this.setDefaultCloseOperation(EXIT_ON_CLOSE);
			this.setSize(620, 550);

			setVisible(true);
			setResizable(true);

		}

		public static void main (String args[])
		{
			UtilityFrame uf = new UtilityFrame();
		}
}

//compile both programs and run UtilityFrame.java to see what i mean

Problem
When UtilityFrame.java is run and the IP range is entered in the appropriate field (current connections label) on the GUI, a list of connections does not show up as I want it to.
Question:
How do cant i get the current connections field to display the hostname, successful connection (green color) or unsuccesful connection (red).?Also is there a way to loop through again say after 5secs (just a sort of refresh). pls i need help. thanks in advance.
fresher is offline   Reply With Quote