import javax.swing.*; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; import java.util.Date; import java.text.*; /** * Sample 9 * */ public class Sample9 extends JFrame implements ActionListener{ // inherit JFrame BorderLayout layout = null; JLabel statusLine = new JLabel(" ",JLabel.RIGHT); Timer tick = null; JTabbedPane tabPanel = new JTabbedPane(); String[] toons = {"Fred Flintstone","Wilma Flinstone","Dino","Barney Rubble","Betty Rubble", "Jane Jetson","George Jetson","Kilroy Jetson","Judy Jetson","Astro", "Marge Simpson","Homer Simpson","Bart Simpson","Maggie Simpson","Lisa Simpson", "Santa's Little Helper"}; JList list = new JList(toons); // create a list control loaded with character names JScrollPane tab1 = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); // make the list scrollable JTable table = new JTable(100,100); // declare a table object JScrollPane tab2 = new JScrollPane(table); // define a scrollpane that will hold the table JTextArea text = new JTextArea(); // declare a text area object JScrollPane tab3 = new JScrollPane(text); // define a scrollpane that will hold the text area /** * This Constructor sets up the window */ public Sample9() { // // setup the frames banner/title bar // super("Sample9"); // call JFrame's constructor and give it a title setSize(600,400); // set the width of the window to 300 pixels, height to 200. // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exit the whole program when this frame is closed addWindowListener(new WindowAdapter() { // Quit the application public void windowClosing(WindowEvent e) { System.exit(0); } }); // // create a status line with a timer // getContentPane().add("South",statusLine); // add it to the bottom tick = new Timer(1000,this); // create a timer to update time every second tick.start(); // start the timer // // initialize the table and tabbed panel objects // table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); // tell it not to resize cells when window size changes (this allows scrolling) tabPanel.add("Cartoon Characters",tab1); // add the list as the first tab tabPanel.add("A 100x100 Table",tab2); // add the table/scrollpane as the second tab tabPanel.add("Text Editor", tab3); // add the text editor as the third tab getContentPane().add("Center",tabPanel); // add it to the center area } /** * Listen for timer events */ public void actionPerformed(ActionEvent e) { if(e.getSource() == tick) { // only do the following if the event was generated by the timer we created SimpleDateFormat df = new SimpleDateFormat("h:mm:ss aaa"); // create a date format object Date now = new Date(System.currentTimeMillis()); // creaate a date object based on current time statusLine.setText(df.format(now)); // update the time on the status line } } /********************************* * Main routine, Note, this routine can be in any class. */ public static void main(String[] arguments){ Sample9 fr = new Sample9(); // create the frame fr.setVisible(true); // allow it to be seen } }