problem getting japplet static field

Hi,

i have a japplet where i have a static jlist field.

i'm trying to implement chat using sockets.

i have thread class. in this class, i want the selected index of jlist used in japplet.

the basic logic is....i run the server, i open 3 instances of japplet. the list will populate with number of clients connected....in this case...3. i select particular value from list and i retrive particular socket object out of a vector (used to store socket object) using this index.

so the problem is when i do ClietApplet.list.getSelectedIndex()...it gives -1

can anyone pls suggest me what would be the problem or what would be other way round ?

[683 byte] By [suhasnda] at [2007-11-26 18:59:41]
# 1

A JList will return a -1 when there is nothing selected. Im finding it very hard to guess what your code looks like, so without much code here is my best guess for what is going on...

you have typed something like:

public static JList list;

in class ClientApplet.

This is all very well, because in public ClientApplet() you will probably do:

list = new JList();

So anything in class ClientApplet, after you have created an instance of it, will have a nice JList they can play with. Problem is, if you try to access list from the static field of ClientApplet and not an instance of ClientApplet, list has not been initialised and hence will naturally have no selected value... I think that makes sense... :-|

Better yet, post some code and Ill have a look for you.

:-)

there is a fellow named camickr who would suggest using an SSCCE (i think that is it...). Much wisdom in this because you'll get a better answer.

Cheers.

Jason.

fireman.sparkeya at 2007-7-9 20:41:09 > top of Java-index,Desktop,Core GUI APIs...
# 2
Hey,thanks a lot....yes...you are right...since the list is not initialised, its not giving particular index selected. i will post the exact code of what i'm doing currently. hope this will make things much clear.
suhasnda at 2007-7-9 20:41:09 > top of Java-index,Desktop,Core GUI APIs...
# 3

here is the client......

// Java Core Packages

import java.awt.*;

import java.awt.event.*;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.net.Socket;

import java.util.*;

// Java extension packages

import javax.swing.*;

public class AppletClient extends JApplet implements ActionListener,Runnable

{

JTextField text = new JTextField(20);

JTextArea area = new JTextArea(20,20);

JScrollPane area_pane = new JScrollPane();

JButton button = new JButton("Send");

JScrollPane scrollpane = new JScrollPane(area);

static JList list = new JList();

JScrollPane list_pane = new JScrollPane();

DefaultListModel model = new DefaultListModel();

OutputStreamWriter outputstream = null;

BufferedReader br;

String txt = "";

protected Thread listener;

public void init()

{

Container content = getContentPane();

content.setBackground(Color.white);

button.addActionListener(this);

area_pane.getViewport().add(area,null);

content.setLayout(new BoxLayout(content,BoxLayout.Y_AXIS));

try

{

Socket socket = new Socket("10.10.10.122",5090);

br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

outputstream = new OutputStreamWriter(socket.getOutputStream());

}

catch (IOException e)

{

}

model.addElement("Client 1");

model.addElement("Client 2");

model.addElement("Client 3");

list.setModel(model);

list.setVisibleRowCount(3);

list.setVisible(true);

list_pane.getViewport().add(list,null);

content.add(list_pane);

content.add(area_pane);

content.add(text);

content.add(button);

listener = new Thread (this);

listener.start ();

}

public void start()

{

}

public void stop()

{

}

public void run ()

{

try

{

while (true)

{

txt = br.readLine() + "\n";

area.append(txt);

}

}

catch (IOException ex)

{

ex.printStackTrace ();

}

finally

{

validate ();

try

{

outputstream.close();

}

catch (IOException ex)

{

ex.printStackTrace ();

}

}

}// method run ends

public void actionPerformed(ActionEvent e)

{

try

{

//int i = list.getSelectedIndex();

//String msg = text.getText() + " " + i;

System.out.println("Index is "+ list.getSelectedIndex());

String msg = text.getText() + "\n";

outputstream.write(msg);

outputstream.flush();

text.setText("");

text.requestFocus();

}

catch (Exception e1)

{

}

}

}// AppletClient Ends

suhasnda at 2007-7-9 20:41:09 > top of Java-index,Desktop,Core GUI APIs...
# 4

here is server and server thread

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.border.*;

import java.net.*;

import java.io.*;

import java.util.*;

public class NewServer

{

public ServerSocket serverSocket = null;

BufferedReader input;

String txt = "";

static int list_val = 0;

OutputStreamWriter outputstream = null;

public NewServer()

{

super();

System.out.println("in constructor");

startSocketWait();

}

public void startSocketWait()

{

try

{

serverSocket = new ServerSocket(5090);

while (true)

{

Socket client = serverSocket.accept ();

System.out.println ("Accepted from " + client.getInetAddress ());

list_val = list_val + 1;

System.out.println("list_val is "+ list_val);

ServerThread st = new ServerThread(client);

st.start();

}

}

catch ( java.io.InterruptedIOException e )

{

System.out.println("InterruptedIOException Occured");

e.printStackTrace();

}

catch(Exception e)

{

System.out.println("error " + e);

}

}

public static void main(String args[])

{

NewServer server = new NewServer();

}

}

***************

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.net.Socket;

import java.util.Vector;

import java.io.*;

import java.util.*;

class ServerThread extends Thread {

protected Socket client;

protected static int key;

OutputStreamWriter outputstream = null;

BufferedReader br;

static String[] message_split;

ServerThread(Socket client) {

this.client = client;

try

{

outputstream = new OutputStreamWriter(client.getOutputStream());

br = new BufferedReader(new InputStreamReader(client.getInputStream()));

}

catch(Exception e5)

{

}

}

//protected static HashMap map = new HashMap();

protected static Vector handlers = new Vector ();

public void run() {

try

{

handlers.addElement (this);

//map.put(key,this);

while (true)

{

String msg = br.readLine() + "\n";

broadcast (msg);

}

}

catch (Exception e)

{

e.printStackTrace();

}

finally

{

handlers.removeElement(this);

try

{

client.close();

}

catch (Exception e2)

{

System.out.println("Oh no! " +

e2.toString());

}

}

}

protected static void broadcast (String message)

{

synchronized (handlers)

{

Enumeration e = handlers.elements ();

//while (e.hasMoreElements ())

//{

//ServerThread st = (ServerThread) e.nextElement ();

ServerThread st = (ServerThread) handlers.get(AppletClient.list.getSelectedIndex());

try

{

System.out.println("in try");

synchronized (st.outputstream)

{

System.out.println("in outputstream");

st.outputstream.write("Client : " + message);

}

st.outputstream.flush ();

}

catch (IOException ex)

{

}

//}

}

}// method broadcast ends

}// ServerThread ends

pls run the server...NewServer.java and open 3 or 4 instances of AppletClient....whenever the client selects any particular index from list...the message should go to that client

thanks in advance

suhasnda at 2007-7-9 20:41:09 > top of Java-index,Desktop,Core GUI APIs...