LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-14-2004, 05:37 PM   #1
randomx
Member
 
Registered: Feb 2003
Location: Hawaii
Distribution: Debian
Posts: 130

Rep: Reputation: 16
Question How can I output realtime system resources to a label or textarea? JAVA


How can I output realtime system resources into a label or textarea using JAVA?

by system resources I mean memory, swapping, cpu utilitization. You know, stuff that changes constantly by the second.

I would like to mirror exactly what's going on in realtime to a label or textarea.

any piece of code or idea is welcome.

thanks in advance.
randomx


pseudocode here

Code:
try {
        Runtime.getRuntime ().exec ( "/usr/bin/top" ) ;
      }
      catch ( IOException ex ) {
      }

  //realtime output to a string or file

 jLabel1.setText(getting all realtime inputs from top command);

Last edited by randomx; 07-14-2004 at 05:53 PM.
 
Old 07-14-2004, 06:01 PM   #2
csfalcon
Member
 
Registered: Jun 2004
Location: MD
Distribution: Fedora Core
Posts: 269

Rep: Reputation: 31
You want to implement some type of "polling", for example every 5 sec you check the value of the system resource and then update the GUI.

In java, there is a Timer class that you can set the timer and the action to take when the Timer goes off.



Code:
		// ActionListener is an interfact, so create a class that implements this interface here
		ActionListener recPoller = new ActionListener(); 
		Timer poller = new Timer(5000, recPoller);
		poller.start();

Code:
YourResourcePoller implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			//check for resource values

			//update your GUI
			textBox.setText(resource info);
		}
	}

Last edited by csfalcon; 07-14-2004 at 06:04 PM.
 
Old 07-15-2004, 02:59 PM   #3
randomx
Member
 
Registered: Feb 2003
Location: Hawaii
Distribution: Debian
Posts: 130

Original Poster
Rep: Reputation: 16
Question multiline label containing exact system command output

Thanks csfalcon. I implemented your idea of using a timer.

However, the new issue is related to multiline labels with the exact output of the command as you see it in console.

Background info: I'm triggering a system command to display memory utilization. Anyway, as you know, the "free" command displays many lines of text one after the other.


I want to have a label displaying the command output as it is: multiple lines. Just like you see it in the console. I want to mirror whatever comes up on console after executing free from java.

Using html tags won't work out for me in this particular situation. It just doesn't do the trick. Still displays the LAST line of command output *with or without* html tags.


any ideas? it doesn't have to be a label. It can be anything as long as it works.

Code:
  

//events will trigger every second

Timer t = new Timer(1000, new ActionListener(){
      public void actionPerformed(ActionEvent event)
      {

//determine memory utilization. 

        String cmd = "free -m";
    try {
     String line;
     Process p = Runtime.getRuntime().exec(cmd);
     BufferedReader input =
       new BufferedReader
         (new InputStreamReader(p.getInputStream()));
     while ((line = input.readLine()) != null) {
       label.setText(line);
       }
     input.close();
     }
    catch (Exception err) {
     err.printStackTrace();
     }
      }
    });
    t.start();
 
Old 07-15-2004, 03:18 PM   #4
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 53
Use a bufferedReader to read line by line from the /proc/meminfo pseudofile, parsing out the info you don't want.

The convenience of linux: everything is a file
 
Old 07-15-2004, 03:41 PM   #5
csfalcon
Member
 
Registered: Jun 2004
Location: MD
Distribution: Fedora Core
Posts: 269

Rep: Reputation: 31
Code:
while ((line = input.readLine()) != null) {
       label.setText(line);
       }
This part of your code will just keep updating the label with the new line of text and replace the previous line. You are want create a multiline string and then set the label.

Code:
String str = "";
       while ((line = input.readLine()) != null) {
       str += (line + "\n");
       }
       label.setText(str);
I am not sure if this is what you were asking about, hope it helps.
 
Old 07-15-2004, 04:01 PM   #6
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 53
Quote:
Originally posted by csfalcon
Code:
while ((line = input.readLine()) != null) {
       label.setText(line);
       }
This part of your code will just keep updating the label with the new line of text and replace the previous line. You are want create a multiline string and then set the label.

Code:
String str = "";
       while ((line = input.readLine()) != null) {
       str += (line + "\n");
       }
       label.setText(str);
I am not sure if this is what you were asking about, hope it helps.
If he replaces the label with a JTextArea he can just use the append(String str) method to make it multiline output.
 
Old 07-15-2004, 06:41 PM   #7
randomx
Member
 
Registered: Feb 2003
Location: Hawaii
Distribution: Debian
Posts: 130

Original Poster
Rep: Reputation: 16
Thumbs up

Hi everybody...thanks for all your inputs...

I mixed a little bit of everything and this is the final WORKING code.

I'm posting it here. Maybe somebody might needed in the future. It's all about helping each other right?.

Code:
String cmdMem = "free -m -t -o" ;
      try {
        String lineMem ;
        String outputMem = "" ;
        Process procMem = Runtime.getRuntime ().exec ( cmdMem ) ;
        BufferedReader input =
            new BufferedReader
            ( new InputStreamReader ( procMem.getInputStream () ) ) ;
        while ( ( lineMem = input.readLine () ) != null ) {
          outputMem = ( lineMem + "\n" ) ;
          jTextAreaSystemResourcesOutput.append ( outputMem ) ;

        }

        input.close () ;
      }
      catch ( Exception err ) {
        err.printStackTrace () ;
      }
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Optimal Java/C editor and learning resources jrdioko Programming 3 11-09-2005 09:22 AM
System Resources RySk8er30 Mandriva 2 06-23-2005 06:23 PM
java /proc (getting system resources) jinics Programming 1 08-26-2004 05:55 AM
System resources... manofwax Linux - Hardware 2 07-23-2004 08:34 PM
Nano using 90% system resources mooreted Linux - Software 2 11-27-2003 01:59 PM


All times are GMT -5. The time now is 04:45 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration