Hi. I currently have set up a GUI to have buttons on, linking to all my other classes.

I am wondering how I would put the date and time at the bottom of the JFrame/JPanel. How would I do this, and will I put it within my GUI class, or make a new one?
Thank You.

Recommended Answers

All 9 Replies

Hi. I currently have set up a GUI to have buttons on, linking to all my other classes.

I am wondering how I would put the date and time at the bottom of the JFrame/JPanel. How would I do this, and will I put it within my GUI class, or make a new one?
Thank You.

Date today = new Date();
label.setText("today is: " + today);

if you want the current time to be updated continuously, you might want to use a thread to do so

Place collected date&time from Date class in form of text to JLabel

PS: just like stultuske showed in his post just that crucial second before me LOL

Place collected date&time from Date class in form of text to JLabel

PS: just like stultuske showed in his post just that crucial second before me LOL

the turtle can always beat the rabbit ;)

commented: OK, turtle just don't get my angry ;) +12

Sorry, probably a very simple thing, but I dont understand. Where do I put this within my code?

package org.project;

import java.awt.event.*;
import java.io.IOException;
import java.sql.SQLException;
import java.awt.*;
import javax.swing.*;

public class guiMenu {
	

	private JFrame frame; //Sets the frame
	private JPanel panel; //Sets the panel
	private JLabel picture; //Sets the label
	private JButton btnReadFile, btnReadDatabase, btnOpenMatchResults; //Sets the buttons
	static Font a = new Font ("Verdana", Font.BOLD, 11); //Sets a new font
	
	
	
	public static void main(String[]args){
		
		new guiMenu();
	}

	/**
	 * @param args
	 */
	public guiMenu()
	{
		// TODO Auto-generated method stub
		
		frame = new JFrame("The Football League Championship (2008 - 2009)"); //Sets the form name and size
		frame.setSize(600, 400);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		panel = new JPanel(); //Sets the panel
		panel.setLayout(null);
		panel.setBackground(Color.white);
		panel.add(new TextArea(), BorderLayout.CENTER);
		
		btnOpenMatchResults = new JButton("Match Results", new ImageIcon("open.gif"));; //Adds button to the panel, settings its size, co-ordinates and font
		btnOpenMatchResults.setBounds(355, 103, 200, 40);
		btnOpenMatchResults.setFont(a);
		btnOpenMatchResults.addActionListener(new ViewMatchResults()); //Adds action listener to the button
		panel.add(btnOpenMatchResults);
		
		btnReadFile = new JButton("Update League Table", new ImageIcon("update.gif"));
		btnReadFile.setBounds(355, 166, 200, 40);
		btnReadFile.setFont(a);
		btnReadFile.addActionListener(new ReadMatchFile()); //Adds action listener to the button
		panel.add(btnReadFile);
		
		btnReadDatabase = new JButton("League Table", new ImageIcon("open.gif"));; //Adds button to the panel, settings its size, co-ordinates and font
		btnReadDatabase.setBounds(355, 232, 200, 40);
		btnReadDatabase.setFont(a);
		btnReadDatabase.addActionListener(new ReadTheDatabase()); //Adds action listener to the button
		panel.add(btnReadDatabase);
		
		ImageIcon image = new ImageIcon ("football.gif"); //Adds an image to the panel, setting its size and co-ordinates
		picture = new JLabel (image);
		picture.setBounds (0,0,600,400);
		panel.add(picture);
		
		frame.getContentPane().add(panel);
		frame.setVisible(true);
	}
	
	class ReadMatchFile implements ActionListener
	{
		
		public void actionPerformed(ActionEvent event)
		{
		
		//Object source = event.getSource();	
		
		System.out.println("Results have been updates in the League Table");
		readFile fileIn = new readFile();
		try
		{
			fileIn.readIn();
		} 
		
		catch (Exception e)
		
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		}
	}
	

	class ReadTheDatabase implements ActionListener
	{
			
		public void actionPerformed(ActionEvent event)
		{
			
			//Object source = event.getSource();	
			
			readDatabase ReadIn = null;
			try {
				ReadIn = new readDatabase();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try 
			{
				ReadIn.readData();
			} 
			catch (SQLException e1) 
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
	
	class ViewMatchResults implements ActionListener
	{
		
		public void actionPerformed(ActionEvent event)
		{
		
		//Object source = event.getSource();	
		
		openResults fileIn = new openResults();
		try
		{
			fileIn.readMatch();
		} 
		
		catch (Exception e)
		
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		}
	}
	}

Thank You.

You want to have date&time at the bottom of the frame so place it inside guiMenu() before setVisible() for simplicity now.

I have created this, but the two "Date" appear in red so the program sends back errors...

private JLabel label;


Date today = new Date();
label.setText("today is: " + today);

Don't forget import java.util.Date;

Date today = new Date();
JLabel label = new JLabel();
label.setText("today is: "+today.toString()); 
panel.add(label);

Few notes:

  • panel.setLayout(null); are you sure you want to do this? You better to leave default layout or set one.
  • Have look on Java naming conventions as you use them in bad way
  • Use of code tags would be nice addition to your posted code

No errors come up, but the date and time is still not appearing...

Hence my hint on panel.setLayout(null); , comment it out and see what happens. Maybe after that you go for layouts ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.