home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

java help

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1450
Discussions: 200,927, Posts: 2,379,136, Members: 246,293
Old January 12th, 2006, 11:59 PM   Digg it!   #1 (permalink)
Member
 
Join Date: May 2005
Location: NYC
Posts: 370
Send a message via AIM to sunny22
java help

I wrote a simple instant messaging program with no gui. I designed the gui seperately. Now i want to run the program with the gui, how do i do that?

Heres the code:

Socket Server-

import java.util.*;
import java.io.*;
import java.net.*;

class MessageServer {

private static ServerSocket servSock;
private static final int PORT = 1234; // connect on port 1234

public static void main(String[] args) {

System.out.println("Opening port... \n");
try {
servSock = new ServerSocket(PORT);
}

catch(IOException e) {
System.out.println("Unable to set up port!");
System.exit(1); // exit if there is a connection problem
}

do {
run();
} while (true); // infinite loop
}

private static void run() {

Socket link = null;

try {
link = servSock.accept(); // wait on server until message is recieved
Scanner input = new Scanner(link.getInputStream());
PrintWriter out = new PrintWriter(
link.getOutputStream(),true); // output

int numMessages = 0;
String message = input.nextLine();

while (!message.equals("***CLOSE***")) {
System.out.println("Message Recieved.");
numMessages++;
out.println("Message " + numMessages
+ ": " + message);
message = input.nextLine();
}

out.println(numMessages + " messages recieved.");
input.close();
link.close();
}

catch (IOException e) {
e.printStackTrace();
}

finally {
try {
System.out.println("\n* Closing connection...*");
link.close();
}

catch (IOException e) {
System.out.println("Unable to disconnect!");
System.exit(1);
}
}
}
}



client -

import java.util.*;
import java.io.*;
import java.net.*;

class MessageClient {

private static InetAddress host;

private static final int PORT = 1234; // connect to server on port 1234

public static void main(String args[]) {
try {
host = InetAddress.getLocalHost();
}

catch (UnknownHostException e) {
System.out.println("Host not found!");
System.exit(1);
}
run();
}

private static void run() {


Socket link = null;

try {
link = new Socket(host,PORT);
Scanner networkInput =
new Scanner(link.getInputStream());
PrintWriter out = new PrintWriter(
link.getOutputStream(),true);

//Set up scanner for keyboard entry
Scanner userEntry = new Scanner(System.in);

String message="", response;
do {
System.out.print("Enter message: ");
message = userEntry.nextLine();
out.println(message);
response = networkInput.nextLine();
System.out.println("\nType> " + response);

} while (!message.equals("***CLOSE***"));
}
catch (IOException e) {
e.printStackTrace();
}

finally {

try {
System.out.println("\n* Closing connection...*");
link.close();
}
catch (IOException e) {
System.out.println("Unable to disconnect!");
System.exit(1);
}
}
}
}



gui -

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.net.*;

public class Simple extends JFrame implements ActionListener {

private static Simple frame;
private static JButton button;
private static JButton button1;
private static JButton button2;
private static JButton button3;
private static JLabel label;
private static JTextField line;
private static JTextField text;

public static void main(String[] args) {

Simple frame = new Simple();
frame.setTitle("Chatroom");
frame.setSize(500,500);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}

public Simple() {

setLayout(new FlowLayout());

JTextArea text = new JTextArea(24,40);
text.setWrapStyleWord(true);
text.setLineWrap(true);

add(new JScrollPane(text));

button1 = new JButton("Submit");
button = new JButton("Quit");

JLabel label = new JLabel();
JTextField line = new JTextField(40);
add(line);
add(label);

add(button1);
add(button);


button1.addActionListener(this);
button.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == button1)

if (e.getSource() == button)
System.exit(0);

}
}


sorry for the long post
__________________
MSI K8N Neo4 Platinum SLI Socket 939 nForce4
Athlon 64 3200+ Venice Socket 939
CORSAIR XMS 2GB 184-Pin DDR 400 (PC 3200)
ATI x800xl PCI Express
sunny22 is offline   Reply With Quote
Old January 13th, 2006, 12:17 AM     #2 (permalink)
Banned
 
Join Date: Oct 2005
Posts: 752
did u write them from scratch or you get that from the net?
towlies high is offline   Reply With Quote
Old January 13th, 2006, 12:28 AM     #3 (permalink)
Member
 
Join Date: May 2005
Location: NYC
Posts: 370
Send a message via AIM to sunny22
the server and client r part me part a tutorial and the gui is from scratch
none of it is overwhelmingly complicated, i just don't know how to combine the client and gui lol
sunny22 is offline   Reply With Quote
Old January 13th, 2006, 03:13 PM     #4 (permalink)
Banned
 
