My Simple Reminder and Shut downer in Java

Java programming topics
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

My Simple Reminder and Shut downer in Java

Post by Nipuna » Sat May 26, 2012 7:58 pm

Hi

I made a simple reminder and automatic computer shut downer in Java about 2 months ago.

Because I always miss my meals and decide to code an app to prevent that. (But I am not using it much so there is no difference even tough i made it ;) )

I didn't post about this in here because the main code that does the core task of the app, I got it from a website because I couldn't figure it out. I tried with Java API but couldn't. That's why I didn't post my app here. Since I don't like to post a thing that is only half made by me. :( Is it OK to post half work of mine or isn't that a good thing ?

If you can fill me up with that then I can clear my mind about that.

Anyway back to topic. I used Netbens to build this but only used it as the code editor. This has a GUI but I code them all and the other codes all by myself, Only used Netbeans as the code efitor and compiler and runner because it's easy to use and has so many features and even point if I get any errors. Anyway, I didn't even use its code completion because If i do so it's not good because then I'll forget some codes.

Here is what this app does.

Remind the free defined times with messages. In my case.
Breakfast time, Lunch time and Dinner time.

Then shut down the computer.

The code I used that I couldn't find out (I mean the one I got from the internet) is,
The code that gets current system time and then prints it to a JLabel. That's the code I got from the net. And some other too it's the code that uses to shut down the computer.

Anyway, Here is the code. And I am attaching the .jar file too. (If you put this to your start up folder of your Windows computer then this app automatically get started every time you start up your computer.)
Sorry I couldn't add comments to the code because I didn't want to post because as I said half of the code is not mine :cry:

Code: Select all

import java.awt.*;  
import java.awt.event.*;  
import java.text.*;  
import java.util.Date;  
import javax.swing.*;  
   
public class R_n_S extends JFrame  
{  
    public R_n_S()  
    {  
        super("R_and_S");
        final JLabel timeLabel = new JLabel(); 
        final JLabel message = new JLabel("Program is running, Current time:");
        add(message, BorderLayout.CENTER);
        add(timeLabel, BorderLayout.LINE_END);
   
        final DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");  
        ActionListener timerListener = new ActionListener()  
        {  
            public void actionPerformed(ActionEvent e)  
            {  
                Date date = new Date();  
                String time = timeFormat.format(date);  
                timeLabel.setText(time); 
                if(time.equals("8:30:00")){
                    JOptionPane.showMessageDialog(null,"Breakfast Time "+time);
                    dispose();
                    try{
                        Runtime runtime = Runtime.getRuntime();
                        Process proc = runtime.exec("shutdown -s -t 30");
                        System.exit(0);
                    }
                    catch(Exception error){}

                }
                else if(time.equals("12:00:00")){
                    JOptionPane.showMessageDialog(null,"Lunch Time "+time);
                    dispose();
                    try{
                        Runtime runtime = Runtime.getRuntime();
                        Process proc = runtime.exec("shutdown -s -t 30");
                        System.exit(0);
                    }
                    catch(Exception error){}
                }
                else if(time.equals("20:30:00")){
                    JOptionPane.showMessageDialog(null,"Dinner Time "+time);
                    dispose();
                    try{
                        Runtime runtime = Runtime.getRuntime();
                        Process proc = runtime.exec("shutdown -s -t 30");
                        System.exit(0);
                    }
                    catch(Exception error){}
                }
            }  
        };  
        Timer timer = new Timer(1000, timerListener);  
        // to make sure it doesn't wait one second at the start  
        timer.setInitialDelay(0);  
        timer.start();  
    }  
   
    public static void main(String[] args) throws Exception  
    {  
        JFrame frame = new R_n_S();  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        frame.setSize(270,100);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);  
    }  
}
I hope you will enjoy this and correct me the things that I've done wrong. :)
Reminder and Shut downer.zip
(2.45 KiB) Downloaded 568 times

Thanks
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Re: My Simple Reminder and Shut downer in Java

Post by Herath » Sat May 26, 2012 10:39 pm

If you could point out the parts that you do not understand, we can try to discuss the thing in more details. :)
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: My Simple Reminder and Shut downer in Java

Post by Nipuna » Sun May 27, 2012 9:52 am

Thank you very much for tying to help me :)

Here is one piece of code that I don't get well.

Code: Select all

final DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");  
        ActionListener timerListener = new ActionListener()  
        {  
            public void actionPerformed(ActionEvent e)  
            {  
                Date date = new Date();  
                String time = timeFormat.format(date);  
                timeLabel.setText(time); 
                if(time.equals("8:30:00")){
                    JOptionPane.showMessageDialog(null,"Breakfast Time "+time);
                    dispose();
                    try{
                        Runtime runtime = Runtime.getRuntime();
                        Process proc = runtime.exec("shutdown -s -t 30");
                        System.exit(0);
                    }
                    catch(Exception error){}
In there what I don't get is how is this working. I understand what these 2 do.

Code: Select all

final DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");  
Creates a SimpleDateFormat object but what it does actually? I know it is used to get time but can you explain it in details please?

Code: Select all

        ActionListener timerListener = new ActionListener()  
I know this one so no need to explain this ;)

Actually in above code I mean the big one, I understand what it does but don't know how those objects get time etc.. Actually I understand most of it but don't know when we want to build a program how me must find suitable codes from the API that's the main problem :?

And the other one is this

Code: Select all

Timer timer = new Timer(1000, timerListener);  
        // to make sure it doesn't wait one second at the start 
        timer.setInitialDelay(0);  
        timer.start();

As I said above i have an idea how this works but not a clear idea :?

And another thing, If we get codes from the net and then we make apps isn't that a good thing? I mean because then are we considered as no skilled programers? :? It's a huge problem that is in my mind :(

Thank you for helping me :)
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Re: My Simple Reminder and Shut downer in Java

Post by Herath » Sun May 27, 2012 10:32 am

It is not totally possible to remeber everything about API of some platform unless you have worked with it for years. Always refer the API documentation when you do not know about a newly found class. In this case you can refer to SimpleDateFormat class javadoc located at http://docs.oracle.com/javase/1.4.2/doc ... ormat.html.

Code: Select all

final DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");  
The above statement create an SimpleDateFormat object, then the computer's current time is read with the following code,

Code: Select all

Date date = new Date();  
String time = timeFormat.format(date);  
with timeFormat.format() (timeFormat is an instance of SimpleDateFormat), the time is formatted to "HH:mm:ss" format. So if the current time is 1.30 PM the formatted time would be 13:30:00.

In the timer code, it is creating a new timer with 1000 miliseconds(1000miliseconds=1 second) interval. Every time the timer ticks, the code inside the ActionaListener is executed.

Before implementing any piece of application, it is better to have a design of how it is going to achieve things. In this case, you need to be able to know the current time, need to check time regularly and do something at a pre-defined time of the day.

And copying code is not a sin. It is OK as long as you understand it and not a unique algorithm so tht you won't have to face problems if it is not open source. However, copying a whole program without mentioning the original author is not good. And it is always a good habit to mention the sources if you copy/refer something. :)
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: My Simple Reminder and Shut downer in Java

Post by Nipuna » Sun May 27, 2012 11:12 am

Thanks friend.

I always refer API but I didn't understand methods that are related to DateFromat that's why I had to copy the code from the net.

Anyway thank you friend :)

Also can you suggest me some ideas to create programs ? So I can gain more knowledge. in these days I am not getting any ideas :?





Thanks
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: My Simple Reminder and Shut downer in Java

Post by Nandika » Tue May 29, 2012 12:53 pm

Hi Nipuna,
Your Remainder and Shut downer is very useful software. :biggrin: :biggrin: :biggrin:
I think,It must be develop. :idea:
  • Add user define times.
    Add password protection.(Parents can add a time and lock. :o )
    Automatically start with start up.
    ........
    ........
This is very useful software for me. :lol:
I have a problem while starting this software.Java give me an error like this. :cry:

Code: Select all

MissingFieldException[ The following required field is missing from the launch file: <jnlp>]
	at com.sun.javaws.jnl.XMLFormat.parse(Unknown Source)
	at com.sun.javaws.jnl.LaunchDescFactory.buildDescriptor(Unknown Source)
	at com.sun.javaws.jnl.LaunchDescFactory.buildDescriptor(Unknown Source)
	at com.sun.javaws.jnl.LaunchDescFactory.buildDescriptor(Unknown Source)
	at com.sun.javaws.Main.launchApp(Unknown Source)
	at com.sun.javaws.Main.continueInSecureThread(Unknown Source)
	at com.sun.javaws.Main$1.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Image
What is the reason for this?I used JRE7.I also have installed JDK 1.7.0_03.
Thank you.
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: My Simple Reminder and Shut downer in Java

Post by Nipuna » Tue May 29, 2012 1:02 pm

Thanks Nandika :)

I'll try to add the functions you stated later. :)

I made this using JDK 6, but I don't think that is a problem.

Did you try to build the app using the source I added?

Anyway, I'll search about this and let you know
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Re: My Simple Reminder and Shut downer in Java

Post by Herath » Tue May 29, 2012 1:04 pm

seems like a problem with the launcher.just try compiling the code in netbeans and exporting a jar file. it will work.

[ Post made via Mobile Device ] Image
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: My Simple Reminder and Shut downer in Java

Post by Nipuna » Tue May 29, 2012 1:10 pm

Herath wrote:seems like a problem with the launcher.just try compiling the code in netbeans and exporting a jar file. it will work.

[ Post made via Mobile Device ] Image
Yes that's how I made this .JAR file too. ;)

Try to build the app from the source code in Netbeans then do as Herath said.
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: My Simple Reminder and Shut downer in Java

Post by Nandika » Tue May 29, 2012 2:07 pm

I created a .jar file.it automatically created in %project folder%/dist/R_n_S.jar.but,didn't work when i Launch it.But,in NetbeansIDE it work. :)
Working.PNG
Working.PNG (15.32 KiB) Viewed 13511 times
I also did this,I think,it changes JRE version.but,not result. :(
err.PNG
err.PNG (38.53 KiB) Viewed 13511 times
Post Reply

Return to “Java Programming”