This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What is wrong with this code????  RSS feed

 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class LogPanel that reads content of a file and prints it to a JTextArea. The class extends JPanel and implements Runnable so it can be run as a thread and added as a gui component.
I also wrote a class that creates a frame and added the LogPanel to it. the frame has windoweventlistener, so it can be closed.
If user closes I run stopPanel method. I know it exits the run method, but for some reason it does not stop the thread.
Why??

Thanks a lot. Yarik
I have added [ code] [ /code] UBB codes to your code. Hope you don't mind
[This message has been edited by Rahul Mahindrakar (edited January 15, 2001).]
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The problem lies in the above loop. The while loop is not entered again till the whole file is read through the if loop. As such the thread does not stop till it has read the whole file. A solution would be to set the Thread as a daemon thread, which would exit as soon as the main thread that creates it no longer exits.
Another problem with your code i think is that it will continuously read the file till the stop is true. This problem must also be addressed by you.
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank for the reply.
I thought about it. But it is not the case. The file I tested with had 2 lines. also I inserted System.out.println("STOPPED") before the end of the while and before the end of the run().
Both of them were outputed. But the thread does not stop.
???
Thanks, Yarik.
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one more thing.
The code is working in the same way as "tail -f" in Unix.
It continuously read the file, and outputs its content to the text area. Every time new record is appended to the file, it will be shown on the text area as well.

Yarik.
 
sunil choudhary
Ranch Hand
Posts: 144
Java Redhat Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Yaroslav,I tried to run your code but there was no main and hence I inserted one but with errors.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class Papanel
{
public static void main(String[]args)
{JFrame frame=new LogPanel();
frame.show();
}
}

public class LogPanel extends JPanel implements Runnable
{
--
......
however it is not working can you point out what am I doing wrong.
I know your post is anwered but still I want to get waht you were asking and a working code at that will help ...a lot
Sunil
 
Jerry Pulley
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yaroslav,
I wrote a main class to use your panel, and it seems to work ok. I put a quick test for the log panel thread's aliveness in <code>windowClosing()</code>; it shows that the thread does indeed die. Can you be more explicit about the symptoms you're seeing?<pre>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class LogTest {
LogPanel logPanel;
JFrame f;
JButton stopBtn;
Thread logThread;
public LogTest() {
try {
logPanel = new LogPanel( "test.txt" );
} catch (Exception e) {
e.printStackTrace();
System.exit( 0 );
}
f = new JFrame();
f.getContentPane().setLayout( new BorderLayout() );
f.getContentPane().add( logPanel,
BorderLayout.CENTER );
stopBtn = new JButton( "STOP" );
stopBtn.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
logPanel.stopPanel();
}
});
f.getContentPane().add( stopBtn, BorderLayout.SOUTH );
logThread = new Thread( logPanel );
f.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
logPanel.stopPanel();
try {
Thread.currentThread().sleep( 100 );
} catch (InterruptedException ie) {}
System.out.println( "logThread.isAlive() = " + logThread.isAlive() );
System.exit( 0 );
}
});
f.setSize( 300, 200 );
f.setVisible( true );
logThread.start();
}
public static void main( String[] args ) {
LogTest me = new LogTest();
}
}</pre>
Jerry
P.S. If you place your code inside < pre> and < /pre> tags we'll be able to read it easier. Don't include the spaces after the opening angle brackets; I just put them there so the tags wouldn't be interpreted as HTML in this message.

[This message has been edited by Jerry Pulley (edited January 12, 2001).]
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all
Thank you for helping me.
The following is my main()

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import remotelogger.*; //includes logpanel
public class LogMonitor
{
public static void main(String[] args)
{
String USAGE = new String("Usage: LogMonitor [log file]");
if ( args.length < 1 )<br /> {<br /> System.err.println(USAGE);<br /> System.exit(1);<br /> }<br /> String file = new String(args[0]);<br /> try<br /> {<br /> final LogPanel lp = new LogPanel(file);<br /> final JFrame frame = new JFrame("Monitoring Tools Log (" + file + ")");<br /> Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();<br /> Dimension frameSize = frame.getSize();<br /> frame.setSize(480,240);<br /> if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 4,
(screenSize.height - frameSize.height) / 4);
frame.setVisible(true);
frame.getContentPane().add(lp);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
lp.stopPanel(); //use logpanel's STOP to stop the thread
frame.dispose();
}
});
Thread th = new Thread(lp);
th.setDaemon(true); // th will be killed as soon as main() is not alive
th.start();
frame.setVisible(true);
}catch(Exception e)
{
new ErrorHandler(e); // my error handler
System.exit(1);
}
}
}

The problem is:
After I close the frame, thread (logpanel) does not stop.

Thank you.
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sunil,
The LogPanel extends JPanel, so you may want to create a frame,
and add the panel as a component.
Thank you.
 
Jerry Pulley
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yaroslav,
The problem is in your <code>windowClosing()</code> method. <code>dispose</code> doesn't destroy the frame - it just releases any native screen resources it's tied to. Check out the API description for more info. To end the program at that point, the usual practice is to call <code>System.exit( 0 )</code>.
To see that the log panel thread does indeed die, let's run your program under jdb. First, give the log panel thread a name so you can easily recognize it in the debugger. Start it with <code>%jdb LogMonitor test.txt</code> or whatever works for your packages and test file. Once the debugger has initialized and you have the prompt, type <code>run</code> to start the program. Once it's up and running, type <code>threads</code> to see all the threads in the system. You'll see the log panel thread in the main group. Click the close button on the frame, and the frame vanishes but the program is still running. And so, at this point, is the log panel thread. Wait a few seconds though (because you've got a 10-second <code>wait</code> in <code>LogPanel.run()</code>) and list the threads again. You'll see that the log panel thread is gone, but the main thread (called Thread-1 on my system) is still up and running.
If you edit the monitor class to use <code>System.exit( 0 )</code> instead of <code>dispose</code> the program will shut down as soon as you close the frame. (It's not necessary to make the log panel thread a daemon, but it doesn't hurt anything.)
Jerry
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jerry,
Thank you very much for help.
I will try to use System.exit(0).
I did not use because I had a problem with it.
I had multithreaded app. If one of the threads failed on the certain task I was stoping it with System.exit(0) but it stopped the whole app.
But I guess in this case I do not care. since the panel is totally independed from other components.
Thanks, Yarik.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!