|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

05-28-2007, 06:58 AM
|
|
Member
|
|
Join Date: May 2007
Posts: 3
|
|
|
Chat Client
I need to do this for my class
But I am confuse can someone help me out
The GUI to your chat client will look like the following:
The File menu will have Send(Ctrl-S) and Exit(Ctrl-E) menu items. The Edit menu will have a Cut(Ctrl-X), Copy(Ctrl-C), and Paste(Ctrl-V) menu items. The Help menu will have an About(F10) menu item. You will need to add mnemonics to each menu item, as shown in parentheses above. In other words, if the user types Ctrl-S, the text in the text field should be sent to the server, as if the OK button were clicked or the File->Send menu item were clicked.
Hopefully each of those menu items is self-explanatory, but one note is that a user will not be able to cut or paste from the large text field, but only copy. The About box from the Help menu will display a dialog box that shows information such as your name, the name of the application, the version, and of course, your instructor’s name.
The main class for your chat client will be named ChatClient and be in the chatclient.gui package.
For the networking portion of the project, your code will need to communicate with the ChatServer program we wrote in class. The ChatClient will begin by sending a message with a username to the ChatServer. The ChatServer will store the username with each socket connection so that each subsequent message that is sent from the ChatClient will be prepended with the username by the ChatServer before being sent to the other ChatClients. The ChatServer will prepend the username and “->”to the message.
You will also need to read a configuration file that will specify the user’s name, the server’s name, and the port on which the ChatServer is listening. If you would like to add more information into the configuration file, you can, but make sure it at least has these three parameters. Here is a sample configuration file:
# username
username=jeff
# server hostname
hostname=127.0.01
# server port
port=6789
Any line that is blank or starts with a # should be ignored. The rest of the lines will contain key=value pairs. You will read the name of the configuration parameter along the command line in your ChatClient code.
The username is the value that will be prepended to each line sent. The hostname and port give the location of the ChatServer. The ChatServer and ChatClient could be running on the same computer or separate computers.
Further, you need to have a different type of message to allow for private chats. In class, we only implemented broadcasting, meaning that whenever the ChatServer received a message, it sent that message to all users. You will need to allow another type of message to be transmitted that specifies the name of a user followed by the message to send to that user. In that case, only that user will receive the message, but not any other user.
Ensure that no exceptions can be thrown. Even for badly formatted configuration files, there should be no possibility of throwing an exception. Also, you may have helper classes if necessary.
|
|

05-28-2007, 12:02 PM
|
|
Senior Member
|
|
Join Date: Dec 2006
Posts: 748
|
|
Copy/Pasting a homework text here and waiting for help does not work on any forum. You should try to solve your problem yourself and ask help when you are got stuck somewhere.
You can start your research from the following url because you will need to learn how to use Java sockets to solve this homework:
Lesson: All About Sockets (The Java™ Tutorials > Custom Networking)
|
|

05-29-2007, 03:20 AM
|
|
Member
|
|
Join Date: May 2007
Posts: 3
|
|
|
I need help
I need to implement the action to cut, copy, paste and send.
Here in my code I have the actionListener already added but it does not do anything...... Also I need to implement my GUI as a client Server do I need to enter the socket before the main method
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.getKeyStrok e(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.getKeyStrok e(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();
}
}
|
|

05-29-2007, 03:22 AM
|
|
Member
|
|
Join Date: May 2007
Posts: 3
|
|
|
Thank you for the advise I did read the socket tutorial, and it's easy but not when you need to implement a GUI Chat with that
|
|

05-31-2007, 12:37 PM
|
|
Senior Member
|
|
Join Date: Dec 2006
Posts: 748
|
|
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:
/*
* 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..
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|