Link to home
Start Free TrialLog in
Avatar of Murdoc
Murdoc

asked on

Thread problem

Hi

I'm trying to develope simple application with threads.
The problem is that i cannot in thread add line to table model
:(((  (message: "cannot resolve symbol listmodel " " appears all the time )
'cause i'm newbie and because earlier i was using delphi which is a bit more friendly (but i have finished with delphi)

Here is my source code:
------------------------------------------------------------------------------------------

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

public class watek {
   

public static JFrame w = new JFrame();
public static JPanel jp = new JPanel();  
public static JPanel jpa = new JPanel();      
public static JPanel jpan = new JPanel();      

public static DefaultListModel listModel = new DefaultListModel(); ;
public static JList list = new JList(listModel);




public static JRadioButton jrb = new JRadioButton();
public static JRadioButton jrb1 = new JRadioButton();
public static JLabel jl = new JLabel();
public static JLabel jl1 = new JLabel();
public static JTextField jtf = new JTextField();
public static JSpinner js = new JSpinner();

public static JButton jb = new JButton("Connect");
public static JTextField tf = new JTextField();
public static JButton jb1 = new JButton("Disconnect");



  public static void main(String args[])
  {
     //listModel.addElement(in.readLine());
     
      jb.addActionListener(new ActionListener()
         {
                       public void actionPerformed(ActionEvent event)
              {
                 
     // listModel = new DefaultListModel();
                
      myThread newThread = new myThread("bialystok.ircnet.pl","6667");
      Thread thisThread = new Thread ( newThread );
      thisThread.start();
                 
                       }
                       
      });


w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = w.getContentPane();

//jp.setBackground(Color.blue);
//jpa.setBackground(Color.red);
//jpan.setBackground(Color.yellow);

js.setPreferredSize(new java.awt.Dimension(55, 18));
list.setPreferredSize(new java.awt.Dimension(200,100));
jtf.setPreferredSize(new java.awt.Dimension(65,18));

jrb.setText("UDP");
jrb1.setText("TCP");
jl.setText("Host:");
jl1.setText("Port:");

jp.setLayout(new BoxLayout(jp,BoxLayout.LINE_AXIS));
jp.add(jrb);
jp.add(Box.createHorizontalStrut(5));
jp.add(new JSeparator(SwingConstants.VERTICAL));
jp.add(Box.createHorizontalStrut(5));
jp.add(jrb1);
jp.add(Box.createHorizontalStrut(5));
jp.add(new JSeparator(SwingConstants.VERTICAL));
jp.add(Box.createHorizontalStrut(5));
jp.add(jl);
jp.add(Box.createHorizontalStrut(5));
jp.add(new JSeparator(SwingConstants.VERTICAL));
jp.add(Box.createHorizontalStrut(5));
jp.add(jtf);
jp.add(Box.createHorizontalStrut(5));
jp.add(new JSeparator(SwingConstants.VERTICAL));
jp.add(Box.createHorizontalStrut(5));
jp.add(jl1);
jp.add(Box.createHorizontalStrut(5));
jp.add(new JSeparator(SwingConstants.VERTICAL));
jp.add(Box.createHorizontalStrut(5));
jp.add(js);


jpa.setLayout(new BoxLayout(jpa,BoxLayout.LINE_AXIS));
jpa.add(jb);
jpa.add(Box.createHorizontalStrut(5));
jpa.add(new JSeparator(SwingConstants.VERTICAL));
jpa.add(Box.createHorizontalStrut(5));
jpa.add(tf);
jpa.add(Box.createHorizontalStrut(5));
jpa.add(new JSeparator(SwingConstants.VERTICAL));
jpa.add(Box.createHorizontalStrut(5));
jpa.add(jb1);


w.getContentPane().add(jp,BorderLayout.PAGE_START);

w.getContentPane().add(list,BorderLayout.CENTER);

w.getContentPane().add(jpa,BorderLayout.PAGE_END);



//setSize(600,100);

w.pack();
w.setVisible(true);

  }
}







class myThread implements Runnable {
  private String leport,lehost;
  public myThread (String host, String port)
  {
  lehost=host;
  leport=port;
  }

  public void run() {
    try {
         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
         Socket kkSocket = null;    
         PrintWriter out = null;    
         BufferedReader in = null;
        kkSocket = new Socket(lehost,Integer.valueOf(leport.toString()).intValue());
        out = new PrintWriter(kkSocket.getOutputStream(), true); //
 in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
       BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
      String fromServer, fromUser;
      out.println("NICK jobob");

          while ((fromServer = in.readLine()) != null)
          {
          boolean lpuh=fromServer.equals("NOTICE AUTH :*** Looking up your hostname...");
          if (lpuh==true)    
          {
          out.println("USER Murdoc localhost localhost :Dont bother");    
          System.out.println("System: NOTICE AUTH :*** Looking up your hostname...");
        listModel.addElement(in.readLine());
          }
          boolean nir=fromServer.equals("NOTICE AUTH :*** No Ident response");
                   if (nir==true)    
          {
          out.println("USERHOST jobob");
          out.println("MODE jobob +i");
          System.out.println("System: NOTICE AUTH :*** No Ident response");    
          }
            //listModel.addElement(in.readLine());
         
       
            System.out.println("Server: " + fromServer);  

       
            if (fromServer.equals("exit") || fromServer.equals("quit"))
             {
               System.out.println("kicked from server");
               break;
             }
         // a=a+1;  
          }

        out.close();      
        in.close();
        stdIn.close();
        kkSocket.close();


        } catch (Exception e) {
     
    }
  }
}
-------------------------------------------------------------------------------------------

I'd like to allow display text in listmodel which is inside the thread but cannot.....
 any ideas ??

P.S. If you could show the easiest way how to correct listning 'cause it's very difficult for me to change everything ....

Regards
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

listmodel is probably not visible in the scope of the area of code where it appears. Can you check that?
It compiles now, but whether it is what you want to do or not ... ?


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

public class watek {
 

public static JFrame w = new JFrame();
public static JPanel jp = new JPanel();  
public static JPanel jpa = new JPanel();    
public static JPanel jpan = new JPanel();    

public static DefaultListModel listModel = new DefaultListModel(); ;
public static JList list = new JList(listModel);




public static JRadioButton jrb = new JRadioButton();
public static JRadioButton jrb1 = new JRadioButton();
public static JLabel jl = new JLabel();
public static JLabel jl1 = new JLabel();
public static JTextField jtf = new JTextField();
public static JSpinner js = new JSpinner();

public static JButton jb = new JButton("Connect");
public static JTextField tf = new JTextField();
public static JButton jb1 = new JButton("Disconnect");

static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));



 public static void main(String args[])
 {
    //listModel.addElement(input.readLine());
     
      jb.addActionListener(new ActionListener()
        {
                      public void actionPerformed(ActionEvent event)
             {
                 
    // listModel = new DefaultListModel();

    listModel = new DefaultListModel();
try{
    listModel.addElement(input.readLine());
}catch(IOException ioe){}
             
      myThread newThread = new myThread("bialystok.ircnet.pl","6667");
     Thread thisThread = new Thread ( newThread );
     thisThread.start();
                 
                      }
                     
      });


w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = w.getContentPane();

//jp.setBackground(Color.blue);
//jpa.setBackground(Color.red);
//jpan.setBackground(Color.yellow);

js.setPreferredSize(new java.awt.Dimension(55, 18));
list.setPreferredSize(new java.awt.Dimension(200,100));
jtf.setPreferredSize(new java.awt.Dimension(65,18));

jrb.setText("UDP");
jrb1.setText("TCP");
jl.setText("Host:");
jl1.setText("Port:");

jp.setLayout(new BoxLayout(jp,BoxLayout.LINE_AXIS));
jp.add(jrb);
jp.add(Box.createHorizontalStrut(5));
jp.add(new JSeparator(SwingConstants.VERTICAL));
jp.add(Box.createHorizontalStrut(5));
jp.add(jrb1);
jp.add(Box.createHorizontalStrut(5));
jp.add(new JSeparator(SwingConstants.VERTICAL));
jp.add(Box.createHorizontalStrut(5));
jp.add(jl);
jp.add(Box.createHorizontalStrut(5));
jp.add(new JSeparator(SwingConstants.VERTICAL));
jp.add(Box.createHorizontalStrut(5));
jp.add(jtf);
jp.add(Box.createHorizontalStrut(5));
jp.add(new JSeparator(SwingConstants.VERTICAL));
jp.add(Box.createHorizontalStrut(5));
jp.add(jl1);
jp.add(Box.createHorizontalStrut(5));
jp.add(new JSeparator(SwingConstants.VERTICAL));
jp.add(Box.createHorizontalStrut(5));
jp.add(js);


jpa.setLayout(new BoxLayout(jpa,BoxLayout.LINE_AXIS));
jpa.add(jb);
jpa.add(Box.createHorizontalStrut(5));
jpa.add(new JSeparator(SwingConstants.VERTICAL));
jpa.add(Box.createHorizontalStrut(5));
jpa.add(tf);
jpa.add(Box.createHorizontalStrut(5));
jpa.add(new JSeparator(SwingConstants.VERTICAL));
jpa.add(Box.createHorizontalStrut(5));
jpa.add(jb1);


w.getContentPane().add(jp,BorderLayout.PAGE_START);

w.getContentPane().add(list,BorderLayout.CENTER);

w.getContentPane().add(jpa,BorderLayout.PAGE_END);



//setSize(600,100);

w.pack();
w.setVisible(true);

 }
//}







static class myThread implements Runnable {
 private String leport,lehost;
 public myThread (String host, String port)
 {
 lehost=host;
 leport=port;
 }

 public void run() {
   try {
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        Socket kkSocket = null;    
        PrintWriter out = null;    
        BufferedReader in = null;
       kkSocket = new Socket(lehost,Integer.valueOf(leport.toString()).intValue());
       out = new PrintWriter(kkSocket.getOutputStream(), true); //
 in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
      BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
     String fromServer, fromUser;
    out.println("NICK jobob");

         while ((fromServer = in.readLine()) != null)
          {
         boolean lpuh=fromServer.equals("NOTICE AUTH :*** Looking up your hostname...");
         if (lpuh==true)    
          {
         out.println("USER Murdoc localhost localhost :Dont bother");    
         System.out.println("System: NOTICE AUTH :*** Looking up your hostname...");
       listModel.addElement(in.readLine());
          }
         boolean nir=fromServer.equals("NOTICE AUTH :*** No Ident response");
                  if (nir==true)    
          {
         out.println("USERHOST jobob");
         out.println("MODE jobob +i");
          System.out.println("System: NOTICE AUTH :*** No Ident response");    
          }
         //listModel.addElement(in.readLine());
         
     
            System.out.println("Server: " + fromServer);  

     
            if (fromServer.equals("exit") || fromServer.equals("quit"))
             {
              System.out.println("kicked from server");
              break;
            }
        // a=a+1;  
         }

       out.close();      
        in.close();
       stdIn.close();
       kkSocket.close();


       } catch (Exception e) {
     
   }
 }
}
}
Avatar of Murdoc
Murdoc

ASKER

it's not adding to table any text :((

you should connect to bialystok.ircnet.pl on port 6667 ..
Avatar of Murdoc

ASKER

form freezes :((
I'm not sure what this code does, but comment it out, as it's blocking your thread:

>>
                              listModel = new DefaultListModel();
                              try {
                                    listModel.addElement(input.readLine());
                              }
                              catch (IOException ioe) {}
>>
Avatar of Murdoc

ASKER

This program is connecting to irc server ..and in my code above if you will compile(without listModel.addElement(in.readLine());)  it and run from msdos or console you will get all messages that server sends to you
All i want is to display this messages in Table

Regards
That was the reason for the problem you were having.

>>All i want is to display this messages in Table

It would be much better to display them in a text component
Avatar of Murdoc

ASKER

the problem is that "cannot resolve symbol listmodel " , propably there would be the same problem with text component

regards
So like CEHJ said, remove that bit, compile this code, and you receive your messages.


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

public class watek {
 

public static JFrame w = new JFrame();
public static JPanel jp = new JPanel();  
public static JPanel jpa = new JPanel();    
public static JPanel jpan = new JPanel();    

//public static DefaultListModel listModel = new DefaultListModel(); ;
//public static JList list = new JList(listModel);




public static JRadioButton jrb = new JRadioButton();
public static JRadioButton jrb1 = new JRadioButton();
public static JLabel jl = new JLabel();
public static JLabel jl1 = new JLabel();
public static JTextField jtf = new JTextField();
public static JSpinner js = new JSpinner();

public static JButton jb = new JButton("Connect");
public static JTextField tf = new JTextField();
public static JButton jb1 = new JButton("Disconnect");

static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));



 public static void main(String args[])
 {
    //listModel.addElement(input.readLine());
     
      jb.addActionListener(new ActionListener()
        {
                      public void actionPerformed(ActionEvent event)
             {
                 
    // listModel = new DefaultListModel();

  //  listModel = new DefaultListModel();
//try{
  //  listModel.addElement(input.readLine());
//}catch(IOException ioe){}
             
     myThread newThread = new myThread("bialystok.ircnet.pl","6667");
     Thread thisThread = new Thread ( newThread );
     thisThread.start();
                 
                      }
                     
      });


w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = w.getContentPane();

//jp.setBackground(Color.blue);
//jpa.setBackground(Color.red);
//jpan.setBackground(Color.yellow);

js.setPreferredSize(new java.awt.Dimension(55, 18));
//list.setPreferredSize(new java.awt.Dimension(200,100));
jtf.setPreferredSize(new java.awt.Dimension(65,18));

jrb.setText("UDP");
jrb1.setText("TCP");
jl.setText("Host:");
jl1.setText("Port:");

jp.setLayout(new BoxLayout(jp,BoxLayout.LINE_AXIS));
jp.add(jrb);
jp.add(Box.createHorizontalStrut(5));
jp.add(new JSeparator(SwingConstants.VERTICAL));
jp.add(Box.createHorizontalStrut(5));
jp.add(jrb1);
jp.add(Box.createHorizontalStrut(5));
jp.add(new JSeparator(SwingConstants.VERTICAL));
jp.add(Box.createHorizontalStrut(5));
jp.add(jl);
jp.add(Box.createHorizontalStrut(5));
jp.add(new JSeparator(SwingConstants.VERTICAL));
jp.add(Box.createHorizontalStrut(5));
jp.add(jtf);
jp.add(Box.createHorizontalStrut(5));
jp.add(new JSeparator(SwingConstants.VERTICAL));
jp.add(Box.createHorizontalStrut(5));
jp.add(jl1);
jp.add(Box.createHorizontalStrut(5));
jp.add(new JSeparator(SwingConstants.VERTICAL));
jp.add(Box.createHorizontalStrut(5));
jp.add(js);


jpa.setLayout(new BoxLayout(jpa,BoxLayout.LINE_AXIS));
jpa.add(jb);
jpa.add(Box.createHorizontalStrut(5));
jpa.add(new JSeparator(SwingConstants.VERTICAL));
jpa.add(Box.createHorizontalStrut(5));
jpa.add(tf);
jpa.add(Box.createHorizontalStrut(5));
jpa.add(new JSeparator(SwingConstants.VERTICAL));
jpa.add(Box.createHorizontalStrut(5));
jpa.add(jb1);


w.getContentPane().add(jp,BorderLayout.PAGE_START);

//w.getContentPane().add(list,BorderLayout.CENTER);

w.getContentPane().add(jpa,BorderLayout.PAGE_END);



//setSize(600,100);

w.pack();
w.setVisible(true);

 }
//}







static class myThread implements Runnable {
 private String leport,lehost;
 public myThread (String host, String port)
 {
 lehost=host;
 leport=port;
 }

 public void run() {
   try {
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        Socket kkSocket = null;    
        PrintWriter out = null;    
        BufferedReader in = null;
       kkSocket = new Socket(lehost,Integer.valueOf(leport.toString()).intValue());
       out = new PrintWriter(kkSocket.getOutputStream(), true); //
 in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
      BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
     String fromServer, fromUser;
    out.println("NICK jobob");

         while ((fromServer = in.readLine()) != null)
          {
         boolean lpuh=fromServer.equals("NOTICE AUTH :*** Looking up your hostname...");
         if (lpuh==true)    
          {
         out.println("USER Murdoc localhost localhost :Dont bother");    
         System.out.println("System: NOTICE AUTH :*** Looking up your hostname...");
  //     listModel.addElement(in.readLine());
          }
         boolean nir=fromServer.equals("NOTICE AUTH :*** No Ident response");
                  if (nir==true)    
          {
         out.println("USERHOST jobob");
         out.println("MODE jobob +i");
          System.out.println("System: NOTICE AUTH :*** No Ident response");    
          }
    //     listModel.addElement(in.readLine());
         
     
            System.out.println("Server: " + fromServer);  

     
            if (fromServer.equals("exit") || fromServer.equals("quit"))
             {
              System.out.println("kicked from server");
              break;
            }
        // a=a+1;  
         }

       out.close();      
        in.close();
       stdIn.close();
       kkSocket.close();


       } catch (Exception e) {
     
   }
 }
}
}
Avatar of Murdoc

ASKER

still doesen't display in JTable............................:(((
>>still doesen't display in JTable............................:(((

Which JTable, there isn't even one in the code you posted?
Avatar of Murdoc

ASKER

sorry..my fault JList of course .....
Avatar of Murdoc

ASKER

I'm thinking about JList but I'm typing JTable.....in my question is full source code
Regards
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Murdoc

ASKER

ooops ...you get the points
Avatar of Murdoc

ASKER

btw. Could you Cehj send me your source code on my address (wthouting ziping , etc):   photo_delphi@wp.pl
Make sure you're using the same code as i:

http://www.cehjohnson.uklinux.net/aire/watek.java

Also, (nearly?) all of those public static variables should be private and not static
Don't post your address - you'll get spammed like mad ;-) If you're bothered, ask Community Support to delete that
8-)
Avatar of Murdoc

ASKER

One of my mailbox is so spammed so I'm not even trying to retrieve any of the  message , besides this mailbox I posted has some kind of spam protection
Avatar of Murdoc

ASKER

Anywat thanks for help

Regards