Thread: Chat Client
View Single Post
  #5 (permalink)  
Old 05-31-2007, 01:37 PM
levent levent is offline
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
Hi again,

I checked your source code. It is running fine. I fixed some minor compilation errors and i guess they were because fo copy/paste to the forum. But in case you need it, i am pasting it to here again:

Code:
/* * Main.java * * Created on 31 May�s 2007 Per�embe, 12:30 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package javaapplication2; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.KeyStroke; public class ChatGui extends JFrame{ //private JTextField TF; //private JTextArea TA; private JButton ok; ChatGui cg; static JTextField tf; static boolean s= true; public ChatGui(){ super("Natalie's Chat Client"); setSize(500,300); setLocation(100,100); setLayout(new BorderLayout()); JTextArea TA = new JTextArea(); add(TA,BorderLayout.CENTER); ok=new JButton("ok"); JPanel TFPanel = new JPanel(); TFPanel.setLayout(new BorderLayout()); JTextField TF = new JTextField(); TFPanel.add(TF,BorderLayout.CENTER); TFPanel.add(ok,BorderLayout.EAST); add(TFPanel,BorderLayout.SOUTH); //add(TF,BorderLayout.SOUTH); TA.setEditable(false); JMenuBar jmb=new JMenuBar(); JMenu FileMenu = new JMenu("File"); JMenuItem SendMenuItem = new JMenuItem("Send"); SendMenuItem.setMnemonic(KeyEvent.VK_S); SendMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK)); //*******add actionevents, to send FileMenu.add(SendMenuItem); JMenuItem ExitMenuItem = new JMenuItem("Exit"); ExitMenuItem.setMnemonic(KeyEvent.VK_E); ExitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,ActionEvent.CTRL_MASK)); ExitMenuItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ System.exit(1); } }); FileMenu.add(ExitMenuItem); jmb.add(FileMenu); JMenu EditMenu = new JMenu("Edit"); JMenuItem CutMenuItem = new JMenuItem("Cut"); CutMenuItem.setMnemonic(KeyEvent.VK_X); CutMenuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_X,ActionEvent.CTRL_MASK)); CutMenuItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ try{ cg.getTextField().cut(); cg.setSaved(false); }catch(NullPointerException npe){} } }); EditMenu.add(CutMenuItem); JMenuItem CopyMenuItem = new JMenuItem("Copy"); CopyMenuItem.setMnemonic(KeyEvent.VK_C); CopyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK)); CopyMenuItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ try{ cg.getTextField().copy(); }catch(NullPointerException npe){} } }); //***********setActionevents from edit menu items,later. EditMenu.add(CopyMenuItem); JMenuItem PasteMenuItem = new JMenuItem("Paste"); PasteMenuItem.setMnemonic(KeyEvent.VK_V); PasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK)); PasteMenuItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ try{ cg.getTextField().paste(); cg.setSaved(false); }catch(NullPointerException npe){} } }); EditMenu.add(PasteMenuItem); jmb.add(EditMenu); JMenu HelpMenu = new JMenu("Help"); JMenuItem AboutMenuItem= new JMenuItem("About"); AboutMenuItem.setMnemonic(KeyEvent.VK_F10); AboutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F10,ActionEvent.SHIFT_MASK)); //*********might be a problem with the mnemonic for about. I'm not sure what to use in place //of the ALT_MASK. AboutMenuItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ JOptionPane.showMessageDialog(ChatGui.this,"Natali e Catherine Martinez\n ChatClient, version one\n co-author: Dr. Jeff Miller"); } }); HelpMenu.add(AboutMenuItem); jmb.add(HelpMenu); setJMenuBar(jmb); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(1); } }); setVisible(true); } public static void setTextField(JTextField jtf) { tf = jtf; } public static JTextField getTextField() { return tf; } public void setSaved(boolean isSaved) { s = isSaved; } public boolean getSaved() { return s; } public static void main(String[] args) { ChatGui cg= new ChatGui(); } }
As far as i see it is working fine. For sockets, you will need to create a thread and will listen for connections with the help of that thread. If you try to listen in Swing event thread, then that can make your GUI freeze. You can create that thread after you pressed any button in your GUI or before GUI is created. It all depends on your requirements.

Let us know if you need further help..
Reply With Quote