943,907 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1053
  • Java RSS
Nov 28th, 2008
0

Date and Time in GUI

Expand 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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ebiz is offline Offline
16 posts
since Nov 2008
Nov 28th, 2008
0

Re: Date and Time in GUI

Click to Expand / Collapse  Quote originally posted by ebiz ...
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)
  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
Reputation Points: 935
Solved Threads: 356
Nearly a Posting Maven
stultuske is offline Offline
2,497 posts
since Jan 2007
Nov 28th, 2008
0

Re: Date and Time in GUI

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.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Nov 28th, 2008
0

Re: Date and Time in GUI

Click to Expand / Collapse  Quote originally posted by peter_budo ...
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
Reputation Points: 935
Solved Threads: 356
Nearly a Posting Maven
stultuske is offline Offline
2,497 posts
since Jan 2007
Nov 28th, 2008
0

Re: Date and Time in GUI

Sorry, probably a very simple thing, but I dont understand. Where do I put this within my code?
java Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ebiz is offline Offline
16 posts
since Nov 2008
Nov 28th, 2008
0

Re: Date and Time in GUI

You want to have date&time at the bottom of the frame so place it inside guiMenu() before setVisible() for simplicity now.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Nov 28th, 2008
0

Re: Date and Time in GUI

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);
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ebiz is offline Offline
16 posts
since Nov 2008
Nov 28th, 2008
0

Re: Date and Time in GUI

Don't forget import java.util.Date;
Java Syntax (Toggle Plain Text)
  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
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Nov 28th, 2008
0

Re: Date and Time in GUI

No errors come up, but the date and time is still not appearing...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ebiz is offline Offline
16 posts
since Nov 2008
Nov 28th, 2008
0

Re: Date and Time in GUI

Hence my hint on panel.setLayout(null); , comment it out and see what happens. Maybe after that you go for layouts
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: editing file
Next Thread in Java Forum Timeline: star array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC