Solved

Thread problem

Posted on 2004-09-26
21
278 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:Murdoc
  • 11
  • 7
  • 3
21 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 12154127
listmodel is probably not visible in the scope of the area of code where it appears. Can you check that?
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 12154177
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
 

Author Comment

by:Murdoc
ID: 12154196
it's not adding to table any text :((

you should connect to bialystok.ircnet.pl on port 6667 ..
0
 

Author Comment

by:Murdoc
ID: 12154199
form freezes :((
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12154240
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
 

Author Comment

by:Murdoc
ID: 12154260
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12154272
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
 

Author Comment

by:Murdoc
ID: 12154286
the problem is that "cannot resolve symbol listmodel " , propably there would be the same problem with text component

regards
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 12155214
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
 

Author Comment

by:Murdoc
ID: 12162878
still doesen't display in JTable............................:(((
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:CEHJ
ID: 12163691
>>still doesen't display in JTable............................:(((

Which JTable, there isn't even one in the code you posted?
0
 

Author Comment

by:Murdoc
ID: 12163861
sorry..my fault JList of course .....
0
 

Author Comment

by:Murdoc
ID: 12163881
I'm thinking about JList but I'm typing JTable.....in my question is full source code
Regards
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 180 total points
ID: 12164215
The code i fixed works fine for me (although i changed the address):

http://www.cehjohnson.uklinux.net/aire/irc.png
0
 

Author Comment

by:Murdoc
ID: 12164339
ooops ...you get the points
0
 

Author Comment

by:Murdoc
ID: 12164362
btw. Could you Cehj send me your source code on my address (wthouting ziping , etc):   photo_delphi@wp.pl
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12164375
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12164395
Don't post your address - you'll get spammed like mad ;-) If you're bothered, ask Community Support to delete that
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12164398
8-)
0
 

Author Comment

by:Murdoc
ID: 12164443
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
 

Author Comment

by:Murdoc
ID: 12164470
Anywat thanks for help

Regards

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
advertisement module in core php 4 144
Java / Linux and Regular Expressions 11 69
Java SE 8u111  Lot of stuff broke 11 54
servlet  URL Rewriting 1 26
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now