Hello,
I'm really new to doing Java only been doing it about 2 month so really a novice. I'm currently attempting to make my first app that is not a school project. I want to embed the current time in my application that is pulled from the local system. It is a time card program and when I create my time function I'm just unsure where to call it so that it comes up in the main frame. If I'm making any glaring errors please be gentle ass this is also my first time posting on this site.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.Border;
import java.util.Calendar;
import java.text.SimpleDateFormat;

public class TimeCard {
	int Date;
	int Time;
	int Hours;
	int timeStart;
	int timeStop;
	int time2Start;
	int time2Stop;
	
	

	public TimeCard() {
		
	}

	/**
	 * @param args
	 */	
	public static void myWindow() {
		JFrame mainFrame = new JFrame("Time Card"); // Creates JFrame object
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
		mainFrame.setSize(600,400); // Size of JFrame 600x400.
		mainFrame.setVisible(true); // Make Visible

		// Create a panel and add components to it
		JPanel cPane = new JPanel();
		// Create a Content Pane lowered and sunk in
		Border loweredbevel = BorderFactory.createLoweredBevelBorder();
		cPane.setBorder(loweredbevel);
		mainFrame.add(cPane);
		JOptionPane.showMessageDialog(cPane, "Welcome to Time Card");
		
	}	
	public String timeNow(){
		Calendar cal = Calendar.getInstance();
		String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
		return sdf.format(cal.getTime());
		
	}
		

	public static void main(String[] args) {

Thanks for your reply and it may seem dumb but I am not getting the placement. When I attempted to created this eclipse stated that I make getter and setters and it placed them after the main section which does not allow the time to show within my frame. I figure I will need to align it at some point but I don't even show it at this time so is my placement off?

I'm not really following your description of the issue. If you could post the code and mention where you believe there is a problem, that would probably help.

OK here are the changes I made which I know is still not complete but I'm trying to at least get the JLabel to show in my Frame and it does not. I figure once I get it to show that I can move forward with the rest of my code from there.
I know I'm doing something wrong but I'm so new at this I'm just not picking it up.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.Border;
import java.util.Calendar;
import java.text.SimpleDateFormat;

public class TimeCard {
	int Date;
	int Time;
	int Hours;
	int timeStart;
	int timeStop;
	int time2Start;
	int time2Stop;
	private static JLabel time;
	
	

	
	public static void timeCard() {
		JFrame mainFrame = new JFrame("Networking Solutions Time Card"); // Creates JFrame object
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
		mainFrame.setSize(600,400); // Size of JFrame 600x400.
		mainFrame.setVisible(true); // Make Visible

		// Create a panel and add components to it
		JPanel cPane = new JPanel();
		// Create a Content Pane lowered and sunk in
		Border loweredbevel = BorderFactory.createLoweredBevelBorder();
		cPane.setBorder(loweredbevel);
		mainFrame.add(cPane);
		JOptionPane.showMessageDialog(cPane, "Welcome to the Networking Solution Time Card");
		
		setTime(new JLabel("This is the time"));
		}
	
		
	public String timeNow(){
		Calendar cal = Calendar.getInstance();
		String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
		return sdf.format(cal.getTime());
		
		
	}
		

	public static void setTime(JLabel time) {
		TimeCard.time = time;
	}


	public static JLabel getTime() {
		return time;
	}


	public static void main(String[] args) {
		
		timeCard(); // Call frame into being	
	
	}
	
 }

Ok, first thing, drop the static modifiers. Let TimeCard be a normal object and make that code you have in the static method timeCard() your constructor for the class. main() can instantiate it to run it

public static void main(String[] args) {
  new TimeCard();
}

This just gets you away from the "make everything static and run it through main()" design mindset. Java is an OO language so you want to approach it that way as soon as possible.

Now, moving on, initialize your 'time' Jlabel in the constructor and add it to your 'cPane' panel when you are setting up your frame and panel components. Post back if you have trouble with that part. It's better if you can try it yourself first.

Once the label is on the panel, all you have to do to update the time text is call

time.setText( timeNow() );

That can be done with the Swing Timer I mentioned above, but you need a label displaying some text first.

Thanks for your help been trying your suggestions but have not gotten it to work. I guess I need some more trial and error so it's back to the drawing board. I will come back once my code is working better.

Ok I finally got what you were saying Ezzaral and I got it to work. I created the JLabel and was able to call the time it worked perfectly. Thanks again for all your helped and making a noob like me feel welcome.

This article has been dead for over six months. Start a new discussion instead.