Results 1 to 17 of 17
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default a concrete question

    The main class contains the next code fragment:

    Java Code:
    else  {
                System.out.println(" file NOT found! ");
                JOptionPane.showMessageDialog(null,
                          "The application looks for the file\n" +
                          "CalendarDataFile.txt\n" +
                          "if not found the that file, two dialogs\n" +
                          "will appear after each other to\n" +
                          "set the day-panels of the\n" +
                          "calendar screen");
                dialog1 = new CalendarDialog("DIALOG1");
                dialog2 = new CalendarDialog("DIALOG2");
                calpanel = new CalendarPanel();
              
                dialog1.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
                dialog1.setBounds(300, 300, 520, 350);
                dialog1.setVisible(true);
     }
    
    // here the beginning of the constructor:
    
      public CalendarDialog(String flg) {
            flag = flg; // this flag will set the content of dialog1 or dialog2
            icon = createImageIcon("images/lic2.gif");
            setTitle(flag);
            System.out.println("print flag " + flag);

    so the first dialog appears as expected and the monitor prints:
    file NOT found!
    print flag DIALOG1
    print flag DIALOG2


    but when trying to manipulate the (first) dialog happens this:

    print flag DIALOG2
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at studyrooms.CalendarDialog$DialogListener.actionPer formed(CalendarDialog.java:383)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.JToggleButton$ToggleButtonModel.setPre ssed(JToggleButton.java:291)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6382)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3275)
    at java.awt.Component.processEvent(Component.java:614 7)
    at java.awt.Container.processEvent(Container.java:208 3)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4744)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2141)
    at java.awt.Component.dispatchEvent(Component.java:45 72)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4619)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4280)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4210)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2127)


    showing dialog window "DIALOG1" and: print flag DIALOG2

    why is that, when in dialog one the flag should be "DIALOG1"
    (I AM NOT GETTING THIS ONE AND I BELIEVE IT IS CRUCIAL?

  2. #2
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: a concrete question

    BTW when I swap order things do work!

    dialog2 = new CalendarDialog("DIALOG2");
    dialog1 = new CalendarDialog("DIALOG1");
    calpanel = new CalendarPanel();

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default Re: a concrete question

    Quote Originally Posted by willemjav View Post
    at studyrooms.CalendarDialog$DialogListener.actionPer formed(CalendarDialog.java:383)
    Check that line in your code; something is null in there and it shouldn't be; there's not much more that can be concluded without seeing your code ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: a concrete question

    Of course, jos, I know why and even where it happens.
    The second dialog (DIALOG2) sets an extra textfield which is
    not present at the first dialog (DIALOG1). Sure when at dialog1
    and wanting to get the text of that field will give you that null pointer
    for sure! But that is not the problem.

    When initiating both dialogs and opening the first one on
    screen the private flag is set to DIALOG1 and everything will be just fine.
    I do not understand why, when being at the dialog1 object the flag is
    DIALOG2 (of the last initiated object). So let each object: dialog1 and dialog2
    has its one flags: DIALOG1, DIALOG2, right?
    (line 11 = 383)
    Java Code:
    if (cmd.equals("SELECT")) {
                    for (int i = 0; i < Fieldnumber(flag) ; i++) { //comboref.length
                        if (compref[i].getboxref().isSelected())  {
                                compref[i].getcomboA1().setEnabled(true);
                                compref[i].getcomboA2().setEnabled(true);
                                compref[i].getcomboB1().setEnabled(true);
                                compref[i].getcomboB2().setEnabled(true);
                                System.out.println(" print flag " + flag);
                                if (flag.equals("DIALOG2")) // when second dialog selected
                                    compref[i].gettextfieldref().setEnabled(true); // line 383
                        }
                        else {
                            compref[i].getcomboA1().setEnabled(false);
                            compref[i].getcomboA2().setEnabled(false);
                            compref[i].getcomboB1().setEnabled(false);
                            compref[i].getcomboB2().setEnabled(false);
                            compref[i].getcomboA1().setSelectedIndex(0);
                            compref[i].getcomboA2().setSelectedIndex(0);
                            compref[i].getcomboB1().setSelectedIndex(0);
                            compref[i].getcomboB2().setSelectedIndex(0);
                            if (flag.equals("DIALOG2"))
                                compref[i].gettextfieldref().setEnabled(false);
                        }
                   }
                }
    Last edited by willemjav; 05-25-2013 at 12:01 PM.

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: a concrete question

    The dialog class:

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package studyrooms;
    
    /**
     *
     * @author willem
     */
    
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import javax.swing.*;
    
    
    /**
     *
     *
     */
    public class CalendarDialog extends JDialog{
        private ImageIcon icon;
        private ActionListener DListener;
        private ItemListener clistener;
        private JCheckBox selectbox;
        private JComboBox comboA1, comboA2, comboB1, comboB2;
        private JTextField infoField;
        private JTextArea infoarea;
        private JButton okb, chancelb, resetb;
        private ComponentRef[] compref;
        private String[] hourlist, minlist, monthlist, daylist;
        private int count;
        private static String flag;
        private CalendarDialog dialog2;
        private static CalendarDialog dialog1;
        private static CalendarPanel calenderpanel;
    
        public static void main(String[] args) {
           calenderpanel = new  CalendarPanel();
           calenderpanel.setBounds(30, 30, 1700, 900);
           calenderpanel.setTitle("Calendar Window");
           calenderpanel.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
           dialog1 = new CalendarDialog("DIALOG1");
           dialog1.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
           dialog1.setBounds(300, 300, 520, 350);
    
           dialog1.setModal(true);
           calenderpanel.setVisible(true);
           dialog1.setVisible(true);
    
        }
    
        public CalendarDialog(String flg) {
            flag = flg; // this flag will set the content of dialog1 or dialog2
            icon = createImageIcon("images/lic2.gif");
            setTitle(flag);
            System.out.println("print flag " + flag);
    
            setResizable(true);
            
         //   System.out.println("the flag is " + flag);
    
    
            hourlist = new String[] {"7", "8", "9", "10", "11", "12", "13",
                                 "14", "15", "16", "17", "18", "19", "20",
                                  "21", "22", "23"};
            minlist = new String[] {"00", "30"};
    
            monthlist = new String[] {"enero", "febrero", "marzo", "abril", "mayo",
                                   "junio", "julio", "augusto", "septiembro",
                                   "octobre", "noviembre", "deciembre"};
    
            daylist = new String[] {"1", "2", "3", "4", "5", "6", "7", "8", "9",
                                "10", "11", "12", "13","14", "15", "16", "17",
                                "18", "19", "20", "21", "22", "23", "24", "25",
                                "26", "27", "28", "29", "30", "31"};
    
            DListener = new DialogListener();
            clistener = new ComboListener();
    
            compref = new ComponentRef[Fieldnumber(flag)]; // initiate the class that holds
                for (int i = 0; i < compref.length; i++)  // the refferences of the boxes
                        compref[i] = new ComponentRef();
    
            Container contentPane = getContentPane();
            contentPane.add(createDialogPanel());
    
        }
    
    
         private JPanel createDialogPanel()  {
    
            JPanel panel = new JPanel(new BorderLayout());
            JPanel iconpanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 30, 10));
            JPanel buttonpanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
            JLabel iconlabel = new JLabel(icon);
            iconpanel.add(iconlabel);
    
            panel.setBackground(Color.WHITE);
            iconpanel.setBackground(Color.WHITE);
            buttonpanel.setBackground(Color.WHITE);
    
            infoarea = new JTextArea(3,25);
            infoarea.setPreferredSize(new Dimension(20,20));
            infoarea.setFont(new Font("Serif", Font.ITALIC, 16));
            infoarea.setEditable(false);
            if (flag.equals("DIALOG1")) { // first and second dialog text
                infoarea.setText("Set the schedule of the week days\n"
                             + "when active, check the box and\n"
                             + "and enter the duration");
            }
            if (flag.equals("DIALOG2"))  {
                infoarea.setText("Set several vacations and\n"
                             + "class free periods by checking\n"
                             + "the box and typing a short info");
            }
            iconpanel.add(infoarea);
    
            okb = new JButton("OK");
            okb.setActionCommand("OK");
            okb.addActionListener(DListener);
            chancelb = new JButton("CHANCEL");
            okb.setActionCommand("CH");
            okb.addActionListener(DListener);
    
            resetb = new JButton("RESET");
            resetb.setActionCommand("RS");
            resetb.addActionListener(DListener);
    
            buttonpanel.add(resetb);
            buttonpanel.add(okb);
            buttonpanel.add(chancelb);
    
            JPanel comppanel = new JPanel();
            comppanel.setBackground(Color.WHITE);
            comppanel.setLayout(new BoxLayout(comppanel,
                    BoxLayout.LINE_AXIS));
    
            Box bh = Box.createHorizontalBox();
            Box bv = Box.createVerticalBox();
    
            if (flag.equals("DIALOG1")) {
                bv.add(createSinglePanel("lunes    "));
                bv.add(createSinglePanel("martes   "));
                bv.add(createSinglePanel("miercoles"));
                bv.add(createSinglePanel("jueves   "));
                bv.add(createSinglePanel("viernes  "));
                bv.add(createSinglePanel("sabado   "));
                bv.add(createSinglePanel("domingo  "));
            }
            if (flag.equals("DIALOG2"))  {
                 for (int i = 0; i <  Fieldnumber(flag); i++)
                     bv.add(createSinglePanel((i+1) + " "));
            }
    
            comppanel.add(bv);
    
            panel.add(iconpanel, BorderLayout.NORTH);
            panel.add(comppanel, BorderLayout.CENTER);
            panel.add(buttonpanel, BorderLayout.SOUTH);
            return panel;
         }
    
         private Box createSinglePanel(String label)  {
            String[] list1 = null;
            String[] list2 = null;
    
            if (flag.equals("DIALOG1"))  { // first dialog
                list1 = hourlist;
                list2 = minlist;
    
            }
            if (flag.equals("DIALOG2"))  {  // second dialog
                list1 = monthlist;
                list2 = daylist;
            }
    
            JLabel lb = new JLabel(" until ");
            Box bx = Box.createHorizontalBox();
    
            selectbox = new JCheckBox(label);
            selectbox.setActionCommand("SELECT");
            selectbox.setSelected(false);
            selectbox.addActionListener(DListener);
            compref[count].setboxref(selectbox);
    
            comboA1 = new JComboBox(list1);
            comboA2 = new JComboBox(list2);
            comboB1 = new JComboBox(list1);
            comboB2 = new JComboBox(list2);
            lb.setLabelFor(comboB1);
    
            comboA1.setSelectedIndex(0);
            comboA2.setSelectedIndex(0);
            comboB1.setSelectedIndex(0);
            comboB2.setSelectedIndex(0);
    
            comboA1.addItemListener(clistener);
            comboA2.addItemListener(clistener);
        //    comboB1.addItemListener(clistener);
        //    comboB2.addItemListener(clistener);
    
            compref[count].setcomboA1(comboA1);
            compref[count].setcomboA2(comboA2);
            compref[count].setcomboB1(comboB1);
            compref[count].setcomboB2(comboB2);
            comboA1.setEnabled(false);
            comboA2.setEnabled(false);
            comboB1.setEnabled(false);
            comboB2.setEnabled(false);
    
            infoField = null;
            if (flag.equals("DIALOG2"))  {// in case of second dialog
                    infoField = new JTextField(23);
                    infoField.setMaximumSize(infoField.getPreferredSize());
                    infoField.setText("info of vacation etc");
                    infoField.setEnabled(false);
                    compref[count].settextfieldref(infoField);
            }
    
            bx.add(selectbox);
            if (flag.equals("DIALOG2")) bx.add(infoField);
            bx.add(Box.createHorizontalGlue());
            bx.add(comboA1);
            bx.add(comboA2);
            bx.add(lb);
            bx.add(comboB1);
            bx.add(comboB2);
    
            count++;
            return bx;
        }
    
        public JButton getOkButton() { // the reference of the OK and Chancel button
            return okb;
        }
        public JButton getChButton() {
            return  chancelb;
        }
    
    
        boolean CheckDialogInput() {
    
            boolean bln = true;
            String s = null;
            for (int i = 0; i < Fieldnumber(flag) ; i++) {
                if (compref[i].getboxref().isSelected()) {
                    if (compref[i].getcomboA1().getSelectedIndex() >
                        compref[i].getcomboB1().getSelectedIndex()) {
                        bln = false;
                    }
    
                    if (compref[i].getcomboA1().getSelectedIndex() ==
                        compref[i].getcomboB1().getSelectedIndex() &&
                        compref[i].getcomboA2().getSelectedIndex() >=
                        compref[i].getcomboB2().getSelectedIndex()) {
                        bln = false;
                    }
                    if (flag.equals("DIALOG2"))  {
                        System.out.println("print field ref " + compref[i].gettextfieldref());
                        s = compref[i].gettextfieldref().getText();
                        System.out.println("print string second " + s);
                        if ( s.equals("") || s.equals("info of vacation etc"))
                            bln = false;
                    }
                }
            }
            return bln;
        }
    
        private int Fieldnumber(String flg)  {
            if (flag.equals("DIALOG1"))
                 return 7;
            else
                 return 10;
        }
    
    
        class ComponentRef {
           private JCheckBox boxref;
           private JComboBox comborefA1, comborefA2, comborefB1, comborefB2;
           private JTextField textfieldref;
    
           public void setboxref(JCheckBox br)  {
                    boxref = br;
           }
           public JCheckBox getboxref()  {
                    return boxref;
           }
    
           public void setcomboA1(JComboBox cb)  {
                   comborefA1 = cb;
           }
           public JComboBox getcomboA1()  {
                    return comborefA1;
           }
    
           public void setcomboA2(JComboBox cb)  {
                   comborefA2 = cb;
           }
           public JComboBox getcomboA2()  {
                    return comborefA2;
           }
    
           public void setcomboB1(JComboBox cb)  {
                   comborefB1 = cb;
           }
           public JComboBox getcomboB1()  {
                    return comborefB1;
           }
    
           public void setcomboB2(JComboBox cb)  {
                   comborefB2 = cb;
           }
           public JComboBox getcomboB2()  {
                    return comborefB2;
           }
    
           public void settextfieldref(JTextField tf)  {
                   textfieldref = tf;
           }
           public JTextField gettextfieldref()  {
                    return textfieldref;
           }
    
        }
    
        private class ComboListener implements ItemListener {
    
            public void itemStateChanged(ItemEvent ie) {
    
                    int x = Fieldnumber(flag);
                    for (int i = 0; i < x ; i++) {
                        if (compref[i].getboxref().isSelected()) {
                            System.out.println("Source: " + ie.getItem());
                            x = compref[i].getcomboA1().getSelectedIndex();
                            compref[i].getcomboB1().setSelectedIndex(x);
                            for (int ii = 0; ii < x ; ii++) {
                              //  compref[i].getcomboB1().removeItemAt(ii+1);
    
                            }
                        }
                    }
    
                    for (int i = 0; i < x ; i++) {
                        if (compref[i].getboxref().isSelected()) {
                            x = compref[i].getcomboA2().getSelectedIndex();
                            compref[i].getcomboB2().setSelectedIndex(x);
                            for (int ii = 0; ii < x ; ii++) {
                              //  compref[i].getcomboB2().removeItemAt(ii+1);
    
                            }
                        }
                    }
            }
        }
    
        private class DialogListener implements ActionListener {
            public void actionPerformed(ActionEvent e)  {
                String cmd = e.getActionCommand();
    
                 if (cmd.equals("SELECT")) {
                    for (int i = 0; i < Fieldnumber(flag) ; i++) { //comboref.length
                        if (compref[i].getboxref().isSelected())  {
                                compref[i].getcomboA1().setEnabled(true);
                                compref[i].getcomboA2().setEnabled(true);
                                compref[i].getcomboB1().setEnabled(true);
                                compref[i].getcomboB2().setEnabled(true);
                                System.out.println(" print flag " + flag);
                                if (flag.equals("DIALOG2")) // when second dialog selected
                                    compref[i].gettextfieldref().setEnabled(true);
                        }
                        else {
                            compref[i].getcomboA1().setEnabled(false);
                            compref[i].getcomboA2().setEnabled(false);
                            compref[i].getcomboB1().setEnabled(false);
                            compref[i].getcomboB2().setEnabled(false);
                            compref[i].getcomboA1().setSelectedIndex(0);
                            compref[i].getcomboA2().setSelectedIndex(0);
                            compref[i].getcomboB1().setSelectedIndex(0);
                            compref[i].getcomboB2().setSelectedIndex(0);
                            if (flag.equals("DIALOG2"))
                                compref[i].gettextfieldref().setEnabled(false);
                        }
                   }
                }
    
    
    
                if (cmd.equals("RS")) { //reset button
                    for (int i = 0; i < Fieldnumber(flag) ; i++) {
    
                            compref[i].getboxref().setSelected(false);
                            compref[i].getcomboA1().setEnabled(false);
                            compref[i].getcomboA2().setEnabled(false);
                            compref[i].getcomboB1().setEnabled(false);
                            compref[i].getcomboB2().setEnabled(false);
                            compref[i].getcomboA1().setSelectedIndex(0);
                            compref[i].getcomboA2().setSelectedIndex(0);
                            compref[i].getcomboB1().setSelectedIndex(0);
                            compref[i].getcomboB2().setSelectedIndex(0);
                       if (flag.equals("DIALOG2"))  {
                           compref[i].gettextfieldref().setText("info of vacation etc");
                           compref[i].gettextfieldref().setEnabled(false);
                       }
                   }
                }
    
                if (cmd.equals("CH")) {
                  System.out.println("CHANCEL " + dialog2);
                  dialog2.setVisible(false);
                 // setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                }
    
                if (cmd.equals("OK")) {
    
                    if (flag.equals("DIALOG1"))  {
                        System.out.println("OK1 ");
                        if (!CheckDialogInput()) {
                                JOptionPane.showMessageDialog(null,
                                    "Please check the hour and minutes\n" +
                                    "fields and correct the entry/ies.\n");
                        } else {
    
                          // code to write still
                        }
                    }
    
                    if (flag.equals("DIALOG2"))  {
    
                        if (!CheckDialogInput()) {
                                JOptionPane.showMessageDialog(null,
                                    "Please check the month and day\n" +
                                    "fields and correct the entry/ies.\n" +
                                    "The info field should be filled out too.\n");
                        } else {
    
                               // code to write still   
                        }
                    }
    
                    if (flag.equals("DIALOG3"))  {
                        System.out.println("DIALOG3");
                    }
                }
            }
        }
    
    
        protected static ImageIcon createImageIcon(String path) { // loads the icons
            java.net.URL imgURL = CalendarPanel.class.getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
       }
    
    }

  6. #6
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: a concrete question

    the base class

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package studyrooms;
    
    /**
     *
     * @author willem
     */
     
    
    
    
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.io.File;
    import java.io.FileNotFoundException;
    import javax.swing.*;
    
    /**
     *
     * 
     */
    public class Main  {
        private CalendarPanel calpanel;
        private CalendarDialog dialog1, dialog2;
    
        public Main() throws FileNotFoundException      {
            
            if (lookForDatafile()) {
                System.out.println(" file found! ");
                calpanel = new CalendarPanel();
                calpanel.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
             //   deskmonitor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                calpanel.setVisible(true);
                calpanel.addWindowListener( new WindowAdapter()   {
                public void windowClosing(WindowEvent e)  {
                  calpanel = (CalendarPanel)(JFrame) e.getSource();
    
                    int result = JOptionPane.showConfirmDialog(
                        calpanel,
                            "Are you sure you want to exit the application?",
                            "Exit Application",
                            JOptionPane.YES_NO_OPTION);
    
                    if (result == JOptionPane.YES_OPTION) { // mayor clean up DeskMonitor
                        calpanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    //    calpanel.stoptimer();
                        System.out.println("timer t stopped ");
                    }
                }
                });
    
               
            }
            else  {
                System.out.println(" file NOT found! ");
                JOptionPane.showMessageDialog(null,
                          "The application looks for the file\n" +
                          "CalendarDataFile.txt\n" +
                          "if not found the that file, two dialogs\n" +
                          "will appear after each other to\n" +
                          "set the day-panels of the\n" +
                          "calendar screen");
                dialog1 = new CalendarDialog("DIALOG1");
                dialog2 = new CalendarDialog("DIALOG2");
                calpanel = new CalendarPanel();
              
                dialog1.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
                dialog1.setBounds(300, 300, 520, 350);
            //  dialog1.setModal(true);
                dialog1.setVisible(true);
           }      
        }
    
    
        public static void main(String[] args) throws FileNotFoundException   {
                Main main = new Main();
         
       }
    
    
       public boolean lookForDatafile()  {
            if(new File("CalendarDataFile.txt").exists())
                return true;
            else
                return false;
       }
    
    }

  7. #7
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: a concrete question

    and finally the calendar class (when you compile these classes you will see what I mean)

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package studyrooms;
    
    /**
     *
     * @author willem
     */
    
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.util.Calendar;
    import java.util.Locale;
    import javax.swing.*;
    import javax.swing.border.Border;
    
    /**
     *
     * 
     */
    public class CalendarPanel extends JFrame {
    
        private Font theFont, smallFont;
        private Color bluecolor, blackcolor;
        private Calendar cal, calprint;
        private MouseHandler Mhandler;
        private PanelData paneldata[];
        private String monthlabel[], days[];
        private int monthCons[];
        private int daycount, thisyear, maxvacationfields;
    
    
    
        CalendarPanel()  {
            cal = Calendar.getInstance(Locale.GERMANY);
            calprint = Calendar.getInstance(Locale.GERMANY);
            setBounds(30, 30, 1700, 900);
            setTitle("Calendar Window");
    
            bluecolor = Color.BLUE;
            blackcolor = Color.BLACK;
            theFont = new Font("SansSerif", Font.PLAIN, 14);
            smallFont = new Font("SansSerif", Font.PLAIN, 11);
    
            monthlabel = new String[] {"SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER",
            "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST"};
    
            days = new String[]  {"lunes", "martes", "miércoles", "jueves",
                "viernes", "sábado", "domingo"};
    
            monthCons = new int[] {Calendar.SEPTEMBER, Calendar.OCTOBER,
            Calendar.NOVEMBER, Calendar.DECEMBER, Calendar.JANUARY,
            Calendar.FEBRUARY, Calendar.MARCH, Calendar.APRIL,
            Calendar.MAY, Calendar.JUNE, Calendar.JULY,
            Calendar.AUGUST};
    
            Mhandler = new MouseHandler(); // mousehandler for the panels
    
            paneldata = new PanelData[370]; // initialize panel data array
                for (int i = 0; i < paneldata.length; i++) {
                        paneldata[i] = new PanelData();
                }
    
    
            daycount = 0; // this counts the days of the year for the panels
            // set the actual year as acedemic year year or year-1
            thisyear = cal.get(Calendar.YEAR);
            thisyear = thisyear-1;
            Container contentPane = getContentPane();
            contentPane.add(setupYearCalenderPanel(thisyear));
    
         //   System.out.println(" first year day (sunday=1) " + resetFirstLastDay(thisyear, 0));
         //   int maxday = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
         //   int firstday = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
         //   System.out.println(" max day of that month " + maxday);
         //   System.out.println(" first day of that month " + firstday);
        }
    
        public static void main(String[] args) {
            CalendarPanel frame = new CalendarPanel();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    
        public JPanel setupYearCalenderPanel(int year)  {
                // the 12 month of the year
                JPanel yearPanel = new JPanel(new GridLayout(3,4,2,2));
                yearPanel.setBackground(Color.WHITE);
                for (int i = 0; i < 12; i++) { // set the month cicle
                    if (i == 4) year = year +1; // shift for the acedemic year
                    yearPanel.add(setupSingleCalenderPanel(resetFirstLastDay(year, i), year, i));
                }
    
                return yearPanel;
        }
    
        public int resetFirstLastDay(int year, int month){ // month 0-11
            // the acedemic year runs from sept to sept
            // one year shift for the leap-year of the month feb
            year=year-1;
    
            calprint.set(year, monthCons[month], 1);
            int weekday = calprint.get(Calendar.DAY_OF_WEEK);
            int maxday = calprint.getActualMaximum(Calendar.DAY_OF_MONTH);
    
           // scaling for the calendar graph
            weekday = weekday + 5;
            if (weekday > 6) weekday = weekday - 7;
           // System.out.println(" year >" + year + " month number >" + month +
           //         " day of the week sunday=1 >" + weekday);
           // System.out.println("the mont " + month + " daynumber " + weekday +
           //         " last day of the month " + maxday);
            return weekday; // the first day of the month 0 = monday until 6
    
    
        }
    
         public int CalcDaynumber(int year, int month)  {
             int maxday = 0;
             month = month + 4; // shift for the acedemic year sept-sept
             if (month >= 11) month = month - 11;
             for (int i = 0; i < month; i++) {
                resetFirstLastDay(year, i); // this sets a particular date
                // this gives the max days of that particular month
                int x = calprint.getActualMaximum(Calendar.DAY_OF_MONTH);
                maxday = maxday + x;
            //    System.out.println(" number of tdays per month " + (i+1) + " > " + maxday);
             }
    
             return maxday;
         }
    
    
        public JPanel setupSingleCalenderPanel(int day, int year, int month)  {
            // for layout it is important that all of the grids get filled
            JPanel mainPanel = new JPanel(new BorderLayout());
            mainPanel.setBackground(Color.WHITE);
            JPanel monthPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
            monthPanel.setBackground(Color.WHITE);
    
    
            // first row, set the text of that particular month
            JLabel monthLabel = new JLabel(year + "/ " + monthlabel[month]);
            monthPanel.add(monthLabel);
            JPanel daysPanel = new JPanel(new GridLayout(7, 7, 1, 1));
            daysPanel.setBackground(Color.WHITE);
            JPanel dPanel;
            JLabel dayLabel;
                // sets the second row that contains the week days
            for (int i = 0; i < 7; i++) {
    
                dPanel = new JPanel(new GridLayout(1, 1));
                dPanel.setBackground(Color.WHITE);
             //   dPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
                dayLabel = new JLabel("", SwingConstants.CENTER);
                dayLabel.setBackground(Color.WHITE);
                dayLabel.setFont(smallFont);
              //  dayLabel.setVerticalTextPosition(JLabel.BOTTOM);
                dayLabel.setVerticalAlignment(SwingConstants.BOTTOM);
                dayLabel.setText("" + days[i]);
    
    
                dPanel.add(dayLabel);
                daysPanel.add(dPanel);
            }
    
                // the first empty month day panels
            for (int i = 0; i < day; i++) {
                dPanel = new JPanel();
                dPanel.setBackground(Color.WHITE);
                daysPanel.add(dPanel);
            }
                // set the actual month for getting the max day of that month
                // calculate the day panels and the rest panels to the fill the month
                // month 0-11, days 1-31 days of the week 1-7 sunday is 1
            resetFirstLastDay(year, month); // this sets a particular date
            // this gives the max days of that particular month
            int maxday = calprint.getActualMaximum(Calendar.DAY_OF_MONTH);
            int restday = 31 - maxday;
    
                // setup the actual daypanels of the month
            for (int i = 0; i < maxday; i++) {
                dPanel = new JPanel();
    
                dPanel.setBackground(Color.WHITE);
                daysPanel.add(dPanel);
                JLabel dateLabel = new JLabel("" + (i+1));
                dPanel.add(dateLabel);
                dPanel.addMouseListener(Mhandler);
                paneldata[daycount].setPanelRef(dPanel);
                paneldata[daycount].setDateString(monthlabel[month]
                                                        + "/ " + (i+1));
                daycount++;
                Border etched = BorderFactory.createEtchedBorder(blackcolor,
                                                    blackcolor);
                dPanel.setBorder(etched);
            }
    
            for (int i = 0; i < 11-day+restday; i++) {
                dPanel = new JPanel();
                dPanel.setBackground(Color.WHITE);
                daysPanel.add(dPanel);
    
            }
    
            mainPanel.add(monthPanel, BorderLayout.NORTH);
            mainPanel.add(daysPanel, BorderLayout.CENTER);
            Border etched = BorderFactory.createEtchedBorder(blackcolor,
                                                    blackcolor);
            mainPanel.setBorder(etched);
    
            return mainPanel;
        }
    
    
    
    
    
        private class PanelData { // the year array for each day
            private boolean dayset;
            private JPanel panelreference;
            private String datestring;
    
            public void setDayAvailable(boolean dt)  {
                    dayset = dt;
                }
            public boolean getDayAvailable()  {
                    return dayset;
                }
    
            public void setDateString(String st)  {
                    datestring = st;
                }
            public String getDateString()  {
                    return datestring;
                }
    
            public void setPanelRef(JPanel pr)  {
                    panelreference = pr;
                }
            public JPanel getPanelRef()  {
                    return panelreference;
                }
    
        }
    
    
    
    
        public class MouseHandler implements MouseListener {
    
            public void mousePressed(MouseEvent e) {
            }
    
            public void mouseReleased(MouseEvent e) {
            }
    
            public void mouseEntered(MouseEvent e) {
            }
    
            public void mouseExited(MouseEvent e) {
            }
    
            public void mouseClicked(MouseEvent e) {
                int indx = 0;
                if (e.getClickCount() >= 2) {
                    // roominfopanrl array
                    for (int i = 0; i < paneldata.length; i++) {
                       if (paneldata[i].getDayAvailable()) {
                          indx = i;
    
                       }
                       else {
                          indx = i;
                       }
    
                       if (e.getSource() == paneldata[i].getPanelRef()) {
                                System.out.println(paneldata[i].getDateString() +
                                        " day number " + i);
                       }
                    }
                    System.out.println(paneldata[indx].getDateString());
                }
            }
    
       }
    
    
    
    }

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default Re: a concrete question

    That String flag is static, i.e. there's only one flag for all the dialogs.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: a concrete question

    wow that is it, many thanks jos
    I made it static because if went over the main (static) method
    and forgot to take static out......
    (I am still learning)

  10. #10
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: a concrete question


  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default Re: a concrete question

    Quote Originally Posted by willemjav View Post
    wow that is it, many thanks jos
    I made it static because if went over the main (static) method
    and forgot to take static out......
    (I am still learning)
    Thou art forgiven but sin no more.

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: a concrete question

    here is probably the source of all our trouble (see chapter 17)

    http://people.umass.edu/klement/imp/imp-ebk.pdf

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default Re: a concrete question

    Quote Originally Posted by willemjav View Post
    here is probably the source of all our trouble (see chapter 17)

    http://people.umass.edu/klement/imp/imp-ebk.pdf
    Russell's classes are very much unlike Java's classes; they are algebraic classes, i.e. sets as subsets of a 'universal set'. He also defined sets in terms of itself, e.g. a class C doesn't contain itself as an element; let class UC be the set of all classes C; can UC contain itself? And that's Russell's paradox.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: a concrete question

    I am happy you found a russell's paradox
    and the fact that his classes are not java's classes
    encourage me to continue with java

    willemjav

  15. #15
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: a concrete question

    I passed a the reference of the Dialog's OK button to the Main class like this:

    Java Code:
    public JButton getOkButton() { // the reference of the OK and Chancel button
            return okb;
        }
    and did the following at the action listener at the Main class (which is finally working):

    Java Code:
     private class ButtonListener implements ActionListener {
            public void actionPerformed(ActionEvent e)  {
                String cmd = e.getActionCommand();
                Object source = e.getSource();
                    System.out.println("listener ");
                if (source == dialog1.getOkButton()) {
                    System.out.println("OK1 ");
                }
                 if (cmd.equals("CH1")) {
                  System.out.println("CHANCEL1 " + dialog1);
                  dialog1.setVisible(false);
                  dialog1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                }
    
                if (cmd.equals("CH2")) {
                  System.out.println("CHANCEL2 " + dialog2);
                  dialog2.setVisible(false);
                  dialog2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                }
    
                if (cmd.equals("OK1")) {
    
                        System.out.println("OK1 ");
                        if (!dialog1.CheckDialogInput()) {
                    //    if (false) {
                                JOptionPane.showMessageDialog(null,
                                    "Please check the hour and minutes\n" +
                                    "fields and correct the entry/ies.\n");
                        } else {
                            dialog1.setVisible(false);
                            dialog2.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
                            dialog2.setBounds(300, 300, 760, 520);
                            dialog2.setVisible(true);
                        }
                }
    
                 if (cmd.equals("OK2")) {
    
                        System.out.println("OK2 ");
                        if (!dialog2.CheckDialogInput()) {
                    //    if (false) {
                                JOptionPane.showMessageDialog(null,
                                    "Please check the hour and minutes\n" +
                                    "fields and correct the entry/ies.\n");
                        } else {
    
                             dialog2.setVisible(false);
                             calpanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                             calpanel.setVisible(true);
                             System.out.println("OK3 " + dialog2);
    
                        }
                }
    
            }
        }
    I only need to read out the dialogs next and paint the calendar panel

  16. #16
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: a concrete question

    oops what you people are doing .... it will take almost an hour to understand this code :/

  17. #17
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: a concrete question

    any suggestions to make things more simple, qazi?

Similar Threads

  1. Replies: 2
    Last Post: 05-25-2012, 04:00 PM
  2. Java Question [Beginner Question]
    By joker760 in forum New To Java
    Replies: 3
    Last Post: 12-13-2011, 04:01 PM
  3. question posted by indissa: library question.
    By Fubarable in forum New To Java
    Replies: 2
    Last Post: 11-18-2011, 01:14 AM
  4. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 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
  •