| | |
Date and Time in GUI
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
•
•
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.
Java Syntax (Toggle Plain Text)
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
PS: just like stultuske showed in his post just that crucial second before me LOL
Last edited by peter_budo; Nov 28th, 2008 at 11:53 am.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Nov 2008
Posts: 16
Reputation:
Solved Threads: 0
Sorry, probably a very simple thing, but I dont understand. Where do I put this within my code?
Thank You.
java Syntax (Toggle Plain Text)
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(); } } } }
Last edited by Ancient Dragon; Nov 28th, 2008 at 5:26 pm. Reason: add code tags
You want to have date&time at the bottom of the frame so place it inside guiMenu() before setVisible() for simplicity now.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Don't forget
Few notes:
import java.util.Date; Java Syntax (Toggle Plain Text)
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
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Hence my hint on
panel.setLayout(null); , comment it out and see what happens. Maybe after that you go for layouts
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Similar Threads
- Creating first GUI Java Program (Java)
- how can add date in vb.net form (VB.NET)
- date n time TO BE DISPLAYED AT THE LEFT HAND CORNER . (Java)
- Program needs to have time updated every minute (Java)
- Remove Hidden Windows Components (Windows tips 'n' tweaks)
- internet messanger (Java)
- Time Refreshing on a GUI Menu Bar (Java)
Other Threads in the Java Forum
- Previous Thread: editing file
- Next Thread: star array
| Thread Tools | Search this Thread |
2dgraphics android api apple applet application arguments array arrays automation banking binary binarytree bluetooth capture chat chatprogramusingobjects class classes client code color component count database derby design eclipse eclipsedevelopment encryption error exception fractal game givemetehcodez graphics gridlayout gui html ide if_statement image input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel julia keyword linux list loop macintosh map method methods midlethttpconnection mobile netbeans newbie nullpointerexception object os print printing problem producer program programming project projectideas read recursion reference replaysolutions ria scanner screen server set size sms sort sourcelabs sql stop string swing threads transforms tree ui unicode validation windows






