1 import java.awt.*; 2 import java.awt.event.ActionEvent ; 3 import java.awt.event.ActionListener ; 4 import java.util.Calendar ; 5 import java.util.Date ; 6 import javax.swing.*; 7 import org.jdesktop.swing.calendar.*; 8 9 public class JXMonthViewDemo { 10 public static void main(String args[]) { 11 JFrame frame = new JFrame("Month Viewer Test App"); 12 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 13 final JXMonthView monthView = new JXMonthView(); 14 16 Calendar cal1 = Calendar.getInstance(); 17 cal1.set(2004, 1, 1); 18 Calendar cal2 = Calendar.getInstance(); 19 cal2.set(2004, 1, 5); 20 21 long[] flaggedDates = new long[] { 22 cal1.getTimeInMillis(), 23 cal2.getTimeInMillis(), 24 System.currentTimeMillis() 25 }; 26 java.util.Arrays.sort(flaggedDates); 27 28 monthView.setFlaggedDates(flaggedDates); 29 30 monthView.addActionListener(new ActionListener () { 31 public void actionPerformed(ActionEvent e) { 32 System.out.println( 33 ((JXMonthView)e.getSource()).getSelectedDateSpan()); 34 } 35 }); 36 37 monthView.setFirstDayOfWeek(Calendar.MONDAY); 38 monthView.setPreferredCols(1); 40 monthView.setPreferredRows(2); 41 monthView.setSelectionMode(JXMonthView.NO_SELECTION); 42 monthView.setTodayBackground(Color.BLUE); 43 44 JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.LEADING)); 46 47 JButton button = new JButton("Toggle orientation"); 48 button.addActionListener(new ActionListener () { 49 public void actionPerformed(ActionEvent ev) { 50 monthView.setComponentOrientation( 51 (monthView.getComponentOrientation() == 52 ComponentOrientation.RIGHT_TO_LEFT) ? 53 ComponentOrientation.LEFT_TO_RIGHT : 54 ComponentOrientation.RIGHT_TO_LEFT); 55 monthView.repaint(); 56 } 57 }); 58 controlPanel.add(button); 59 controlPanel.add(Box.createHorizontalStrut(5)); 60 61 JLabel label = new JLabel("Selection Mode:"); 62 controlPanel.add(label); 63 JComboBox cBox = new JComboBox(new String [] { "None", "Single", 64 "Multiple", "Week" }); 65 cBox.addActionListener(new ActionListener () { 66 public void actionPerformed(ActionEvent ev) { 67 JComboBox c = (JComboBox)ev.getSource(); 68 int index = c.getSelectedIndex(); 69 monthView.setSelectedDateSpan(null); 70 monthView.setSelectionMode(index); 71 } 72 }); 73 controlPanel.add(cBox); 74 controlPanel.add(Box.createHorizontalStrut(5)); 75 76 label = new JLabel("Anti-aliased text:"); 77 controlPanel.add(label); 78 JCheckBox checkBox = new JCheckBox(); 79 checkBox.addActionListener(new ActionListener () { 80 public void actionPerformed(ActionEvent ev) { 81 JCheckBox c = (JCheckBox)ev.getSource(); 82 monthView.setAntialiased(c.isSelected()); 83 } 84 }); 85 controlPanel.add(checkBox); 86 87 frame.getContentPane().setLayout(new BorderLayout()); 88 frame.getContentPane().add(monthView, BorderLayout.CENTER); 89 frame.getContentPane().add(controlPanel, BorderLayout.SOUTH); 90 frame.pack(); 91 frame.setVisible(true); 92 93 SwingUtilities.invokeLater(new Runnable () { 94 public void run() { 95 monthView.setSelectedDateSpan(new DateSpan( 96 new Date (System.currentTimeMillis()), 97 new Date (System.currentTimeMillis()))); 98 monthView.setSelectedDateSpan(null); 99 } 101 }); 102 } 103 } 104 | Popular Tags |