Join Date: Oct 2005
Posts: 752
maybe ask whoever wrote the tutorial...or published it.
towlies high is offline   Reply With Quote
Old January 13th, 2006, 03:22 PM     #5 (permalink)
Perfetc Member
 
VHockey86's Avatar
 
Join Date: Jan 2003
Location: Maryland Suburbia
Posts: 4,334
You need to setup your action performed method to handle the input from a JTextBox. Then you should be able to feed that back into your non-gui class.

You'll want the main class of your non-gui class to create and instance of the GUI.
VHockey86 is offline   Reply With Quote
Old January 13th, 2006, 03:25 PM     #6 (permalink)
Member
 
Join Date: May 2005
Location: NYC
Posts: 370
Send a message via AIM to sunny22
how would i tell my "submit" button to send the typing to the server.
I dont know of any acrtion listner methods to do it
sunny22 is offline   Reply With Quote
Old January 13th, 2006, 04:13 PM     #7 (permalink)
Perfetc Member
 
VHockey86's Avatar
 
Join Date: Jan 2003
Location: Maryland Suburbia
Posts: 4,334
Create a method in the client like "public void sendMessage(String msg)", which handles sending the message to the server of course.

Then have your action listener take the text from JTextBox when the user hits submit and send it to the sendMessage method.

Here's a link to a directory of code from my computer science 2 projects, which was an instant messaging client.
http://www.andrewpangborn.com/files/.../CS2/project2/

The server however was already written for us, but you may want to look at "ChatWindowGUI.java" for reference on actionPerformed stuff.

Code:
//Creates the input box and puts it in a scroll pane
	textBox = new JTextArea(3,0);
	//Adds a keybinding to the enter key to send messages.
        textBox.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),"enter");
        textBox.getActionMap().put("enter",new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
		    if(textBox.getText().length() > 0) {
			sendMsg(textBox.getText());
		    }
                }
            });
Its setting up a keybinding to the enter key and defining an action for it. It's probably a bit confusing if you are new to java

alternatively, just do something like this
Code:
// misc code
JButton submit = new JButtion("submit);
JTextArea text = new JTextArea(3,0);
submit.addActionListener(this);
// misc code
public void actionPerformed(ActionEvent e) {
            if(e.getSource() == submit) {
                   theClient.sendMessage(text.getText()); //assumes you have a reference to the client object called "theClient"
            }
}

Last edited by VHockey86 : January 13th, 2006 at 04:24 PM.
VHockey86 is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
java hw help sr71000 Webmastering and Programming 2 November 2nd, 2005 09:27 PM
Java Rand Dusing Webmastering and Programming 3 August 17th, 2004 08:42 AM
JAVA 1firecapt General Tech Discussion 0 April 2nd, 2004 09:48 PM
C++ or Java? shawshank62 General Tech Discussion 17 February 25th, 2004 01:04 PM
Java for XP??!??!?!?! skybolt_1 Applications and Operating Systems 4 July 15th, 2002 11:45 AM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (2867)
Obama the Muslim (14)
Is the PSU I received dead? (10)
windows vista security holes (9)
HIS HD5770 graphic card question (15)
Foreign voltage (10)
Print spooler problem (13)
Install XP pro and a Vista laptop ?.. (10)
Dept. of HS: NSA 'Helped' Develop V.. (15)
A good PSU? (10)
Ideal cheap graph card for PC-Gamin.. (15)
New Computer wont recognize XP disc (7)
EVGA 9800 gtx help with finding a g.. (8)
World's largest Monopoly Game using.. (331)
Recent Discussions
Fire in DVD (2)
Safe International POS for CC,DC cash.. (0)
Regular Build (0)
Modern Warfare For the PC (33)
radeon x850xt platinum & shader 3 (3)
Have you switched yet? (84)
Install XP pro and a Vista laptop ?? (10)
Wireless Router+Cable Modems and Much.. (0)
Optical Audio A-B Switch (1)
windows vista security holes (9)
The NTDVM CPU has encountered an ille.. (24)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (34)
Wireless speakers for PC? (11)
Print spooler problem (13)
Help getting around port 80 for camer.. (2)
Display shows 3x5 inch in middle of s.. (3)
monitor will not turn on at all, (1)
World's largest Monopoly Game using G.. (331)
Foreign voltage (10)
FiOS modem/router interfering with ne.. (7)
Browsers wont load websites (2)
Virus Doctor Popup? (1)
Dept. of HS: NSA 'Helped' Develop Vis.. (15)
EVGA 9800 gtx help with finding a goo.. (8)
Problem with speed step/turbo boost? (1)


All times are GMT -4. The time now is 02:16 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28