EXTREMELY THANKFUL FOR ANYONE THAT WILL HELP ME
Hi. I have a huge question to ask you guys. I'm a java beginner and don't really know a whole lot. I mean I've been taking classes for about 6 months now and I know the basic logic of forming it and some of the standard library classes. The problem is we've never covered anything with date and time. What i want and NEEd to create for my personal use is a :
Java clock display with date, time and "Message Screen on top". Screen should shift and change colors slightly because I don't want it to "burn in". In time the seconds should be incrementing like in a digital clock and the whole thing should be in this format
Monday, May 24, 200505:25:06 PM.
The Message Screen should automatically scroll or transition at intervals in order for the whole message to be seen. I want to load the message from an externall file and after displaying it at a time schedule, it should go back to clock display. It should archive all those messages as well.
I came up with this idea because me and a couple of my friends are running this small company and we'd like to have that for our own personal use. Kind of an annoucement screen.
If anyone was so kind to help me in any kind of way I'd be grateful. Thanks in advance
For you first question you get some code.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class TimerTest extends JFrame implements ActionListener
{
JLabel timeLabel;
JLabel scrollLabel;
public TimerTest()
{
timeLabel = new JLabel( new Date().toString() );
getContentPane().add(timeLabel, BorderLayout.NORTH);
scrollLabel = new JLabel( "Some continuously scrolling text!!" );
getContentPane().add(scrollLabel, BorderLayout.SOUTH);
javax.swing.Timer timer = new javax.swing.Timer(100, this);
timer.start();
}
public void actionPerformed(ActionEvent e)
{
timeLabel.setText( new Date().toString() );
String oldText = scrollLabel.getText();
// Scroll right to left
String newText = oldText.substring(1) + oldText.substring(0, 1);
// Scroll left to right
//int length = oldText.length();
//String newText = oldText.substring(length-1, length) + oldText.substring(0, length-1);
scrollLabel.setText( newText );
}
public static void main(String[] args)
{
TimerTest frame = new TimerTest();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
}
Before posting any more questions you need to read the Swing tutorial titled "Createing aGUI Using JFC/Swing" for the basics:
http://java.sun.com/docs/books/tutorial/
Then entire tutorial can be downloaded from the same page.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.*;
import javax.swing.text.BadLocationException;
public class Messenger
{
TimeKeeper clock;
Message message;
CardLayout cards;
JPanel parent;
JButton read;
public Messenger()
{
clock = new TimeKeeper();
message = new Message(this);
cards = new CardLayout();
parent = new JPanel(cards);
parent.add("clock", clock);
parent.add("message", message);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(getUIPanel(), "North");
f.getContentPane().add(parent);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
clock.start();
}
private JPanel getUIPanel()
{
final JButton nav = new JButton("next");
read = new JButton("read");
read.setEnabled(false);
ActionListener l = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();
if(button == nav)
rotate();
if(button == read)
message.read();
}
};
nav.addActionListener(l);
read.addActionListener(l);
JPanel panel = new JPanel();
panel.add(nav);
panel.add(read);
return panel;
}
public void rotate()
{
cards.next(parent);
if(clock.isShowing())
{
clock.start();
read.setEnabled(false);
}
else
{
clock.stop();
read.setEnabled(true);
}
}
public static void main(String[] args)
{
new Messenger();
}
}
class Message extends JPanel
{
Messenger messenger;
JTextArea textArea;
JScrollPane scrollPane;
final int DELAY = 4 * 1000;
public Message(Messenger messenger)
{
this.messenger = messenger;
textArea = new JTextArea();
textArea.setMargin(new Insets(5,5,5,5));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
scrollPane = new JScrollPane(textArea);
setLayout(new BorderLayout());
add(scrollPane);
}
public void read()
{
File file = new File(".");
File[] files = file.listFiles(filter);
readFiles(files);
}
private void readFiles(final File[] files)
{
final int limit = Math.min(8, files.length);
for(int j = 0; j < limit; j++)
System.out.println(files[j].getName());
new Thread(new Runnable()
{
public void run()
{
// get here to avoid squirrelly results in scrollTextArea
int viewHeight = scrollPane.getViewport().getViewSize().height;
for(int j = 0; j < limit; j++)
{
// read file into textArea
System.out.println("reading: " + files[j].getName());
Reader reader = null;
try
{
reader = new BufferedReader(
new InputStreamReader(
new FileInputStream(files[j])));
textArea.read(reader, files[j]);
reader.close();
}
catch(FileNotFoundException fnfe)
{
System.err.println("file not found: " + fnfe.getMessage());
}
catch(IOException ioe)
{
System.err.println("read error: " + ioe.getMessage());
}
scrollPane.revalidate();
int totalHeight = getTextAreaHeight();
scrollTextArea(totalHeight, viewHeight);
textArea.setText("");
}
messenger.rotate();
}
}).start();
}
private int getTextAreaHeight()
{
// find height of filled textArea
int docLength = textArea.getDocument().getLength();
int height = 0;
try
{
Rectangle docSize = textArea.modelToView(docLength);
height = docSize.y + docSize.height;
}
catch(BadLocationException ble)
{
System.err.println("modelToView: " + ble.getMessage());
}
return height;
}
private void scrollTextArea(int totalHeight, int viewHeight)
{
int steps = totalHeight/viewHeight;
//System.out.println("totalHeight = " + totalHeight + "\t" +
//"viewHeight = " + viewHeight + "\t" +
//"steps = " + steps);
JScrollBar vScrollBar = scrollPane.getVerticalScrollBar();
int position = 0, count = 0;
// scroll document in increments
while(count++ <= steps)
{
vScrollBar.setValue(position);
textArea.repaint();
//System.out.println("position = " + position + "\t" +
//"totalHeight - position = " +
//(totalHeight - position));
position += viewHeight;
try
{
Thread.sleep(DELAY);
}
catch(InterruptedException ie)
{
System.err.println("scroll interrupt" + ie.getMessage());
}
}
}
private FileFilter filter = new FileFilter()
{
public boolean accept(File pathname)
{
String path = pathname.getPath();
int start = path.lastIndexOf(".");
String ext = path.substring(start+1);
if(ext.equals("java"))
return true;
return false;
}
};
}
class TimeKeeper extends JPanel
{
JLabel label;
Thread ticker;
boolean keepRunning;
DateFormat df;
public TimeKeeper()
{
keepRunning = false;
//Monday, May 24, 2005, 05:25:06 PM
df = new SimpleDateFormat("EEEE, MMMM d, yyyy, hh:mm:ss a");
label = new JLabel();
label.setFont(new Font("lucida sans demibold", Font.PLAIN, 14));
label.setHorizontalAlignment(JLabel.CENTER);
setLayout(new BorderLayout());
add(label);
}
private Runnable runner = new Runnable()
{
public void run()
{
keepRunning = true;
while(keepRunning)
{
Date now = Calendar.getInstance().getTime();
label.setText(df.format(now));
//System.out.println(Calendar.getInstance().get(Calendar.SECOND));
try
{
Thread.sleep(500);
}
catch(InterruptedException ie)
{
System.err.println("clock interrupt: " + ie.getMessage());
}
}
}
};
public void start()
{
if(!keepRunning)
{
ticker = new Thread(runner);
ticker.start();
}
}
public void stop()
{
keepRunning = false;
ticker = null;
}
}