hi everyone
I want to ask you about three things:
first:
I want just to ensure that I am thinking in the right way
so I want you to tell me is it correct that
the String can save multiple lines of string in on variable
for example: String s="first line \n\n second line\n\n third line\n\n"; << I have lots of lines to store it in one string<< is it correct
Second:
I want you to tell me how I can display this string on a panel. I tried Label by using setText method ,but I had read that it just used for short strings
also I tried JTextFile but it does not work
so could you please tell me how I can do that.
Last thing:
In my client server program sometimes the client receive the answer from the server and sometimes it does not
how I can ensure that the client receive each answer from the server correctly
notice that I already used flush() method but also the sending message did not arrive
Thanks alot
how I can display multi-lines string on panel
Page 1 of 112 Replies - 979 Views - Last Post: 10 January 2012 - 06:53 AM
Replies To: how I can display multi-lines string on panel
#2
Re: how I can display multi-lines string on panel
Posted 09 January 2012 - 12:45 PM
1)Yes
2) Shows us your code
3) Look point 2)
2) Shows us your code
3) Look point 2)
#3
Re: how I can display multi-lines string on panel
Posted 09 January 2012 - 01:18 PM
Here is my whole program
i wrote the interfaces inside the client class
the classes to build the store
i wrote the interfaces inside the client class
package mall;
import java.net.*;
import java.io.*;
public class MallServer {
public static ServerSocket serverSocket = null;
public static Socket clientSocket = null;
public static ServerProcessor processor;
static ShoppingCart sh=null;
static Customer c=null;
static String s=null,uu=null;
static Store store;
public static void main(String[] args){
int port = 1237;
//...............................
// create stores in the store we here
store=new Store("Jeddah Mall");
// create store items and add it to the store
Item game1=new Item(1,"fulla",90.0);
store.addItem(game1);
Item game2=new Item(2,"D chessboard",250.0);
store.addItem(game2);
Item game3=new Item(3,"havana",300.0);
store.addItem(game3);
Item shoe1=new Item(4,"D puma",400);
store.addItem(shoe1);
Item shoe2=new Item(5,"D adidas",300);
store.addItem(shoe2);
Item shoe3=new Item(6,"Nike",90);
store.addItem(shoe3);
Item book1=new Item(7,"D ITfundemital",300.50);
store.addItem(book1);
Item book2=new Item(8," CSfundemital",200.0);
store.addItem(book2);
Item book3=new Item(9,"ISfundemital",250.0);
store.addItem(book3);
// create the connection
try {
processor =new ServerProcessor();
serverSocket = new ServerSocket(port);
while(true){
clientSocket = serverSocket.accept();
storeThread mine = new storeThread(clientSocket);
mine.start();
}// while
}catch(IOException e){
System.err.println(e.getMessage()); }
} // main
}//MallServer
// thread to make the server serve more than one client
class storeThread extends Thread{
Socket client;
static PrintWriter out = null;
static BufferedReader in = null;
String inputLine = null;
static ObjectInputStream ob1=null;
static ObjectOutputStream ob2=null;
public storeThread(Socket client){
this.client = client;
}//end myThread Constructor
public void run(){
try {
ob1=new ObjectInputStream(client.getInputStream());
ob2=new ObjectOutputStream(client.getOutputStream());
out = new PrintWriter(client.getOutputStream(),true);
in= new BufferedReader(new InputStreamReader(client.getInputStream()));
// read client inputs
try{
MallServer.c =(Customer)ob1.readObject();
MallServer.sh=(ShoppingCart)ob1.readObject();
MallServer.c.addShoppingCart(MallServer.sh);
MallServer.store.enter(MallServer.c);
}catch(ClassNotFoundException ell){}
while ((inputLine = in.readLine())!=null) {
// check the request kind
if (inputLine.startsWith("view")) {
out.print(MallServer.processor.view());
out.flush();
}else if (inputLine.startsWith("add")) {
MallServer.processor.buyitem(inputLine);
out.print("item has been added");
//out.flush();
}else if (inputLine.startsWith("total")) {
out.print(MallServer.processor.total());
out.flush();
}
else if (inputLine.startsWith("myitems")) {
out.print(MallServer.processor.viewMyitems());
out.flush();
}
else if (inputLine.startsWith("done")) {
out.println(MallServer.processor.DONE());
out.flush();
break;
}
out.flush();
}//while
} catch (IOException e) { // for big try
e.printStackTrace();
}finally{
try{
client.close();
}catch (IOException e) { // for big try
e.printStackTrace();}
}
}//end run
}//end myThread class
package mall;
import java.util.*;
public class ServerProcessor {
LinkedList<Item> sitems = new LinkedList<Item>();
LinkedList<Item> myitems = new LinkedList<Item>();
int d;
public ServerProcessor (){
}//constructor
public String view(){
String s=null;
// get the store items and save it in item linked list
sitems =MallServer.store.items();
for(int i=0;i< sitems.size();i++){
s=s+"Item number: "+sitems.indexOf(sitems.get(i))
+" Item name: "+sitems.get(i).itname+" \n\n\n";
}
s=s.trim();
// send them back to the server
return s;
}
//...........................
public void buyitem(String s){
if(s.contains("1")){
d=1;
}else if(s.contains("0")){
d=0;
}else if(s.contains("2")){
d=2;
}else if(s.contains("3")){
d=3;
}else if(s.contains("4")){
d=4;
}else if(s.contains("5")){
d=5;
}else if(s.contains("6")){
d=6;
}else if(s.contains("7")){
d=7;
}else if(s.contains("8")){
d=8;
}else {
d=9;
}
// get store items in items linked list
sitems =MallServer.store.items();
// add it to customer cart
MallServer.sh.addItem(sitems.get(d));
}
//...................................
public String total(){
double t=0.0;
// get the customer items and calculate the price using the Shopping cart method
t=MallServer.sh.totalprice();
long c=Math.round(t);
String h=Long.toString(c);
return h;
}
//.....................................
public String viewMyitems(){
String s=null;
//put all user items in an item linked list
myitems =MallServer.sh.items();
for(int i=0;i< myitems.size();i++){
s=s+"Item number: "+myitems.indexOf(myitems.get(i))
+" Item name: "+myitems.get(i).itname+" \n\n\n";
}
s=s.trim();
// return it to the server
return s;
}
//...................................
public String DONE(){
MallServer.store.exit(MallServer.c);
return "thanks for visiting us";
}
}//server processor
package mall;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.LinkedList;
import javax.swing.*;
public class Client1{
public static String host = "localhost";
public static int port = 1237;
public static Socket clientSocket = null;
static String name,output,input;
static JLabel label2;
static String items=null, a;
double t=0.0;
public static Customer c=null;
public static ShoppingCart sh=null;
static LinkedList <Item> sitems=null;
static LinkedList <Item> myitems=null;
public static String s=null,l=null;
public static void main(String[] args) throws IOException {
try {
sh=new ShoppingCart();
c=new Customer("hooor");
// connect the client to the server
clientSocket = new Socket(host, port);
final PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
final BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
final ObjectOutputStream ob=new ObjectOutputStream(clientSocket.getOutputStream());
ob.writeObject(c);
ob.writeObject(sh);
ob.flush();
// create the interface the panel
final Panel panel=new Panel();
// the panel background color
panel.setBackground(new Color(102, 0, 102));
// create label to put on it welcome message
JLabel label=new JLabel();
// set label properties
label.setFont(new java.awt.Font("Times New Roman", 1, 36)); // NOI18N
label.setForeground(new Color(255, 255, 255));
label.setText("Welcome to our Store");
// add the label to the panel
panel.add(label);
// define and add button to let user view store items
JButton viewButton=new JButton();
viewButton.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
viewButton.setText("View Store Items");
viewButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try{
out.println("view");
out.flush();
items=in.readLine();
// display the answer
// get all items number and name and put them in a string
JOptionPane.showMessageDialog(panel,"Store Items: \n\n"+input);
//label2.setText(s);
}catch(IOException ee){}
}
});
// add the button to the panel
panel.add(viewButton);
//***************
// define and add button to let user buy items
JButton addButton=new JButton();
addButton.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
addButton.setText("Buy Item");
addButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try{
// show dialog to get item number to buy it
a=JOptionPane.showInputDialog(panel,"Enter the item number as shown in the view item list","Add Item",1);
// send it to the server
out.println("add_"+a);
out.flush();
//read server response
l=in.readLine();
// display it to the user
JOptionPane.showMessageDialog(panel,l);
}catch(IOException t){}
}});
//add the button to panel 2
panel.add(addButton);
//************************
// define and add button to let user view his items
JButton myButton=new JButton();
myButton.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
myButton.setText("View Your Items");
myButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// send request to the server
out.println("myitems");
out.flush();
try{
items=in.readLine();
// display the answer
label2.setText("You have buyes:\n\n "+items);
}catch(IOException t){}
}});
// add to the panel2
panel.add(myButton);
//**************************
//*****************
// define and add button to let user get his total price
JButton priceButton=new JButton();
priceButton.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
priceButton.setText("Get Total price");
priceButton.setToolTipText("");
priceButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try{
// send the request to the server
out.print("total");
out.flush();
// get answer
String i=in.readLine();
// display the price
JOptionPane.showMessageDialog(panel,"The Total Price is: "+ i);
}catch(IOException t){}
}});
panel.add(priceButton);
///.............................................
// define and add button to let user exit the store
JButton exButton=new JButton();
exButton.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
exButton.setText("Exit");
exButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try{
// show message to get sure that he want to exit
int f=JOptionPane.showConfirmDialog(null,"Do You want to exit? ","Exit",0, 3);
//System.out.println(f);
if(f==0){// if he want to exit
//send request of exit to the server
out.print("done");
out.flush();
// get answer
input=in.readLine();
// display it
JOptionPane.showMessageDialog( panel, input);
}
}catch(IOException ee){}
}});
// add the button to panel2
panel.add(exButton);
//...............
// here we add another label as an empty space to show the results
label2 =new JLabel();
label2.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
label2.setForeground(new java.awt.Color(255, 255, 255));
panel.add(label2);
// create frame to add the panel to it
JFrame frame = new JFrame("My Store");
// set frame properties
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int height = screenSize.height;
int width = screenSize.width;
frame.setSize(width/3, height/2);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// add the panel to the frame
frame.getContentPane().add(panel);
// exception for the connection failure
}catch (UnknownHostException e) {
System.err.println("Don't know about host:" + host);
System.exit(1);
} catch (IOException exp){
System.err.println("Couldn't get I/O for the connection to:" + host);
System.exit(1);
}
}//main
}//client1
the classes to build the store
package mall;
// imports statment
// improt Statments
import java.util.*;
// interface named Store and and inside it their methods
public class Store{
// Lest of used variables
Item item1=null;
String stname=null;// store name
LinkedList <Item> items = new LinkedList<Item>(); // list of the available items
LinkedList <Customer> customers = new LinkedList<Customer>(); // list of the available customers
//Constructor here we make the parameters of the constructor stored in local variables to use it in other methods
public Store(String name) {
this.stname=name;
}//Constructor
//........................................
//this method enter customer c to the store
public void enter(Customer c){
customers.add(c);
}// enter
//................................
//method to returns an empty shopping cart
public ShoppingCart getShopping(){
return new ShoppingCart();
}
//........................................
//this method exits customer c from the store
public void exit(Customer c){
customers.remove(c);
}//exit
//........................................
// this method returns customers in the store
public LinkedList<Customer> customers(){
return customers;
}//customer
//........................................
// this method used when add items and notify its observers when any item added
public void addItem(Item item){
items.add(item);
}// addItem
//........................................
// this method returns items available for sale in the store
public LinkedList<Item> items(){
return items;
}// available items
}
package mall;
public class Customer implements Serializable{
private ShoppingCart shopingcart=null;
private String custname=null;
//........................................
//constructor which declare customer name name;
public Customer(String name) {
this.custname=name;
} // constructor
//add Shopping cart that being used by the customer
public void addShoppingCart(ShoppingCart sc){
this.shopingcart=sc;
}//add Shopping cart
}
package mall;
//class for create item object
public class Item {
String itname=null;
int id=0;
double price=0.0;
// here we just store the variables sent by user in the local variables
public Item (int id,String itname,double price) { //take id, name, number of story and price
this.id=id;
this.itname=itname;
this.price=price;
} //Constructor
}//end class
package mall;
import java.io.Serializable;
import java.util.*;
public class ShoppingCart implements Serializable{
LinkedList <Item> items = new LinkedList<Item>(); // to save the items of the shopping cart
public ShoppingCart() {
} // Constructor
//........................................
//add item in the shopping cart
public void addItem(Item item){
items.add(item);
}//add item
//........................................
//remove item from the the shopping cart
public void removeItem(Item item){
items.remove(item);
}//remove item
//........................................
//return items currently in the cart
public LinkedList<Item> items(){
return items;
}//items
//........................................
//to compute total price of items with discount or not then return it
public double totalprice(){
double t=0.0;
double tem=0.0,p;
for(int i=0;i<items.size();i++){
if((items.get(i).itname).startsWith("D")){
tem=items.get(i).price;
p=tem*0.03;
tem=tem-p;
t+=tem;
}else{
tem=items.get(i).price;
t+=tem;
}
}
return t;
}//total price
}// shoppingCart
#4
Re: how I can display multi-lines string on panel
Posted 09 January 2012 - 03:10 PM
Consider using something like JTextArea
#5
Re: how I can display multi-lines string on panel
Posted 09 January 2012 - 03:18 PM
and do not mix different type of reading on the socket
Read Objects or read bytes but not both
If you need messages containing other things than Customer and ShoppingCart just writeObject()/readObject() String. Stringare objects and you have have to worry about endOfLine, flush, ...
ob1=new ObjectInputStream(client.getInputStream());
in= new BufferedReader(new InputStreamReader(client.getInputStream()));
Read Objects or read bytes but not both
If you need messages containing other things than Customer and ShoppingCart just writeObject()/readObject() String. Stringare objects and you have have to worry about endOfLine, flush, ...
#6
Re: how I can display multi-lines string on panel
Posted 09 January 2012 - 03:24 PM
Or use a class fro all your message
Now you just read/write objects of type Message
and you can define your protocol
if type == 1 the message contains the ShoppingCart sc
if type == 2 the message contains the Client client
if type == 3 the message contaisn both sc and client
if type == 4 look at the String
if type == 5 is a total
if type == 6 is a my items
.....
class Message implements Serializable {
int type;
ShoppingCart sc;
Client client;
String otherMessage;
}
Now you just read/write objects of type Message
and you can define your protocol
if type == 1 the message contains the ShoppingCart sc
if type == 2 the message contains the Client client
if type == 3 the message contaisn both sc and client
if type == 4 look at the String
if type == 5 is a total
if type == 6 is a my items
.....
#7
Re: how I can display multi-lines string on panel
Posted 09 January 2012 - 04:10 PM
I will try that
but I have one more question
is the way that I create store and then the customer and the cart right
I mean is it a good way to create the store and its item in side the server
then create the customer and the cart inside the server thread ??
I want to get sure that this part is not reason for my code fails.
now when I run the code and click on any buttons the the interface stocks and did not return any thing and did not let me click any other buttons after that
but I have one more question
is the way that I create store and then the customer and the cart right
I mean is it a good way to create the store and its item in side the server
then create the customer and the cart inside the server thread ??
I want to get sure that this part is not reason for my code fails.
now when I run the code and click on any buttons the the interface stocks and did not return any thing and did not let me click any other buttons after that
#8
Re: how I can display multi-lines string on panel
Posted 09 January 2012 - 08:06 PM
You do as you wish but as I have seen
MallServer.c =(Customer)ob1.readObject();
MallServer.sh=(ShoppingCart)ob1.readObject();
if obviously means that you
writeObject(aCustomer);
writeObject(aShoppingCart);
so why not combining them ? Right now you can't because you have different objects.
If you have a class Message and you always send object of type Message it will simplify your life a lot.
Then you do not have to send different String that you have to interpret, just switch() on the int type included in your Message object
Your lost frame is probably due to an unsynchronization between the type of frame sent and what you try to read at the other end of the wire
If you have a Java client that talks to a C++ server, or vice-versa, it makes sense to send/read bytes
If you have Java that talk to Java, and if you have to pass an object of any type does not make sense to standardize all your exchanges to a single type of object with a flag inside it indentifying the type of message it is and what it contains.
MallServer.c =(Customer)ob1.readObject();
MallServer.sh=(ShoppingCart)ob1.readObject();
if obviously means that you
writeObject(aCustomer);
writeObject(aShoppingCart);
so why not combining them ? Right now you can't because you have different objects.
If you have a class Message and you always send object of type Message it will simplify your life a lot.
Then you do not have to send different String that you have to interpret, just switch() on the int type included in your Message object
Your lost frame is probably due to an unsynchronization between the type of frame sent and what you try to read at the other end of the wire
If you have a Java client that talks to a C++ server, or vice-versa, it makes sense to send/read bytes
If you have Java that talk to Java, and if you have to pass an object of any type does not make sense to standardize all your exchanges to a single type of object with a flag inside it indentifying the type of message it is and what it contains.
This post has been edited by pbl: 09 January 2012 - 10:23 PM
#9
Re: how I can display multi-lines string on panel
Posted 10 January 2012 - 03:30 AM
I do not know if I understand you correctly
but I have done some changes to my client and server as I understand
but program still not working correctly
here is my code after changing
could you please tell me why it still not working
cause I have to deliver this code to my instructor tomorrow
and it still work incorrectly
I attached the run after i click any button the program stop working
but I have done some changes to my client and server as I understand
but program still not working correctly
here is my code after changing
import java.io.Serializable;
public class MSG implements Serializable {
int type=0;
Object o=null;
public MSG(int type,Object o){
this.type=type;
this.o=o;
}
}//class
import java.net.*;
import java.io.*;
public class MallServer {
public static ServerSocket serverSocket = null;
public static Socket clientSocket = null;
public static ServerProcessor processor;
static ShoppingCart sh=null;
static Customer c=null;
static String s=null,uu=null;
static Store store;
public static void main(String[] args){
int port = 1237;
//...............................
// create stores in the store we here
store=new Store("Jeddah Mall");
// create store items and add it to the store
Item game1=new Item(1,"fulla",90.0);
store.addItem(game1);
Item game2=new Item(2,"D chessboard",250.0);
store.addItem(game2);
Item game3=new Item(3,"havana",300.0);
store.addItem(game3);
Item shoe1=new Item(4,"D puma",400);
store.addItem(shoe1);
Item shoe2=new Item(5,"D adidas",300);
store.addItem(shoe2);
Item shoe3=new Item(6,"Nike",90);
store.addItem(shoe3);
Item book1=new Item(7,"D ITfundemital",300.50);
store.addItem(book1);
Item book2=new Item(8," CSfundemital",200.0);
store.addItem(book2);
Item book3=new Item(9,"ISfundemital",250.0);
store.addItem(book3);
// create the connection
try {
processor =new ServerProcessor();
serverSocket = new ServerSocket(port);
while(true){
clientSocket = serverSocket.accept();
storeThread mine = new storeThread(clientSocket);
mine.start();
}// while
}catch(IOException e){
System.err.println(e.getMessage()); }
} // main
}//MallServer
// thread to make the server serve more than one client
class storeThread extends Thread{
Socket client;
int x;
static BufferedReader in = null;
String inputLine = null;
MSG obj=null;
static ObjectInputStream ob1=null;
static ObjectOutputStream ob2=null;
public storeThread(Socket client){
this.client = client;
}//end myThread Constructor
public void run(){
try {
ob1=new ObjectInputStream(client.getInputStream());
ob2=new ObjectOutputStream(client.getOutputStream());
// read client inputs
try{
while ((obj = (MSG)ob1.readObject())!=null) {
switch(x=obj.type){
case 1:
MallServer.c=(Customer)obj.o;
case 2:
MallServer.sh=(ShoppingCart)obj.o;
MallServer.c.addShoppingCart(MallServer.sh);
MallServer.store.enter(MallServer.c);
case 3:{
inputLine=(String)obj.o;
// check the request kind
if (inputLine.startsWith("view")) {
ob2.writeObject(new MSG(3,MallServer.processor.view()));
}else if (inputLine.startsWith("add")) {
MallServer.processor.buyitem(inputLine);
ob2.writeObject(new MSG(3,"item has been added"));
//
}else if (inputLine.startsWith("total")) {
ob2.writeObject(new MSG(3,MallServer.processor.total()));
}
else if (inputLine.startsWith("myitems")) {
ob2.writeObject(new MSG(3,MallServer.processor.viewMyitems()));
}
else if (inputLine.startsWith("done")) {
ob2.writeObject(new MSG(3,MallServer.processor.DONE()));
break;
}
}//case 3
}//switch
}//while
}catch(ClassNotFoundException el){}
} catch (IOException e) { // for big try
e.printStackTrace();
}/*finally{
try{
client.close();
}catch (IOException e) { // for big try
e.printStackTrace();}}*/
}//end run
}//end myThread class
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.LinkedList;
import javax.swing.*;
public class Client1{
public static String host = "localhost";
public static int port = 1237;
public static Socket clientSocket = null;
static String name,output,input;
static JTextArea label2;
static String items=null, a;
static MSG b=null;
public static Customer c=null;
public static ShoppingCart sh=null;
public static String s=null,l=null;
public static void main(String[] args) throws IOException, ClassNotFoundException {
try {
sh=new ShoppingCart();
c=new Customer("hooor");
// connect the client to the server
clientSocket = new Socket(host, port);
final ObjectOutputStream ob=new ObjectOutputStream(clientSocket.getOutputStream());
final ObjectInputStream ob1=new ObjectInputStream(clientSocket.getInputStream());
// create the customer and the shopping car for the
//client and send them to the server
ob.writeObject(new MSG(1,c));
ob.writeObject(new MSG(2,sh));
ob.flush();
// create the interface the panel
final Panel panel=new Panel();
// the panel background color
panel.setBackground(new Color(102, 0, 102));
// create label to put on it welcome message
JLabel label=new JLabel();
// set label properties
label.setFont(new java.awt.Font("Times New Roman", 1, 36)); // NOI18N
label.setForeground(new Color(255, 255, 255));
label.setText("Welcome to our Store");
// add the label to the panel
panel.add(label);
// create frame to add the panel to it
JFrame frame = new JFrame("My Store");
// set frame properties
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int height = screenSize.height;
int width = screenSize.width;
frame.setSize(width/3, height/2);
frame.setLocationRelativeTo(null);
// define and add button to let user view store items
JButton viewButton=new JButton();
viewButton.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
viewButton.setText("View Store Items");
viewButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try{
// send order
ob.writeObject(new MSG(3,"view"));
ob.flush();
// receive the server answer
b=(MSG)ob1.readObject();
s=(String)b.o;
// display the answer
//JOptionPane.showMessageDialog(panel,"Store Items: \n\n"+input);
label2.setText(s);
}catch(ClassNotFoundException s){}
catch(IOException ee){}}});
// add the button to the panel
panel.add(viewButton);
//***************
// define and add button to let user buy items
JButton addButton=new JButton();
addButton.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
addButton.setText("Buy Item");
addButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try{
// show dialog to get item number to buy it
a=JOptionPane.showInputDialog(panel,"Enter the item number as shown in the view item list","Add Item",1);
//System.out.print(a);
// send it to the server
ob.writeObject(new MSG(3,"add_"+a));
ob.flush();
//read server response
b=(MSG)ob1.readObject();
l=(String)b.o;
// display it to the user
JOptionPane.showMessageDialog(panel,l);
}catch(ClassNotFoundException s){}
catch(IOException ee){}}});
//add the button to panel 2
panel.add(addButton);
//************************
// define and add button to let user view his items
JButton myButton=new JButton();
myButton.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
myButton.setText("View Your Items");
myButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// send request to the server
try{
ob.writeObject(new MSG(3,"myitems"));
ob.flush();
// read the answer
b=(MSG)ob1.readObject();
items=(String)b.o;
// display the answer
label2.setText("You have buyes:\n\n "+items);
}catch(ClassNotFoundException s){}
catch(IOException ee){}}});
// add to the panel2
panel.add(myButton);
//**************************
// define and add button to let user get his total price
JButton priceButton=new JButton();
priceButton.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
priceButton.setText("Get Total price");
priceButton.setToolTipText("");
priceButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try{
// send the request to the server
ob.writeObject(new MSG(3,"total"));
ob.flush();
// get answer
b=(MSG)ob1.readObject();
String i=(String)b.o;
// display the price
JOptionPane.showMessageDialog(panel,"The Total Price is: "+ i);
}catch(ClassNotFoundException s){}
catch(IOException ee){}}});
panel.add(priceButton);
///.............................................
// define and add button to let user exit the store
JButton exButton=new JButton();
exButton.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
exButton.setText("Exit");
exButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try{
// show message to get sure that he want to exit
int f=JOptionPane.showConfirmDialog(null,"Do You want to exit? ","Exit",0, 3);
//System.out.println(f);
if(f==0){// if he want to exit
//send request of exit to the server
ob.writeObject(new MSG(3,"done"));
ob.flush();
// get answer
b=(MSG)ob1.readObject();
input=(String)b.o;
// display it
JOptionPane.showMessageDialog( panel, input);
}
}catch(ClassNotFoundException s){}
catch(IOException ee){}}});
// add the button to panel2
panel.add(exButton);
//...............
// here we add another label as an empty space to show the results
label2 =new JTextArea();
label2.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
label2.setForeground(new java.awt.Color(255, 255, 255));
panel.add(label2);
// add the panel to the frame
frame.getContentPane().add(panel);
frame.setVisible(true);
// exception for the connection failure
}catch (UnknownHostException e) {
System.err.println("Don't know about host:" + host);
System.exit(1);
} catch (IOException exp){
System.err.println("Couldn't get I/O for the connection to:" + host);
System.exit(1);
}
}//main
}//client1
could you please tell me why it still not working
cause I have to deliver this code to my instructor tomorrow
and it still work incorrectly
I attached the run after i click any button the program stop working
Attached image(s)
#10
Re: how I can display multi-lines string on panel
Posted 10 January 2012 - 05:25 AM
Thanks a lot for helping me
Finally, it works correctly
Finally, it works correctly
#11
Re: how I can display multi-lines string on panel
Posted 10 January 2012 - 06:39 AM
You can't do that
Only one static instance od one ShoppingCart and one Customer
Multiple Clients will be connected at the same time, they cannot share and used at the same time the same variables to store your data.
You will need a ShoppingCart and a Customer in the instance variables of each storeThread object.
Imagine Client1 thread doing
- MallServer.sh=(ShoppingCart)obj.o;
- MallServer.c.addShoppingCart(MallServer.sh);
Clien2t thread doing
- MallServer.sh=(ShoppingCart)obj.o;
- MallServer.c.addShoppingCart(MallServer.sh);
Client1 and Client2 doing
- MallServer.store.enter(MallServer.c);
you are using twice the MailServer.c, the one of Client1 is lost
Beside your main() method, noting should be static.
public class MallServer {
public static ServerProcessor processor;
static ShoppingCart sh=null; // <--------
static Customer c=null; // <--------
case 2:
MallServer.sh=(ShoppingCart)obj.o;
MallServer.c.addShoppingCart(MallServer.sh);
MallServer.store.enter(MallServer.c);
Only one static instance od one ShoppingCart and one Customer
Multiple Clients will be connected at the same time, they cannot share and used at the same time the same variables to store your data.
You will need a ShoppingCart and a Customer in the instance variables of each storeThread object.
Imagine Client1 thread doing
- MallServer.sh=(ShoppingCart)obj.o;
- MallServer.c.addShoppingCart(MallServer.sh);
Clien2t thread doing
- MallServer.sh=(ShoppingCart)obj.o;
- MallServer.c.addShoppingCart(MallServer.sh);
Client1 and Client2 doing
- MallServer.store.enter(MallServer.c);
you are using twice the MailServer.c, the one of Client1 is lost
Beside your main() method, noting should be static.
#12
Re: how I can display multi-lines string on panel
Posted 10 January 2012 - 06:44 AM
May be it will work if you have only one Client at a time.
Also get ride of all your String.compare() simply used different Message Type, a lot more efficient
Also get ride of all your String.compare() simply used different Message Type, a lot more efficient
#13
Re: how I can display multi-lines string on panel
Posted 10 January 2012 - 06:53 AM
you really helped me
I fixed that part depending on your advice
now it serves more than one client
Thanks a lot
I fixed that part depending on your advice
now it serves more than one client
Thanks a lot
This post has been edited by Hooor: 10 January 2012 - 07:06 AM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|