Welcome to Dream.In.Code
Become a Java Expert!

Join 150,189 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,033 people online right now. Registration is fast and FREE... Join Now!




Java Clock GUI Question

 
Reply to this topicStart new topic

Java Clock GUI Question, Java Clock GUI Question

Maverick519
24 Sep, 2008 - 10:18 AM
Post #1

New D.I.C Head
*

Joined: 30 Nov, 2007
Posts: 34


My Contributions
Ok im making a program that gets the time and im trying to put it into a applet and display it. I know it will print it out and it works. But i cant get the applet to work.

CODE

CODE

package clock;

import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

public class NewJPanel extends javax.swing.JPanel
{    
    private Date now;
    private String time;
    
    public NewJPanel()
    {
        initComponents();
        try
        {
            while(true)
            {
                initDate();
                int h = now.getHours();
                int m = now.getMinutes();
                int s = now.getSeconds();
                Thread.sleep(1000);
                if (h > 12)
                {
                    h -= 12;
                }
                if (m < 10)
                {
                    String minFirst = Integer.toString(m);
                    String minSecond = "0" + minFirst;
                    time = h + ":" + minSecond + ":" + s;
                }
                if (m > 10)
                {    
                    time = h + ":" + m + ":" + s;
                }
                jLabel1.setText(time);
                System.out.println(time);
            }
        }
        catch (InterruptedException ex)
        {
            Logger.getLogger(NewJPanel.class.getName()).log(Level.SEVERE, null, ex);
        }    
    }

    private void initDate()
    {
        now = new Date();
    }


I have my main

CODE

package clock;

import java.awt.Dimension;
import javax.swing.JApplet;
import javax.swing.JFrame;

public class Start extends JApplet
{
    public static void main (String args[])
    {
        JApplet ja = new JApplet();
        NewJPanel jp = new NewJPanel();
        jp.setVisible(true);
        ja.add(jp);
    }
}







package clock;

import java.awt.Dimension;
import javax.swing.JApplet;
import javax.swing.JFrame;

public class Start extends JApplet
{
    public static void main (String args[])
    {
       JFrame f = new JFrame("Clock");
       NewJPanel p = new NewJPanel();
       f.setPreferredSize(new Dimension(500, 500));
       f.add(p);
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       f.pack();
       f.setVisible(true);
    }
}




I can also try it in a JFram but that does not pop up in a label.


So how can i get the time into a label on a Panel Frame Or Applet?

This post has been edited by Maverick519: 24 Sep, 2008 - 10:19 AM
User is offlineProfile CardPM
+Quote Post

Locke37
RE: Java Clock GUI Question
24 Sep, 2008 - 10:22 AM
Post #2

Contributor of the Year
Group Icon

Joined: 20 Mar, 2008
Posts: 1,274



Thanked: 57 times
Dream Kudos: 325
My Contributions
I make HTML files that run my applets, then load them into my browser. It may take me a couple tries to get this exactly right, I haven't done it for a while.

HTML FILE
CODE
<applet code="Start.class" width=600 height=800></applet>


Then open your internet browser, select File -> Open, navigate to your file and open it, and I hope that will work.

I know that making HTML files DO work, it's just that I might be a bit rusty. unsure.gif

Hope all goes well! biggrin.gif

This post has been edited by Locke37: 24 Sep, 2008 - 10:22 AM
User is offlineProfile CardPM
+Quote Post

Maverick519
RE: Java Clock GUI Question
24 Sep, 2008 - 10:25 AM
Post #3

New D.I.C Head
*

Joined: 30 Nov, 2007
Posts: 34


My Contributions
QUOTE(Locke37 @ 24 Sep, 2008 - 11:22 AM) *

I make HTML files that run my applets, then load them into my browser. It may take me a couple tries to get this exactly right, I haven't done it for a while.

HTML FILE
CODE
<applet code="Start.class" width=600 height=800></applet>


Then open your internet browser, select File -> Open, navigate to your file and open it, and I hope that will work.

I know that making HTML files DO work, it's just that I might be a bit rusty. unsure.gif

Hope all goes well! biggrin.gif



where would i put that in my code? idk how to do that. Do i even need a main?
User is offlineProfile CardPM
+Quote Post

Locke37
RE: Java Clock GUI Question
24 Sep, 2008 - 10:31 AM
Post #4

Contributor of the Year
Group Icon

Joined: 20 Mar, 2008
Posts: 1,274



Thanked: 57 times
Dream Kudos: 325
My Contributions
That code would have gone in an HTML file that you create only to load the project.

Oh wait, on further examination...I have a solution.

In your Start class...

java
public class Start extends JApplet
{
public void init() // this is the main method of applets
{
// you DO NOT need to create an instance of a JApplet here
// since your class extends it.

NewJPanel jp = NewJPanel();
add(jp);
jp.setVisible(true);
}
}


Then create an HTML file, using some text editor, doesn't even matter. Then type that code I gave you before in that HTML file, then load it into your browser.

This post has been edited by Locke37: 24 Sep, 2008 - 10:32 AM
User is offlineProfile CardPM
+Quote Post

Maverick519
RE: Java Clock GUI Question
24 Sep, 2008 - 10:40 AM
Post #5

New D.I.C Head
*

Joined: 30 Nov, 2007
Posts: 34


My Contributions
It sais it failed to load.
User is offlineProfile CardPM
+Quote Post

Locke37
RE: Java Clock GUI Question
24 Sep, 2008 - 10:47 AM
Post #6

Contributor of the Year
Group Icon

Joined: 20 Mar, 2008
Posts: 1,274



Thanked: 57 times
Dream Kudos: 325
My Contributions
I was afraid of that...gimme one minute. smile.gif

Ok, new theory. You need to import one more package, the Graphics class.

java
// new imported package
import java.awt.Graphics;

// replace the init() with this.

public void paint(Graphics g)
{
...
}


And make sure to re-compile it, or your .class file will be outdated! (I did this many a-time in my java class...unsure.gif)

This post has been edited by Locke37: 24 Sep, 2008 - 10:52 AM
User is offlineProfile CardPM
+Quote Post

Maverick519
RE: Java Clock GUI Question
24 Sep, 2008 - 11:01 AM
Post #7

New D.I.C Head
*

Joined: 30 Nov, 2007
Posts: 34


My Contributions
Maybe im doing something wrong. But it wont load. But i use netbeans and it wont even display the time in the applet there. Or if i use a frame it wont work.
User is offlineProfile CardPM
+Quote Post

Maverick519
RE: Java Clock GUI Question
24 Sep, 2008 - 11:11 AM
Post #8

New D.I.C Head
*

Joined: 30 Nov, 2007
Posts: 34


My Contributions
idk if netbeans makes a .class file i cant find it.
User is offlineProfile CardPM
+Quote Post

Maverick519
RE: Java Clock GUI Question
24 Sep, 2008 - 11:51 AM
Post #9

New D.I.C Head
*

Joined: 30 Nov, 2007
Posts: 34


My Contributions
Ok it loaded it fine. But my program does not work. Like it doesn't display the time in the label? I dont get it. Heres my code again

CODE

package clock;

import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

public class NewJPanel extends javax.swing.JPanel
{    
    private Date now;
    private String time;
    
