Just coding for fun. Working on some type of server/client.
Just wondering when I am trying to update the userArea panel why it cannot find the getInetAdress method even though I have used it already.
Full code
Code
/************************************
* Server *
************************************/
import java.io.*; //IOException
import java.net.*; //Socket, ServerSocket, InetAddress, UnknownHostException
import java.util.*; //ArrayList, Date
import java.awt.*; //JFrame
import javax.swing.*; //JTextArea, JScrollPane
public class Server extends JFrame{
private TextPanel textPanel;
private UserPanel userPanel;
private JTextField textField;
private ArrayList users = new ArrayList();
public static void main(String[] args){
new Server();
}
public Server(){
setLayout(new BorderLayout());
textPanel = new TextPanel();
add(textPanel, BorderLayout.CENTER);
userPanel = new UserPanel();
add(userPanel, BorderLayout.EAST);
textField = new JTextField();
add(textField, BorderLayout.SOUTH);
setTitle("Server");
setSize(700,500);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
try
{
ServerSocket serverSocket = new ServerSocket(8000); //attempt to create a server socket
textPanel.addToPanel("Server started at " + new Date() + "\n");
textPanel.addToPanel("Host Name/IP Address: " + InetAddress.getLocalHost() + "\n"); //get host address
while(true){
Socket socket = serverSocket.accept(); //listen for connection requests
textPanel.addToPanel(socket.getInetAddress() + " has connected.\n"); //get client ip
users.add(socket); //add client to the list
userPanel.updateUserList(users);
//Create data input and output streams
DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());
}
}
catch(IOException ex){
System.err.println(ex);
}
}
}
/************************************
* TextPanel *
************************************/
class TextPanel extends JPanel
{
private JTextArea textArea = new JTextArea();
public TextPanel()
{
setLayout(new BorderLayout());
textArea.setEditable(false);
add(new JScrollPane(textArea), BorderLayout.CENTER);
}
void addToPanel(String string)
{
textArea.append(string);
}
}
/************************************
* UserPanel *
************************************/
class UserPanel extends JPanel
{
private JTextArea userArea = new JTextArea();
public UserPanel()
{
setLayout(new BorderLayout());
setPreferredSize(new Dimension(200,475));
userArea.setEditable(false);
add(new JScrollPane(userArea), BorderLayout.CENTER);
}
void updateUserList(ArrayList arrayList)
{
for(int i = 0; i<arrayList.size(); i++)
{
userArea.append((arrayList.get(i)).getInetAddress());
}
}
}
Problem Area
Code
void updateUserList(ArrayList arrayList)
{
for(int i = 0; i<arrayList.size(); i++)
{
userArea.append((arrayList.get(i)).getInetAddress());
}
}
Error
Code
Server.java:101: error: cannot find symbol
userArea.append((arrayList.get(i)).getInetAddress());
^
symbol: method getInetAddress()