![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2006
Posts: 13
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Sexy Programmer
|
Well, it does show you the data you want, but not a the pace you would like it to run.
I changed a couple of things around and managed to get some results. In your ConnectionHandler class's actionPerformed() method, the while loop goes really slow. This try statement takes a while to process. try
{
InetAddress address = InetAddress.getByName(octet1 + "." + octet2 + "." + octet3+ "." + i);
display.setText(display.getText() + "\n" + "Address : " + address.getHostAddress() + "\tName: " + address.getHostName() + "\tReachable : " + address.isReachable(500) + "\n");
i++;
}I recommend you have another thread process this data and have some sort of dialog box show while the data is process and have the frame in the background so the user doesn't interrupt anything or user the thinks the program froze.
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Dec 2006
Posts: 13
Rep Power: 0
![]() |
hi, thanks for your reply. any ideas on how to display a client with a color say green if connection if successful and red if not? im sorting out the threading at th moment. thanks.
|
|
|
|
|
|
#4 |
|
Sexy Programmer
|
That would not be that difficult. The easiest way to go about this is to use a JEditorPane object.
http://java.sun.com/javase/6/docs/ap...ditorPane.html It supports HTML 3.1. If you know HTML, you can use the HTML tags to style the text you want in your display. It doesn't support CSS so using the old HTML way to color thing would be appropriate in this case. You are just coloring text, not trying to make a valid HTML document. Good luck!
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Table issues... | Glastea | HTML / XHTML / CSS | 2 | Jan 12th, 2007 1:54 AM |
| I have an idea, but copyright issues. | Booooze | Project Ideas | 25 | Aug 3rd, 2006 9:31 PM |
| Issues with ComboBoxes and DataSources | Arla | C# | 2 | Jul 18th, 2005 1:59 PM |
| C# - LinkedList Issues | technocraze | C# | 1 | Jul 14th, 2005 1:46 AM |