Date and Time in GUI

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2008
Posts: 16
Reputation: ebiz is an unknown quantity at this point 
Solved Threads: 0
ebiz ebiz is offline Offline
Newbie Poster

Date and Time in GUI

 
0
  #1
Nov 28th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Date and Time in GUI

 
0
  #2
Nov 28th, 2008
Originally Posted by ebiz View Post
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.
  1. Date today = new Date();
  2. 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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,196
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 485
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Re: Date and Time in GUI

 
0
  #3
Nov 28th, 2008
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
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Date and Time in GUI

 
1
  #4
Nov 28th, 2008
Originally Posted by peter_budo View Post
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 16
Reputation: ebiz is an unknown quantity at this point 
Solved Threads: 0
ebiz ebiz is offline Offline
Newbie Poster

Re: Date and Time in GUI

 
0
  #5
Nov 28th, 2008
Sorry, probably a very simple thing, but I dont understand. Where do I put this within my code?
  1. package org.project;
  2.  
  3. import java.awt.event.*;
  4. import java.io.IOException;
  5. import java.sql.SQLException;
  6. import java.awt.*;
  7. import javax.swing.*;
  8.  
  9. public class guiMenu {
  10.  
  11.  
  12. private JFrame frame; //Sets the frame
  13. private JPanel panel; //Sets the panel
  14. private JLabel picture; //Sets the label
  15. private JButton btnReadFile, btnReadDatabase, btnOpenMatchResults; //Sets the buttons
  16. static Font a = new Font ("Verdana", Font.BOLD, 11); //Sets a new font
  17.  
  18.  
  19.  
  20. public static void main(String[]args){
  21.  
  22. new guiMenu();
  23. }
  24.  
  25. /**
  26. * @param args
  27. */
  28. public guiMenu()
  29. {
  30. // TODO Auto-generated method stub
  31.  
  32. frame = new JFrame("The Football League Championship (2008 - 2009)"); //Sets the form name and size
  33. frame.setSize(600, 400);
  34. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35.  
  36. panel = new JPanel(); //Sets the panel
  37. panel.setLayout(null);
  38. panel.setBackground(Color.white);
  39. panel.add(new TextArea(), BorderLayout.CENTER);
  40.  
  41. btnOpenMatchResults = new JButton("Match Results", new ImageIcon("open.gif"));; //Adds button to the panel, settings its size, co-ordinates and font
  42. btnOpenMatchResults.setBounds(355, 103, 200, 40);
  43. btnOpenMatchResults.setFont(a);
  44. btnOpenMatchResults.addActionListener(new ViewMatchResults()); //Adds action listener to the button
  45. panel.add(btnOpenMatchResults);
  46.  
  47. btnReadFile = new JButton("Update League Table", new ImageIcon("update.gif"));
  48. btnReadFile.setBounds(355, 166, 200, 40);
  49. btnReadFile.setFont(a);
  50. btnReadFile.addActionListener(new ReadMatchFile()); //Adds action listener to the button
  51. panel.add(btnReadFile);
  52.  
  53. btnReadDatabase = new JButton("League Table", new ImageIcon("open.gif"));; //Adds button to the panel, settings its size, co-ordinates and font
  54. btnReadDatabase.setBounds(355, 232, 200, 40);
  55. btnReadDatabase.setFont(a);
  56. btnReadDatabase.addActionListener(new ReadTheDatabase()); //Adds action listener to the button
  57. panel.add(btnReadDatabase);
  58.  
  59. ImageIcon image = new ImageIcon ("football.gif"); //Adds an image to the panel, setting its size and co-ordinates
  60. picture = new JLabel (image);
  61. picture.setBounds (0,0,600,400);
  62. panel.add(picture);
  63.  
  64. frame.getContentPane().add(panel);
  65. frame.setVisible(true);
  66. }
  67.  
  68. class ReadMatchFile implements ActionListener
  69. {
  70.  
  71. public void actionPerformed(ActionEvent event)
  72. {
  73.  
  74. //Object source = event.getSource();
  75.  
  76. System.out.println("Results have been updates in the League Table");
  77. readFile fileIn = new readFile();
  78. try
  79. {
  80. fileIn.readIn();
  81. }
  82.  
  83. catch (Exception e)
  84.  
  85. {
  86. // TODO Auto-generated catch block
  87. e.printStackTrace();
  88. }
  89. }
  90. }
  91.  
  92.  
  93. class ReadTheDatabase implements ActionListener
  94. {
  95.  
  96. public void actionPerformed(ActionEvent event)
  97. {
  98.  
  99. //Object source = event.getSource();
  100.  
  101. readDatabase ReadIn = null;
  102. try {
  103. ReadIn = new readDatabase();
  104. } catch (SQLException e) {
  105. // TODO Auto-generated catch block
  106. e.printStackTrace();
  107. }
  108. try
  109. {
  110. ReadIn.readData();
  111. }
  112. catch (SQLException e1)
  113. {
  114. // TODO Auto-generated catch block
  115. e1.printStackTrace();
  116. }
  117. }
  118. }
  119.  
  120. class ViewMatchResults implements ActionListener
  121. {
  122.  
  123. public void actionPerformed(ActionEvent event)
  124. {
  125.  
  126. //Object source = event.getSource();
  127.  
  128. openResults fileIn = new openResults();
  129. try
  130. {
  131. fileIn.readMatch();
  132. }
  133.  
  134. catch (Exception e)
  135.  
  136. {
  137. // TODO Auto-generated catch block
  138. e.printStackTrace();
  139. }
  140. }
  141. }
  142. }
Thank You.
Last edited by Ancient Dragon; Nov 28th, 2008 at 5:26 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,196
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 485
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Re: Date and Time in GUI

 
0
  #6
Nov 28th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 16
Reputation: ebiz is an unknown quantity at this point 
Solved Threads: 0
ebiz ebiz is offline Offline
Newbie Poster

Re: Date and Time in GUI

 
0
  #7
Nov 28th, 2008
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);
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,196
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 485
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Re: Date and Time in GUI

 
0
  #8
Nov 28th, 2008
Don't forget import java.util.Date;
  1. Date today = new Date();
  2. JLabel label = new JLabel();
  3. label.setText("today is: "+today.toString());
  4. 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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 16
Reputation: ebiz is an unknown quantity at this point 
Solved Threads: 0
ebiz ebiz is offline Offline
Newbie Poster

Re: Date and Time in GUI

 
0
  #9
Nov 28th, 2008
No errors come up, but the date and time is still not appearing...
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,196
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 485
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Re: Date and Time in GUI

 
0
  #10
Nov 28th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC