Download Jar. Libraryless. Click here for Pure Java version (1119L/9K).
| 1 | !7 | 
| 2 | |
| 3 | // from http://javahungry.blogspot.com/2013/06/calendar-implementation-gui-based.html | 
| 4 | |
| 5 | static JLabel lblMonth, lblYear; | 
| 6 | static JButton btnPrev, btnNext; | 
| 7 | static JTable tblCalendar; | 
| 8 | static JComboBox cmbYear; | 
| 9 | static JFrame frmMain; | 
| 10 | static Container pane; | 
| 11 | static DefaultTableModel mtblCalendar; //Table model | 
| 12 | static JScrollPane stblCalendar; //The scrollpane | 
| 13 | static JPanel pnlCalendar; | 
| 14 | static int realYear, realMonth, realDay, currentYear, currentMonth; | 
| 15 | |
| 16 | p {
 | 
| 17 | //Prepare frame | 
| 18 |     frmMain = new JFrame ("Gestionnaire de clients"); //Create frame
 | 
| 19 | frmMain.setSize(330, 375); //Set size to 400x400 pixels | 
| 20 | pane = frmMain.getContentPane(); //Get content pane | 
| 21 | pane.setLayout(null); //Apply null layout | 
| 22 | frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Close when X is clicked | 
| 23 | |
| 24 | //Create controls | 
| 25 |     lblMonth = new JLabel ("January");
 | 
| 26 |     lblYear = new JLabel ("Change year:");
 | 
| 27 | cmbYear = new JComboBox(); | 
| 28 |     btnPrev = new JButton ("<<");
 | 
| 29 |     btnNext = new JButton (">>");
 | 
| 30 |     mtblCalendar = new DefaultTableModel(){public boolean isCellEditable(int rowIndex, int mColIndex){return false;}};
 | 
| 31 | tblCalendar = new JTable(mtblCalendar); | 
| 32 | stblCalendar = new JScrollPane(tblCalendar); | 
| 33 | pnlCalendar = new JPanel(null); | 
| 34 | |
| 35 | //Set border | 
| 36 |     pnlCalendar.setBorder(BorderFactory.createTitledBorder("Calendar"));
 | 
| 37 | |
| 38 | //Register action listeners | 
| 39 | btnPrev.addActionListener(new btnPrev_Action()); | 
| 40 | btnNext.addActionListener(new btnNext_Action()); | 
| 41 | cmbYear.addActionListener(new cmbYear_Action()); | 
| 42 | |
| 43 | //Add controls to pane | 
| 44 | pane.add(pnlCalendar); | 
| 45 | pnlCalendar.add(lblMonth); | 
| 46 | pnlCalendar.add(lblYear); | 
| 47 | pnlCalendar.add(cmbYear); | 
| 48 | pnlCalendar.add(btnPrev); | 
| 49 | pnlCalendar.add(btnNext); | 
| 50 | pnlCalendar.add(stblCalendar); | 
| 51 | |
| 52 | //Set bounds | 
| 53 | pnlCalendar.setBounds(0, 0, 320, 335); | 
| 54 | lblMonth.setBounds(160-lblMonth.getPreferredSize().width/2, 25, 100, 25); | 
| 55 | lblYear.setBounds(10, 305, 80, 20); | 
| 56 | cmbYear.setBounds(230, 305, 80, 20); | 
| 57 | btnPrev.setBounds(10, 25, 50, 25); | 
| 58 | btnNext.setBounds(260, 25, 50, 25); | 
| 59 | stblCalendar.setBounds(10, 50, 300, 250); | 
| 60 | |
| 61 | //Make frame visible | 
| 62 | frmMain.setResizable(false); | 
| 63 | frmMain.setVisible(true); | 
| 64 | |
| 65 | //Get real month/year | 
| 66 | GregorianCalendar cal = new GregorianCalendar(); //Create calendar | 
| 67 | realDay = cal.get(GregorianCalendar.DAY_OF_MONTH); //Get day | 
| 68 | realMonth = cal.get(GregorianCalendar.MONTH); //Get month | 
| 69 | realYear = cal.get(GregorianCalendar.YEAR); //Get year | 
| 70 | currentMonth = realMonth; //Match month and year | 
| 71 | currentYear = realYear; | 
| 72 | |
| 73 | //Add headers | 
| 74 |     String[] headers = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; //All headers
 | 
| 75 |     for (int i=0; i<7; i++){
 | 
| 76 | mtblCalendar.addColumn(headers[i]); | 
| 77 | } | 
| 78 | |
| 79 | tblCalendar.getParent().setBackground(tblCalendar.getBackground()); //Set background | 
| 80 | |
| 81 | //No resize/reorder | 
| 82 | tblCalendar.getTableHeader().setResizingAllowed(false); | 
| 83 | tblCalendar.getTableHeader().setReorderingAllowed(false); | 
| 84 | |
| 85 | //Single cell selection | 
| 86 | tblCalendar.setColumnSelectionAllowed(true); | 
| 87 | tblCalendar.setRowSelectionAllowed(true); | 
| 88 | tblCalendar.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); | 
| 89 | |
| 90 | //Set row/column count | 
| 91 | tblCalendar.setRowHeight(38); | 
| 92 | mtblCalendar.setColumnCount(7); | 
| 93 | mtblCalendar.setRowCount(6); | 
| 94 | |
| 95 | //Populate table | 
| 96 |     for (int i=realYear-100; i<=realYear+100; i++){
 | 
| 97 | cmbYear.addItem(String.valueOf(i)); | 
| 98 | } | 
| 99 | |
| 100 | //Refresh calendar | 
| 101 | refreshCalendar (realMonth, realYear); //Refresh calendar | 
| 102 | } | 
| 103 | |
| 104 | public static void refreshCalendar(int month, int year){
 | 
| 105 | //Variables | 
| 106 |     String[] months =  {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
 | 
| 107 | int nod, som; //Number Of Days, Start Of Month | 
| 108 | |
| 109 | //Allow/disallow buttons | 
| 110 | btnPrev.setEnabled(true); | 
| 111 | btnNext.setEnabled(true); | 
| 112 |     if (month == 0 && year <= realYear-10){btnPrev.setEnabled(false);} //Too early
 | 
| 113 |     if (month == 11 && year >= realYear+100){btnNext.setEnabled(false);} //Too late
 | 
| 114 | lblMonth.setText(months[month]); //Refresh the month label (at the top) | 
| 115 | lblMonth.setBounds(160-lblMonth.getPreferredSize().width/2, 25, 180, 25); //Re-align label with calendar | 
| 116 | cmbYear.setSelectedItem(String.valueOf(year)); //Select the correct year in the combo box | 
| 117 | |
| 118 | //Clear table | 
| 119 |     for (int i=0; i<6; i++){
 | 
| 120 |         for (int j=0; j<7; j++){
 | 
| 121 | mtblCalendar.setValueAt(null, i, j); | 
| 122 | } | 
| 123 | } | 
| 124 | |
| 125 | //Get first day of month and number of days | 
| 126 | GregorianCalendar cal = new GregorianCalendar(year, month, 1); | 
| 127 | nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH); | 
| 128 | som = cal.get(GregorianCalendar.DAY_OF_WEEK); | 
| 129 | |
| 130 | //Draw calendar | 
| 131 |     for (int i=1; i<=nod; i++){
 | 
| 132 | int row = new Integer((i+som-2)/7); | 
| 133 | int column = (i+som-2)%7; | 
| 134 | mtblCalendar.setValueAt(i, row, column); | 
| 135 | } | 
| 136 | |
| 137 | //Apply renderers | 
| 138 | tblCalendar.setDefaultRenderer(tblCalendar.getColumnClass(0), new tblCalendarRenderer()); | 
| 139 | } | 
| 140 | |
| 141 | static class tblCalendarRenderer extends DefaultTableCellRenderer{
 | 
| 142 |     public Component getTableCellRendererComponent (JTable table, Object value, boolean selected, boolean focused, int row, int column){
 | 
| 143 | super.getTableCellRendererComponent(table, value, selected, focused, row, column); | 
| 144 |         if (column == 0 || column == 6){ //Week-end
 | 
| 145 | setBackground(new Color(255, 220, 220)); | 
| 146 | } | 
| 147 |         else{ //Week
 | 
| 148 | setBackground(new Color(255, 255, 255)); | 
| 149 | } | 
| 150 |         if (value != null){
 | 
| 151 |             if (Integer.parseInt(value.toString()) == realDay && currentMonth == realMonth && currentYear == realYear){ //Today
 | 
| 152 | setBackground(new Color(220, 220, 255)); | 
| 153 | } | 
| 154 | } | 
| 155 | setBorder(null); | 
| 156 | setForeground(Color.black); | 
| 157 | return this; | 
| 158 | } | 
| 159 | } | 
| 160 | |
| 161 | static class btnPrev_Action implements ActionListener{
 | 
| 162 |     public void actionPerformed (ActionEvent e){
 | 
| 163 |         if (currentMonth == 0){ //Back one year
 | 
| 164 | currentMonth = 11; | 
| 165 | currentYear -= 1; | 
| 166 | } | 
| 167 |         else{ //Back one month
 | 
| 168 | currentMonth -= 1; | 
| 169 | } | 
| 170 | refreshCalendar(currentMonth, currentYear); | 
| 171 | } | 
| 172 | } | 
| 173 | static class btnNext_Action implements ActionListener{
 | 
| 174 |     public void actionPerformed (ActionEvent e){
 | 
| 175 |         if (currentMonth == 11){ //Foward one year
 | 
| 176 | currentMonth = 0; | 
| 177 | currentYear += 1; | 
| 178 | } | 
| 179 |         else{ //Foward one month
 | 
| 180 | currentMonth += 1; | 
| 181 | } | 
| 182 | refreshCalendar(currentMonth, currentYear); | 
| 183 | } | 
| 184 | } | 
| 185 | static class cmbYear_Action implements ActionListener{
 | 
| 186 |     public void actionPerformed (ActionEvent e){
 | 
| 187 |         if (cmbYear.getSelectedItem() != null){
 | 
| 188 | String b = cmbYear.getSelectedItem().toString(); | 
| 189 | currentYear = Integer.parseInt(b); | 
| 190 | refreshCalendar(currentMonth, currentYear); | 
| 191 | } | 
| 192 | } | 
| 193 | } | 
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, jmyaexzvplwz, lpdgvwnxivlt, mjouhigkdmyk, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1013736 | 
| Snippet name: | Calendar UI (original) | 
| Eternal ID of this version: | #1013736/2 | 
| Text MD5: | 55b335ec203ff259a6ec4e47bfe19362 | 
| Transpilation MD5: | 9c2cbb6b5ffc39c80d439c6f32262996 | 
| Author: | stefan | 
| Category: | javax / gui | 
| Type: | JavaX source code (desktop) | 
| Public (visible to everyone): | Yes | 
| Archived (hidden from active list): | No | 
| Created/modified: | 2018-02-10 16:54:42 | 
| Source code size: | 7173 bytes / 193 lines | 
| Pitched / IR pitched: | No / No | 
| Views / Downloads: | 806 / 1741 | 
| Version history: | 1 change(s) | 
| Referenced in: | [show references] |