Java Clock GUI Question

Java Clock GUI Question

Page 1 of 1

9 Replies - 6208 Views - Last Post: 24 September 2008 - 01:02 PM Rate Topic: -----

#1 Maverick519  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 01-December 07

Java Clock GUI Question

Post icon  Posted 24 September 2008 - 11:18 AM

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

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

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 September 2008 - 11:19 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Java Clock GUI Question

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Java Clock GUI Question

Posted 24 September 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
<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:

Hope all goes well! :D

This post has been edited by Locke37: 24 September 2008 - 11:22 AM

Was This Post Helpful? 0
  • +
  • -

#3 Maverick519  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 01-December 07

Re: Java Clock GUI Question

Posted 24 September 2008 - 11:25 AM

View PostLocke37, on 24 Sep, 2008 - 11:22 AM, said:

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
<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:

Hope all goes well! :D



where would i put that in my code? idk how to do that. Do i even need a main?
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Java Clock GUI Question

Posted 24 September 2008 - 11:31 AM

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...

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 September 2008 - 11:32 AM

Was This Post Helpful? 0
  • +
  • -

#5 Maverick519  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 01-December 07

Re: Java Clock GUI Question

Posted 24 September 2008 - 11:40 AM

It sais it failed to load.
Was This Post Helpful? 0
  • +
  • -

#6 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Java Clock GUI Question

Posted 24 September 2008 - 11:47 AM

I was afraid of that...gimme one minute. :)

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

// 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:)

This post has been edited by Locke37: 24 September 2008 - 11:52 AM

Was This Post Helpful? 1
  • +
  • -

#7 Maverick519  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 01-December 07

Re: Java Clock GUI Question

Posted 24 September 2008 - 12:01 PM

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.
Was This Post Helpful? 0
  • +
  • -

#8 Maverick519  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 01-December 07

Re: Java Clock GUI Question

Posted 24 September 2008 - 12:11 PM

idk if netbeans makes a .class file i cant find it.
Was This Post Helpful? 0
  • +
  • -

#9 Maverick519  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 01-December 07

Re: Java Clock GUI Question

Posted 24 September 2008 - 12:51 PM

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

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 September 2008 - 12:53 PM

Was This Post Helpful? 0
  • +
  • -

#10 johnmalloy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 24-September 08

Re: Java Clock GUI Question

Posted 24 September 2008 - 01:02 PM

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:
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 September 2008 - 01:26 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1