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
MurdocAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
The code i fixed works fine for me (although i changed the address):

http://www.cehjohnson.uklinux.net/aire/irc.png
0
 
krakatoaCommented:
listmodel is probably not visible in the scope of the area of code where it appears. Can you check that?
0
 
krakatoaCommented:
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) {
     
   }
 }
}
}
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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

you should connect to bialystok.ircnet.pl on port 6667 ..
0
 
MurdocAuthor Commented:
form freezes :((
0
 
CEHJCommented:
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) {}
>>
0
 
MurdocAuthor Commented:
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
0
 
CEHJCommented:
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
0
 
MurdocAuthor Commented:
the problem is that "cannot resolve symbol listmodel " , propably there would be the same problem with text component

regards
0
 
krakatoaCommented:
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) {
     
   }
 }
}
}
0
 
MurdocAuthor Commented:
still doesen't display in JTable............................:(((
0
 
CEHJCommented:
>>still doesen't display in JTable............................:(((

Which JTable, there isn't even one in the code you posted?
0
 
MurdocAuthor Commented:
sorry..my fault JList of course .....
0
 
MurdocAuthor Commented:
I'm thinking about JList but I'm typing JTable.....in my question is full source code
Regards
0
 
MurdocAuthor Commented:
ooops ...you get the points
0
 
MurdocAuthor Commented:
btw. Could you Cehj send me your source code on my address (wthouting ziping , etc):   photo_delphi@wp.pl
0
 
CEHJCommented:
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
0
 
CEHJCommented:
Don't post your address - you'll get spammed like mad ;-) If you're bothered, ask Community Support to delete that
0
 
CEHJCommented:
8-)
0
 
MurdocAuthor Commented:
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
0
 
MurdocAuthor Commented:
Anywat thanks for help

Regards

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.