1,105,625 Community Members

GUI won't appear unless socket closes but this opens other unwanted GUI

Member Avatar
initialise
Newbie Poster
17 posts since Apr 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi All,

I am writing a client server program that has quite a few GUI's in it. Basically, everything is working except for the problem I'm encountering. Well, obviously :P I am not currently running my server program in order to simulate what would happen to the client if the server connection went down for whatever reason. The exception of not being connected is caught. However, within this exception block I call a GUI, in a class "SocketError", alerting the user to the fact that the program has encountered an error. Then the program exits. However, this GUI does not appear on screen even though the program does register the forced ending of the program in the "SocketError" class.

System.exit(0)

I call the client class to bind the socket in two separate places in my program. In the first instance, at the beginning of the program, if I close the socket in the "SocketError" class then, yet only sometimes, the GUI will flash momentarily before the next window will be called up. I know that once the socket has closed the program returns from the "SocketError" class to where it was called from, bypassing my

Thread.sleep(60000);
System.exit(0)

and causing the main menu window to be called up when I want the program to end. If I call the client class to bind the socket just before the end of my program, where again I make use of

System.exit(0)

, then the GUI doesn't appear at all.

My question is: How do I manage to get my GUI to appear on screen and my sleep and exit carried out with the socket closed? Or, if I do not need to close the socket - seeing as it wasn't accepted - how do I get the GUI to appear?

My Client code:

import java.io.*;
import java.net.*;
public class Client{
  Socket sock;

  Client() {
        BufferedOutputStream bos;
	BufferedInputStream bis;
	int in;
	try{
		//1. creating a socket to connect to the server
		sock = new Socket("myserver.ac.za", 1517);
		File myFile = new File("C:/Extras/Documents.txt");
		bis = new BufferedInputStream(new FileInputStream(myFile));
		bos = new BufferedOutputStream(sock.getOutputStream());
		byte[] byteArray = new byte[(int)myFile.length()];
		while((in = bis.read(byteArray))!= -1)
		{
	          bos.write(byteArray,0,in);
		}
		bos.flush();
		bos.close();
		bis.close();
		sock.close();
		myFile.delete();
		File file = new File("C:/Extras/Documents.txt");
		file.createNewFile();
	}
	catch (Exception e) {
	  System.out.println(e.getMessage());
	  SocketError GUI = new SocketError(sock);
	  GUI.setVisible(true);
	}
    }
}


and my SocketError class:

import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.BorderFactory; 
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import javax.imageio.*;
import java.awt.Graphics.*;
import javax.swing.JComponent.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle;
import java.util.*;

public class SocketError extends JFrame
{
		
 public SocketError(Socket sock)
 {
   Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
   setPreferredSize(new Dimension(dim.width,dim.height));
	
   setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
   setUndecorated(true);

   JPanel mainPanel = new JPanel();
   mainPanel.setPreferredSize(new Dimension(dim.width,dim.height));
   mainPanel.setBackground(new Color(102, 102, 102));
   mainPanel.setBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 5));
   mainPanel.setLayout(new GridBagLayout());
		
   Border bevelBorder = BorderFactory.createBevelBorder(1, Color.GRAY, Color.WHITE);
	
   JButton jButton1 = new JButton();
   JLabel jLabel1 = new JLabel();
   JLabel jLabel2 = new JLabel();
   JLabel jLabel3 = new JLabel();

   jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
   jLabel1.setForeground(new java.awt.Color(255, 255, 255));
   jLabel1.setText("The program has encountered a problem");

   jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24));
   jLabel2.setForeground(new java.awt.Color(255, 255, 255));
   jLabel2.setText("The program will now end");
		
   jLabel3.setFont(new java.awt.Font("Tahoma", 1, 24));
   jLabel3.setForeground(new java.awt.Color(255, 255, 255));
   jLabel3.setText("Please notify the person in charge");
				
   GridBagConstraints ecHeading = new GridBagConstraints();
		
   ecHeading.fill = GridBagConstraints.NONE;
   ecHeading.weightx = 0.5;
   ecHeading.weighty = 0.3;
   ecHeading.gridx = 0;
   ecHeading.gridy = 2;
   mainPanel.add(jLabel1, ecHeading);
   jLabel1.setBorder(bevelBorder);
		
   GridBagConstraints ecHeading2 = new GridBagConstraints();
		
   ecHeading2.fill = GridBagConstraints.NONE;
   ecHeading2.weightx = 0.5;
   ecHeading2.weighty = 0.3;
   ecHeading2.gridx = 0;
   ecHeading2.gridy = 4;
   mainPanel.add(jLabel2, ecHeading2);
   jLabel2.setBorder(bevelBorder);

   GridBagConstraints ecHeading3 = new GridBagConstraints();
		
   ecHeading3.fill = GridBagConstraints.NONE;
   ecHeading3.weightx = 0.5;
   ecHeading3.weighty = 0.3;
   ecHeading3.gridx = 0;
   ecHeading3.gridy = 6;
   mainPanel.add(jLabel3, ecHeading3);
   jLabel3.setBorder(bevelBorder);
		
   add(mainPanel);
   pack();
   try {
      if (sock != null) sock.close();
      Thread.sleep(60000);
      System.exit(0);
    }
   catch (Exception e) {}
 }
}

Thanks in advance :)

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: