Results 1 to 3 of 3
  1. #1
    hariza is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default scrollPane to handle 24 hours time

    Hi ,

    I want to add a scrollPane but I want to display/list a selection of 24 hour valid times so the user can pick anytime from the scrollPane. If there a function within java that can help me to generate that list. Thanks..

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You could fill it with either Strings that show your time, or if you want to get just a little fancier, fill it with Date objects, and use a renderer that displays your date objects in a way satisifies your requirements.

    For instance, I had a JTable recently where I wanted the left column of data to show times, and I used a custom DefaultTableCellRenderer that uses a SimpleDateFormat object to format my Date into an appropriate String. For instance
    Java Code:
       public static final String TIME_PARSE_STR = "hh:mm a";
       private SimpleDateFormat sdFormat = new SimpleDateFormat(TIME_PARSE_STR);
    will format the date to show 12 hour and minutes followed by am or pm, i.e., 03:30 pm.

    Alternatively if my String were "HH:mm" I'd have a 24 hour military clock.

    Here's my code:
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.Window;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class TimeGrid {
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("TimeGride");
          frame.getContentPane().add(new TimeGridMainPanel());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    
    class TimeGridMainPanel extends JPanel {
       private static final long serialVersionUID = 1L;
       private static final int START_TIME = 8;
       private static final int END_TIME = 18;
       private TimeGridTableModel model = new TimeGridTableModel(START_TIME, END_TIME);
       private JTable table = new JTable(model);
       
       public TimeGridMainPanel() {
          table.getColumnModel().getColumn(0).setCellRenderer(new DateCellRenderer());
          JButton clearBtn = new JButton("Clear");
          clearBtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                model.clear();
             }
          });
          JButton exitBtn = new JButton("Exit"); 
          exitBtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                Window win = SwingUtilities.getWindowAncestor(TimeGridMainPanel.this);
                win.dispose();
             }
          });
          JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 10, 0));
          buttonPanel.add(clearBtn);
          buttonPanel.add(new JButton("Save"));
          buttonPanel.add(exitBtn);
          
          setLayout(new BorderLayout());
          add(new JScrollPane(table), BorderLayout.CENTER);
          add(buttonPanel, BorderLayout.SOUTH);
       }
    }
    
    class TimeGridTableModel extends DefaultTableModel {
       private static final long serialVersionUID = 1L;
       public static final String[] HEADERS = {"Time", "Mon", "Tue", "Wed", "Thu", "Fri"};
       public static final String TIME_PARSE_STR = "HH:mm";
    
       private SimpleDateFormat sdFormat = new SimpleDateFormat(TIME_PARSE_STR);
    
       public TimeGridTableModel(int startTime, int endTime) {
          super(HEADERS, 0);
          for (int i = 0; i <= 2 * (endTime - startTime); i++) {
             String timeStr = String.valueOf(startTime + i / 2) + ":" + ((i % 2 == 0) ? "00" : "30");
             try {
                Date date = sdFormat.parse(timeStr);
                Object[] rowData = {date, new Boolean(false), new Boolean(false), new Boolean(false),
                   new Boolean(false), new Boolean(false)};
    
                addRow(rowData);
             } catch (ParseException e) {
                e.printStackTrace();
             }
          }
    
       }
    
       @Override
       public Class<?> getColumnClass(int columnIndex) {
          if (columnIndex == 0) {
             return Date.class;
          } else {
             return Boolean.class;
          }
       }
    
       @Override
       public boolean isCellEditable(int row, int column) {
          return column > 0;
       }
       
       public void clear() {
          for (int row = 0; row < getRowCount(); row++) {
             for (int column = 1; column < getColumnCount(); column++) {
                setValueAt(Boolean.FALSE, row, column);
             }
          }
       }
       
    }
    
    class DateCellRenderer extends DefaultTableCellRenderer {
       private static final long serialVersionUID = 1L;
       public static final String TIME_PARSE_STR = "hh:mm a";
       private SimpleDateFormat sdFormat = new SimpleDateFormat(TIME_PARSE_STR);
    
       public DateCellRenderer() {
          super();
       }
    
       public void setValue(Object value) {
          setText((value == null) ? "" : sdFormat.format(value));
       }
    
    }

  3. #3
    hariza is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

Similar Threads

  1. getting hours and minutes
    By silversurfer2in in forum New To Java
    Replies: 5
    Last Post: 02-17-2010, 07:35 AM
  2. how to add a scrollpane to Jframe
    By yaminisankar in forum AWT / Swing
    Replies: 1
    Last Post: 07-09-2009, 09:56 AM
  3. ScrollPane Issues..
    By hanifa in forum AWT / Swing
    Replies: 4
    Last Post: 09-11-2008, 08:18 AM
  4. Simple Project 4 or 5 Hours Pay $100
    By billaaa777 in forum Jobs Offered
    Replies: 0
    Last Post: 08-30-2008, 12:45 AM
  5. scrollpane
    By Newb in forum AWT / Swing
    Replies: 0
    Last Post: 06-09-2008, 12:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •