andbin ha scritto:
triko ha scritto:
Questa è la classe ThreadManagementChatService
Ok, ora vedo qualcosa di più concreto ma .... credo di aver già ampiamente spiegato la questione. Comunque ripeto.
La riga:
list.select(list.getItemCount()-1);
presuppone che "list" sia un java.awt.List, ovvero un componente "visuale" di cui si vuole selezionare l'ultimo elemento. Ora, nella tua applicazione hai una interfaccia grafica con una listbox?? Dal codice che vedo, a parte un JOptionPane per gli errori, direi di NO.
Quindi quella riga non ti serve.
Allora in realtà non è tutto il codice, Se vuoi ti invio tutte le classi! Se hai voglia di leggerle:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.List;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author MANATEE
*/
public class PanelServerChat extends JFrame
{
private List list;
private JTextField newText = null;
private ThreadManagementChatService managerService = null;
public PanelServerChat()
{
super();
this.setBackground(new Color(50, 100, 255));
//Top panel: messages list
JPanel panList = new JPanel(new BorderLayout(20, 5));
panList.setBackground(new Color(50, 100, 255));
list = new List();
list.setBackground(Color.lightGray);
list.setSize(100, 50);
list.setVisible(true);
//Lateral writes
JLabel chat1 = new JLabel(" Chat ", JLabel.CENTER);
chat1.setForeground(new Color(200, 100, 100));
JLabel chat2 = new JLabel(" Chat ", JLabel.CENTER);
chat2.setForeground(new Color(200, 100, 100));
//Add onject into panel
panList.add(chat1, BorderLayout.WEST);
panList.add(list, BorderLayout.CENTER);
panList.add(chat2, BorderLayout.EAST);
//Insert new message Panel
JPanel newMessage = new JPanel(new BorderLayout(20, 5));
newMessage.setBackground(new Color(50, 100, 225));
JLabel newLab = new JLabel("New Message -> ", JLabel.CENTER);
newLab.setForeground(new Color(255, 255, 0));
newText = new JTextField("");
JButton sendButton = new JButton("SEND");
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String button = e.getActionCommand();
if(button.equals("SEND"));
{
managerService.sendMessage(newText.getText());
newText.setText("");
}
}
});
newMessage.add(newLab, BorderLayout.WEST);
newMessage.add(newText, BorderLayout.CENTER);
newMessage.add(sendButton, BorderLayout.EAST);
this.setLayout(new BorderLayout(0, 5));
add(panList, BorderLayout.CENTER);
add(newMessage, BorderLayout.SOUTH);
connect();
}
public void connect()
{
managerService = new ThreadManagementChatService(10, list);
}
}
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
/**
*
* @author MANATEE
*/
public class ServerChat extends JFrame
{
public ServerChat()
{
super("Chat Server");
this.setSize(new Dimension(500, 300));
this.setLocationRelativeTo(this);
this.setEnabled(true);
this.setBackground(Color.blue);
//create Panel
PanelServerChat pan = new PanelServerChat();
//this.getContentPane().add(pan);
this.add(pan);
this.setVisible(true);
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.List;
import javax.swing.JOptionPane;
/**
*
* @author MANATEE
*/
public class ThreadChatClient implements Runnable
{
private List list;
private Socket client;
private BufferedReader input = null;
private PrintWriter output = null;
Thread me;
public ThreadChatClient(List List, String IpServer, int Port)
{
list = List;
try
{
client = new Socket(IpServer, Port);
input = new BufferedReader(new InputStreamReader(client.getInputStream()));
output = new PrintWriter(client.getOutputStream(), true);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Impossible connect to the server!");
}
me = new Thread(this);
me.start();
}
public void run()
{
while(true)
{
try
{
String message = null;
while((message = input.readLine()) == null)
{ }
managerChat.sendMessage(message);
}
catch(Exception e)
{
output.println("Error sending message to everybody");
}
}
}
public void sendChatMessage(String message)
{
try
{
output.println(message);
}
catch(Exception e)
{ }
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
*
* @author MANATEE
*/
public class ThreadChatConnection implements Runnable
{
private ThreadManagementChatService managerChat;
private Socket client = null;
private BufferedReader input = null;
private PrintWriter output = null;
Thread me;
public ThreadChatConnection(ThreadManagementChatService ManagerChat, Socket Client)
{
managerChat = ManagerChat;
client = Client;
try
{
input = new BufferedReader(new InputStreamReader(client.getInputStream()));
output = new PrintWriter(client.getOutputStream(), true);
}
catch(Exception e)
{
output.println("Error while reading!");
}
me = new Thread(this);
me.start();
}
public void run()
{
while(true)
{
try
{
String message = null;
while((message = input.readLine()) == null)
{ }
managerChat.sendMessage(message);
}
catch(Exception e)
{
output.println("Error sending message to everybody.");
}
}
}
public void sendChatMessage(String message)
{
try
{
output.println(message);
}
catch(Exception e)
{
output.println("Error sending single message.");
}
}
}
import java.net.ServerSocket;
import java.net.Socket;
import java.awt.List;
import javax.swing.JOptionPane;
/**
*
* @author MANATEE
*/
public class ThreadManagementChatService implements Runnable
{
private int maxConnection;
private List list;
private ThreadChatConnection[] listConnection;
Thread me;
private ServerSocket serverChat;
public ThreadManagementChatService(int MaxConnection, List List)
{
maxConnection = MaxConnection-1 ;
list = List;
listConnection = new ThreadChatConnection[MaxConnection];
me = new Thread(this);
me.start();
}
public void run()
{
boolean goon = true;
try
{
serverChat = new ServerSocket(1234);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Impossible to instantiate the server!");
goon = false;
}
if(goon)
{
try
{
for(int users = 0; users < maxConnection; users++)
{
Socket time = null;
time = serverChat.accept();
listConnection[users] = new ThreadChatConnection(this, time);
}
serverChat.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"impossible to instantiate the server!");
}
}
} //End run
public void sendMessage(String message)
{
list.add(message);
//Riga da sistemare
//list.select(list.getItemCount()-1);
for(int users = 0; users < maxConnection; users++)
if(listConnection[users] != null)
listConnection[users].sendChatMessage(message);
}
}