Now JInternalFrame closing method is not working...:(
OK, I managed to get the code working for JFrame's windowClosing() method.
Now the JInternalFrame method is not working...
Help?
--
Best Regards,
Wing Hoe
---------------------------------------------------------------
ICQ: 2213281
Email: winghoe@hotmail.com
www: http://pwp.maxis.net.my/winghoe
---------------------------------------------------------------
Re: Now JInternalFrame closing method is not working...:(
Attaching my code for PublicChat here for you guys to inspect.
--
Best Regards,
Wing Hoe
---------------------------------------------------------------
ICQ: 2213281
Email: winghoe@hotmail.com
www: http://pwp.maxis.net.my/winghoe
---------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
class PublicChat extends JInternalFrame implements Runnable
{//begin class PublicChat
private String userName = "winghoe";
private String chatString;
private JTextField txtChatString;
private JTextArea txtChatArea;
private JScrollPane scrollChatArea;
private JButton btnSend;
private JPanel pnlSouth;
private Status status;
private EventsLog eventsLog;
private RWSettings rwSettings;
private String host;
private int port;
private ObjectOutputStream output;
private ObjectInputStream input;
private Thread publicChatThread;
public PublicChat(EventsLog eventsLog) throws IOException
{//begin PublicChat()
super("Public Chat");
this.eventsLog = eventsLog;
status = new Status();
status.setPublicChatLoaded(true);
eventsLog.writeEventsLog("Initializing PublicChat Components...");
initializeComponents();
eventsLog.writeEventsLog("Reading Public Chat's Host and Port...");
readHostPortNo();
eventsLog.writeEventsLog("Initializing eventHandlers...");
listenToButtonClick();
pack();
show();
}//end PublicChat()
public void listenToInternalFrameEvent()
{//begin listenToInternalFrameEvent()
addInternalFrameListener(
new InternalFrameAdapter()
{//begin InternalFrameAdapter()
public void internalFrameClosing(InternalFrameEvent e)
{//begin internalFrameClosing()
cleanUpProcesses();
}//end internalFrameClosing()
}//end InternalFrameAdapter()
);//complete addInternalFrameListener
}//end listenToInternalFrameEvent()
public void readHostPortNo()
{//begin readHostPortNo()
rwSettings = new RWSettings();
rwSettings.readSettings();
this.host = rwSettings.getServerName();
this.port = rwSettings.getPublicChatPortNo();
}//end readHostPortNo()
public void initializeComponents()
{//begin initializeComponents()
Container c = getContentPane();
c.setLayout(new BorderLayout());
txtChatString = new JTextField();
txtChatArea = new JTextArea();
scrollChatArea = new JScrollPane(txtChatArea);
btnSend = new JButton("Send");
pnlSouth = new JPanel();
txtChatArea.setEditable(false);
txtChatArea.setLineWrap(true);
pnlSouth.setLayout(new BorderLayout());
pnlSouth.add(txtChatString, BorderLayout.CENTER);
pnlSouth.add(btnSend, BorderLayout.EAST);
c.add(pnlSouth, BorderLayout.SOUTH);
c.add(scrollChatArea, BorderLayout.CENTER);
txtChatString.requestFocus();
setPreferredSize(new Dimension(400,200));
setClosable(true);
setResizable(true);
setIconifiable(true);
setMaximizable(true);
setVisible(false);
}//end initializeComponents()
public void listenToButtonClick()
{//begin listenToButtonClick()
btnSend.addActionListener(
new ActionListener()
{//begin ActionListener()
public void actionPerformed(ActionEvent e)
{//begin actionPerformed()
{//begin if
sendChatString(userName,txtChatString.getText());
txtChatString.setText("");
}//end if
}//end actionPerformed()
}//end ActionListener()
);//complete btnSend.addActionListener
}//end listenToButtonClick()
public synchronized void start() throws IOException
{//begin start()
if (publicChatThread == null)
{//begin if
Socket publicChatSocket = new Socket(host, port);
eventsLog.writeEventsLog("Connected to Public Chat Server: " +
host + ", at Port: " + port + "...");
try
{//begin try
eventsLog.writeEventsLog("Setting up I/O streams...");
output = new ObjectOutputStream(
publicChatSocket.getOutputStream());
output.flush();
input = new ObjectInputStream(
publicChatSocket.getInputStream());
output.writeUTF(userName);
output.writeUTF(" >> connecting to Chat Server...");
output.flush();
}//end try
catch(IOException io)
{//begin catch
publicChatSocket.close();
eventsLog.writeEventsLog("I/O Error encountered...\n" + io);
}//end catch
publicChatThread = new Thread(this);
publicChatThread.start();
setVisible(true);
eventsLog.writeEventsLog("Public Chat is ready...");
}//end if
}//end start()
public synchronized void stop() throws IOException
{//begin stop()
if(publicChatThread != null)
{//begin if
publicChatThread.interrupt();
publicChatThread = null;
output.close();
input.close();
}//end if
}//end stop()
public void run()
{//begin run()
try
{//begin try
while (!Thread.interrupted())
{//begin while
userName = input.readUTF();
chatString = input.readUTF();
updateChatArea(userName, chatString);
}//end while
}//end try
catch(Exception ex)
{//begin catch
ex.printStackTrace();
}//end catch
/* catch(IOException io)
{//begin catch
io.printStackTrace();
}//end catch
*/ }//end run()
public void updateChatArea(String userNameAndChatString, String chatString)
{//begin updateChatArea()
txtChatArea.append(userName + " >> " + chatString + "\n");
txtChatArea.setCaretPosition(txtChatArea.getText().length());
txtChatString.requestFocus();
}//end updateChatArea()
public void sendChatString(String userName, String chatString)
{//begin sendChatString()
try
{//begin try
output.writeUTF(userName);
output.writeUTF(chatString);
output.flush();
}//end try
catch(IOException io)
{//begin catch
io.printStackTrace();
}//end catch
catch(NullPointerException noe)
{//begin catch
noe.printStackTrace();
}//end catch
}//end sendChatString()
public void cleanUpProcesses()
{//begin cleanUpProcesses()
try
{//begin try
stop();
status.setPublicChatLoaded(false);
dispose();
}//end try
catch(IOException io)
{//begin catch
}//end catch
}//end cleanUpProcesses()
}//end class PublicChat
Re: Now JInternalFrame closing method is not working...:(
Based on the solution available in the previous JFrame given by Kean,
I tried it on JInternalFrame as well.
code required:
import javax.swing.event.*; // this import is required to get the
InternalFrameEvent
class myJInternalFrame extends JInternalFrame implements
JInternalFrameListener
{
public myJInternalFrame()
{
addInternalFrameListener(this);
}
//all the methods for JInternalFrameListener here, almost like
WindowListener
public void internalFrameClosing(InternalFrameEvent e)
{
System.out.println("Internal frame is closing");
}
}
--
Best Regards,
Wing Hoe
---------------------------------------------------------------
ICQ: 2213281
Email: winghoe@hotmail.com
www: http://pwp.maxis.net.my/winghoe
---------------------------------------------------------------