    public NewJPanel()
    {
        initComponents();
        try
        {
            while(true)
            {
                initDate();
                int h = now.getHours();
                int m = now.getMinutes();
                int s = now.getSeconds();
                Thread.sleep(999);
                if (h > 12)
                {
                    h -= 12;
                }
                if (m < 10)
                {
                    String minFirst = Integer.toString(m);
                    String minSecond = "0" + minFirst;
                    time = h + ":" + minSecond + ":" + s;
                }
                if (m > 10)
                {    
                    time = h + ":" + m + ":" + s;
                }
                jLabel1.setText(time);
                System.out.println(time);
            }
        }
        catch (InterruptedException ex)
        {
            Logger.getLogger(NewJPanel.class.getName()).log(Level.SEVERE, null, ex);
        }    
    }
    private void initDate()
    {
        now = new Date();
    }
    
    // Variables declaration - do not modify                    
    private javax.swing.JLabel jLabel1;
    // End of variables declaration                  
    
}


Ok see were i set the jLabel1.setText(time)

it doesn't set it. idk why. It wont set it to the label. It runs and it prints the time out in netbeans in the output window. But not in the label.

This post has been edited by Maverick519: 24 Sep, 2008 - 11:53 AM
User is offlineProfile CardPM
+Quote Post

johnmalloy
RE: Java Clock GUI Question
24 Sep, 2008 - 12:02 PM
Post #10

New D.I.C Head
*

Joined: 24 Sep, 2008
Posts: 8



Thanked: 2 times
My Contributions
Suggestion, rename the class file and remember to add it in the applet. You are loading the applet but not referencing the class to the JPanel.
**EDIT**
OK blonde momment, insofar as I remember, you have to have the Class inside of the applet.
Example:
CODE

Public class TestClass extends JApplet{
  <Global variables here>
  public void init() {
    <Place Container and everything here from the class>
  }
  class codeClock() {
     <Place Code for the clock here>
  }
}


Then you create an HTML page place the applet in the <body> tag and load in browser.
That should do it.
Hope this helps.


This post has been edited by johnmalloy: 24 Sep, 2008 - 12:26 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:15AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month