Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 12 FirstFirst 1234 ... LastLast
Results 26 to 50 of 284

Thread: library or component for Hijra date chooser

  1. #26

    Default Re: library for Hijra date chooser

    I searched a lot but in the end I did not understand where to put listener(); to close dateDialog and i test and put in the all block of my code one by one and i just see errors and or never open dateDialog and just i see in output print
    dateDialog : Focus Gained
    dateDialog : Focus Lost

    OR

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

    can we use WindowListener for dateDialog Focus Lost???
    Last edited by cnmeysam; April 26th, 2021 at 01:26 AM.

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: library for Hijra date chooser

    Does the JDialog need to be modal? Change it to non-modal and see if it works as desired.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (April 26th, 2021)

  4. #28

    Default Re: library for Hijra date chooser

    something like this?
    public void listener() {
            dateDialog.setModal(false);
    //    dialog.setVisible(true);
     
    //        pannel. 
            addFocusListener(new FocusAdapter() {
                @Override
                public void focusGained(java.awt.event.FocusEvent evt) {
                    dateDialogFocusGained(evt);
                }
     
                public void focusLost(java.awt.event.FocusEvent evt) {
                    dateDialogFocusLost(evt);
                }
     
                private void dateDialogFocusGained(FocusEvent evt) {
                    System.out.println("dateDialog : Focus Gained");
     
                }
     
                private void dateDialogFocusLost(FocusEvent evt) {
                    System.out.println("dateDialog : Focus Lost");
                    dateDialog.setVisible(false);
     
                }
            });
        }

  5. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: library for Hijra date chooser

    Did you change the JDialog to be non-modal? That is done in the constructor.

    What happens when that is done?

    Why do you have small methods that call other small methods?
    Why not do the work in the first method instead of calling the second method?
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (April 26th, 2021)

  7. #30

    Default Re: library for Hijra date chooser

    I apologize for wasting your time. I have learned programming with project-based tutorials such as the phone book, so I I do not understand some of the things you say and can not do properly. As a result, I could not do what you said. Can you do for me what you say in this class?
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Cursor;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.Frame;
    import java.awt.GridLayout;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.FocusAdapter;
    import java.awt.event.FocusEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowFocusListener;
    import java.text.Format;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.time.LocalDate;
    import java.time.LocalDateTime;
    import java.time.chrono.HijrahDate;
    import java.time.format.DateTimeFormatter;
    import java.util.Calendar;
    import java.util.Date;
     
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSpinner;
    import javax.swing.JTextField;
    import javax.swing.SpinnerNumberModel;
    import javax.swing.SwingConstants;
    import javax.swing.SwingUtilities;
    import javax.swing.border.LineBorder;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    import java.time.chrono.HijrahChronology;
    import java.util.Locale;
     
    public class Hijrah extends JTextField {
     
        String testMethod() {                  //  ADDED
     
            HijrahDate hijrahDate = HijrahDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            String formatted = formatter.format(hijrahDate); // 07/03/1439
            System.out.println("hijrahDate " + formatted);
            return formatted.toString(); //<<<<<< ADDED
        }
        private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd";
        private static final int DIALOG_WIDTH = 250;
        private static final int DIALOG_HEIGHT = 210;
     
        private SimpleDateFormat dateFormat;
        private DatePanel datePanel = null;
        private JDialog dateDialog = null;
     
        public Hijrah() {
            this(new Date());
     
        }
     
        public Hijrah(String dateFormatPattern, Date date) {
            this(date);
            DEFAULT_DATE_FORMAT = dateFormatPattern;
     
        }
     
        public Hijrah(Date date) {
            setDate(date);
            setEditable(false);
            setCursor(new Cursor(Cursor.HAND_CURSOR));
            addListeners();
     
        }
     
        public void listener() {
     
     
    //        pannel. 
            addFocusListener(new FocusAdapter() {
                @Override
                public void focusGained(java.awt.event.FocusEvent evt) {
                    dateDialogFocusGained(evt);
                }
     
     
                public void focusLost(java.awt.event.FocusEvent evt) {
                    dateDialogFocusLost(evt);
                }
     
     
                private void dateDialogFocusGained(FocusEvent evt) {
                    System.out.println("dateDialog : Focus Gained");
     
                }
     
                private void dateDialogFocusLost(FocusEvent evt) {
                    System.out.println("dateDialog : Focus Lost");
                    dateDialog.setVisible(false);
     
                }
            });
        }
     
        private void addListeners() {
            addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent paramMouseEvent) {
     
                    if (datePanel == null) {
                        datePanel = new DatePanel();
                    }
                    Point point = getLocationOnScreen();
                    point.y = point.y + 30;
                    showDateDialog(datePanel, point);
     
                }
            });
        }
     
        private void showDateDialog(DatePanel dateChooser, Point position) {
     
            Frame owner = (Frame) SwingUtilities
                    .getWindowAncestor(Hijrah.this);
            if (dateDialog == null || dateDialog.getOwner() != owner) {
                dateDialog = createDateDialog(owner, dateChooser);
            }
            dateDialog.setLocation(getAppropriateLocation(owner, position));
            dateDialog.setVisible(true);
     
     
        }
     
        private JDialog createDateDialog(Frame owner, JPanel contentPanel) {
            JDialog dialog = new JDialog(owner, "Date Selected", true);
            dialog.setUndecorated(true);
            dialog.getContentPane().add(contentPanel, BorderLayout.CENTER);
            dialog.pack();
            dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
     
            return dialog;
     
        }
     
        private Point getAppropriateLocation(Frame owner, Point position) {
            Point result = new Point(position);
            Point p = owner.getLocation();
            int offsetX = (position.x + DIALOG_WIDTH) - (p.x + owner.getWidth());
            int offsetY = (position.y + DIALOG_HEIGHT) - (p.y + owner.getHeight());
     
            if (offsetX > 0) {
                result.x -= offsetX;
            }
     
            if (offsetY > 0) {
                result.y -= offsetY;
            }
     
            return result;
        }
     
        private SimpleDateFormat getDefaultDateFormat() {
            if (dateFormat == null) {
                dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
            }
            return dateFormat;
        }
     
        public void setText(Date date) {
            setDate(date);
     
        }
     
        public void setDate(Date date) {
    //        super.setText(getDefaultDateFormat().format(date) + " " + testMethod());   //<<<<< Changed
              super.setText(testMethod());   //<<<<< Changed
     
        }
     
        public Date getDate() {
            try {
                return getDefaultDateFormat().parse(getText());
            } catch (ParseException e) {
                return new Date();
            }
        }
     
        private class DatePanel extends JPanel implements ChangeListener {
     
            int startYear = 1380;
            int lastYear = 3050;
     
            Color backGroundColor = Color.gray;
            Color palletTableColor = Color.white;
            Color todayBackColor = Color.orange;
            Color weekFontColor = Color.blue;
            Color dateFontColor = Color.black;
            Color weekendFontColor = Color.red;
     
            Color controlLineColor = Color.GREEN;
            Color controlTextColor = Color.white;
            Color controlMoonnameTextColor = Color.BLUE;
     
            JSpinner yearSpin;
            JSpinner monthSpin;
     
            JButton[][] daysButton = new JButton[6][7];
     
            DatePanel() {
                setLayout(new BorderLayout());
                setBorder(new LineBorder(backGroundColor, 2));
                setBackground(backGroundColor);
     
                JPanel topYearAndMonth = createYearAndMonthPanal();
                add(topYearAndMonth, BorderLayout.NORTH);
                JPanel centerWeekAndDay = createWeekAndDayPanal();
                add(centerWeekAndDay, BorderLayout.CENTER);
     
                reflushWeekAndDay();
            }
     
            private JPanel createYearAndMonthPanal() {
     
     
     
                Calendar cal = getCalendar();
                int currentYear = cal.get(Calendar.YEAR);
                int currentMonth = cal.get(Calendar.MONTH) + 1;
     
                JPanel panel = new JPanel();
                panel.setLayout(new FlowLayout());
                panel.setBackground(controlLineColor);
     
     
     
                JLabel yearLabel = new JLabel("Year");
                yearLabel.setForeground(controlTextColor);
                panel.add(yearLabel);
     
                yearSpin = new JSpinner(new SpinnerNumberModel(currentYear,
                        startYear, lastYear, 1));
                yearSpin.setPreferredSize(new Dimension(60, 30));
                yearSpin.setName("Year");
                yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####"));
                yearSpin.addChangeListener(this);
                panel.add(yearSpin);
     
     
                monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1,
                        12, 1));
                monthSpin.setPreferredSize(new Dimension(45, 30));
                monthSpin.setName("Month");
                monthSpin.addChangeListener(this);
     
                if((Integer)monthSpin.getValue()==1){
     
                JLabel monthNameLabel = new JLabel("محرم");
                monthNameLabel.setForeground(controlMoonnameTextColor);
                panel.add(monthNameLabel);
                }else if((Integer)monthSpin.getValue()==2){
     
                JLabel monthNameLabel = new JLabel("صفر");
                monthNameLabel.setForeground(controlMoonnameTextColor);
                panel.add(monthNameLabel);
                }else if((Integer)monthSpin.getValue()==3){
     
                JLabel monthNameLabel = new JLabel("ربیع*الاول");
                monthNameLabel.setForeground(controlMoonnameTextColor);
                panel.add(monthNameLabel);
                }
                else if((Integer)monthSpin.getValue()==4){
     
                JLabel monthNameLabel = new JLabel("ربیع*الثانی");
                monthNameLabel.setForeground(controlMoonnameTextColor);
                panel.add(monthNameLabel);
                }
                else if((Integer)monthSpin.getValue()==5){
     
                JLabel monthNameLabel = new JLabel("جمادی*الاول");
                monthNameLabel.setForeground(controlMoonnameTextColor);
                panel.add(monthNameLabel);
                }else if((Integer)monthSpin.getValue()==6){
     
                JLabel monthNameLabel = new JLabel("جمادی*الثانی");
                monthNameLabel.setForeground(controlMoonnameTextColor);
                panel.add(monthNameLabel);
                }
                else if((Integer)monthSpin.getValue()==7){
     
                JLabel monthNameLabel = new JLabel("رجب");
                monthNameLabel.setForeground(controlMoonnameTextColor);
                panel.add(monthNameLabel);
                }else if((Integer)monthSpin.getValue()==8){
     
                JLabel monthNameLabel = new JLabel("شعبان");
                monthNameLabel.setForeground(controlMoonnameTextColor);
                panel.add(monthNameLabel);
                }
                else if((Integer)monthSpin.getValue()==9){
     
                JLabel monthNameLabel = new JLabel("رمضان");
                monthNameLabel.setForeground(controlMoonnameTextColor);
                panel.add(monthNameLabel);
                }else if((Integer)monthSpin.getValue()==10){
     
                JLabel monthNameLabel = new JLabel("شوال");
                monthNameLabel.setForeground(controlMoonnameTextColor);
                panel.add(monthNameLabel);
                }else if((Integer)monthSpin.getValue()==11){
     
                JLabel monthNameLabel = new JLabel("ذیقعده");
                monthNameLabel.setForeground(controlMoonnameTextColor);
                panel.add(monthNameLabel);
                }else if((Integer)monthSpin.getValue()==12){
     
                JLabel monthNameLabel = new JLabel("ذیحجه");
                monthNameLabel.setForeground(controlMoonnameTextColor);
                panel.add(monthNameLabel);
                }
    //            monthSpin.addChangeListener(new ChangeListener() {
    //    @Override
    //    public void stateChanged(ChangeEvent e) {
    //        JLabel monthNameLabela = new JLabel();
    //        monthNameLabela.setText((String)monthSpin.getValue());
    //       
    //    }
    //});
     
     
     
                panel.add(monthSpin);
     
                JLabel monthLabel = new JLabel("Month");
                monthLabel.setForeground(controlTextColor);
                panel.add(monthLabel);
     
     
                return panel;
            }
     
            private JPanel createWeekAndDayPanal() {
                String colname[] = {"sun", "Mon", "Tu", "We", "Th", "Fr", "Sa"};
                JPanel panel = new JPanel();
                panel.setFont(new Font("Arial", Font.PLAIN, 10));
                panel.setLayout(new GridLayout(7, 7));
                panel.setBackground(Color.white);
     
                for (int i = 0; i < 7; i++) {
                    JLabel cell = new JLabel(colname[i]);
                    cell.setHorizontalAlignment(JLabel.CENTER);
    //                if (i == 0 || i == 6) {
                    if (i == 5) {
                        cell.setForeground(weekendFontColor);
                    } else {
                        cell.setForeground(weekFontColor);
                    }
                    panel.add(cell);
                }
     
                int actionCommandId = 0;
                for (int i = 0; i < 6; i++) {
                    for (int j = 0; j < 7; j++) {
                        JButton numBtn = new JButton();
                        numBtn.setBorder(null);
                        numBtn.setHorizontalAlignment(SwingConstants.CENTER);
                        numBtn.setActionCommand(String
                                .valueOf(actionCommandId));
                        numBtn.setBackground(palletTableColor);
                        numBtn.setForeground(dateFontColor);
                        numBtn.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent event) {
                                JButton source = (JButton) event.getSource();
                                if (source.getText().length() == 0) {
                                    return;
                                }
                                dayColorUpdate(true);
                                source.setForeground(todayBackColor);
                                int newDay = Integer.parseInt(source.getText());
                                Calendar cal = getCalendar();
                                cal.set(Calendar.DAY_OF_MONTH, newDay);
                                setDate(cal.getTime());
     
                                dateDialog.setVisible(false);
                            }
                        });
     
    //                    if (j == 0 || j == 6) {
                        if (j == 5) {
                            numBtn.setForeground(weekendFontColor);
                        } else {
                            numBtn.setForeground(dateFontColor);
                        }
                        daysButton[i][j] = numBtn;
                        panel.add(numBtn);
                        actionCommandId++;
                    }
                }
     
                return panel;
            }
     
            private Calendar getCalendar() {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(getDate());
     
                return calendar;
     
            }
     
            private int getSelectedYear() {
                return ((Integer) yearSpin.getValue()).intValue();
            }
     
            private int getSelectedMonth() {
                return ((Integer) monthSpin.getValue()).intValue();
            }
     
            private void dayColorUpdate(boolean isOldDay) {
                Calendar cal = getCalendar();
                int day = cal.get(Calendar.DAY_OF_MONTH);
                cal.set(Calendar.DAY_OF_MONTH, 1);
                int actionCommandId = day - 2 + cal.get(Calendar.DAY_OF_WEEK);
                int i = actionCommandId / 7;
                int j = actionCommandId % 7;
                if (isOldDay) {
                    daysButton[i][j].setForeground(dateFontColor);
                } else {
                    daysButton[i][j].setForeground(todayBackColor);
                }
     
            }
     
            private void reflushWeekAndDay() {
                Calendar cal = getCalendar();
                cal.set(Calendar.DAY_OF_MONTH, 1);
                int maxDayNo = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                int dayNo = 2 - cal.get(Calendar.DAY_OF_WEEK);
                for (int i = 0; i < 6; i++) {
                    for (int j = 0; j < 7; j++) {
                        String s = "";
                        if (dayNo >= 1 && dayNo <= maxDayNo) {
                            s = String.valueOf(dayNo);
                        }
                        daysButton[i][j].setText(s);
                        dayNo++;
                    }
                }
                dayColorUpdate(false);
            }
     
            public void stateChanged(ChangeEvent e) {
                dayColorUpdate(true);
     
                JSpinner source = (JSpinner) e.getSource();
                Calendar cal = getCalendar();
                if (source.getName().equals("Year")) {
                    cal.set(Calendar.YEAR, getSelectedYear());
                } else {
                    cal.set(Calendar.MONTH, getSelectedMonth() - 1);
                }
                setDate(cal.getTime());
                reflushWeekAndDay();
            }
     
        }
     
    }

  8. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: library for Hijra date chooser

            JDialog dialog = new JDialog(owner, "Date Selected", true);
    Read the API doc for the JDialog class and see what the 3 arguments that are passed to it are for.
    Change the arguments so that the JDialog is not modal.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #32

    Default Re: library for Hijra date chooser

    i use
            JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal
    now i don't know where i must use this
    public void listener() {
     
    //        pannel. 
            addFocusListener(new FocusAdapter() {
                @Override
                public void focusGained(java.awt.event.FocusEvent evt) {
                    dateDialogFocusGained(evt);
                }
     
     
                public void focusLost(java.awt.event.FocusEvent evt) {
                    dateDialogFocusLost(evt);
                }
     
     
                private void dateDialogFocusGained(FocusEvent evt) {
                    System.out.println("dateDialog : Focus Gained");
     
                }
     
                private void dateDialogFocusLost(FocusEvent evt) {
                    System.out.println("dateDialog : Focus Lost");
                    dateDialog.setVisible(false);
     
                }
            });
        }

  10. #33
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: library for Hijra date chooser

    where i must use this
    Replace the constructor that is already in the class with that new version.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #34

    Default Re: library for Hijra date chooser

    i use this
    private JDialog createDateDialog(Frame owner, JPanel contentPanel) {
            JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal
    //        JDialog dialog = new JDialog(owner, "Date Selected", true);
            dialog.setUndecorated(true);
            dialog.getContentPane().add(contentPanel, BorderLayout.CENTER);
            dialog.pack();
            dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
    listener();
            return dialog;
     
        }
    i see this
    https://www.w3schools.com/java/java_constructors.asp
    and use this
    public Hijrah() {
            this(new Date());
            listener();
        }
    but not working
    Last edited by cnmeysam; April 26th, 2021 at 06:31 PM.

  12. #35
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: library for Hijra date chooser

    What component(s) are the listeners being added to? There are at least 3 components in the GUI: the window, the textfield and the dialog box.
    For which component do you want to detect focus changes?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #36

    Default Re: library for Hijra date chooser

    for dialog box

  14. #37
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: library for Hijra date chooser

    not working
    for dialog box
    Is that where the add listener is called for?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #38

    Default Re: library for Hijra date chooser

    no it is not because i called before and not working
    private JDialog createDateDialog(Frame owner, JPanel contentPanel) {
            JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal
    //        JDialog dialog = new JDialog(owner, "Date Selected", true);
            dialog.setUndecorated(true);
            dialog.getContentPane().add(contentPanel, BorderLayout.CENTER);
            dialog.pack();
            dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
          listener();
            return dialog;
     
        }
    i add for window, the textfield and again not working
    Last edited by cnmeysam; April 26th, 2021 at 07:01 PM.

  16. #39
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: library for Hijra date chooser

    not working
    How does the listener() method add the focus listener to the JDialog object? How does the listener() method get access to the dialog object?
    There needs to be a call to the dialog's addFocusListener method.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #40

    Default Re: library for Hijra date chooser

    this is my listener
    public void listener() {
     
            setFocusable(true);
    //        pannel. 
            addFocusListener(new FocusAdapter() {
                @Override
                public void focusGained(java.awt.event.FocusEvent evt) {
                    dateDialogFocusGained(evt);
                }
     
                public void focusLost(java.awt.event.FocusEvent evt) {
                    dateDialogFocusLost(evt);
                }
     
                private void dateDialogFocusGained(FocusEvent evt) {
                    System.out.println("dateDialog : Focus Gained");
     
                }
     
                private void dateDialogFocusLost(FocusEvent evt) {
                    System.out.println("dateDialog : Focus Lost");
                    dateDialog.setVisible(false);
     
                }
     
            });
        }
    and use like this
    private JDialog createDateDialog(Frame owner, JPanel contentPanel) {
            JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal
    //        JDialog dialog = new JDialog(owner, "Date Selected", true);
            dialog.setUndecorated(true);
            dialog.getContentPane().add(contentPanel, BorderLayout.CENTER);
            dialog.pack();
            dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
          listener();
            return dialog;
     
        }

  18. #41
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: library for Hijra date chooser

    For what object is this method called
          addFocusListener(new FocusAdapter() {
    Since it doesn't have a specific object reference with it, then it is calling the method for the class that the listener() method is in.

    Look at these statements:
            dialog.setUndecorated(true);
            dialog.getContentPane().add(contentPanel, BorderLayout.CENTER);
            dialog.pack();
            dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
    They all call methods for the dialog object.

    That is what the add listener needs:
       dialog.addFocusListener(new FocusAdapter() {

    Get rid of the listener() method. Move its code to where it is needed.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #42

    Default Re: library for Hijra date chooser

    not working again
    private JDialog createDateDialog(Frame owner, JPanel contentPanel) {
            JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal
    //        JDialog dialog = new JDialog(owner, "Date Selected", true);
            dialog.setUndecorated(true);
            dialog.getContentPane().add(contentPanel, BorderLayout.CENTER);
            dialog.pack();
            dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
            dialog.addFocusListener(new FocusAdapter() {
     
                    @Override
                public void focusGained(java.awt.event.FocusEvent evt) {
                    dateDialogFocusGained(evt);
                }
     
                public void focusLost(java.awt.event.FocusEvent evt) {
                    dateDialogFocusLost(evt);
                }
     
                private void dateDialogFocusGained(FocusEvent evt) {
                    System.out.println("dateDialog : Focus Gained");
     
                }
     
                private void dateDialogFocusLost(FocusEvent evt) {
                    System.out.println("dateDialog : Focus Lost");
                    dateDialog.setVisible(false);
     
                }
     
            });
     
            return dialog;
     
        }

  20. #43
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: library for Hijra date chooser

    What actions do you take with the program?
    What is printed when the program executes?

    Please post all of the new code.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #44

    Default Re: library for Hijra date chooser

    nothing hapend
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Cursor;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.Frame;
    import java.awt.GridLayout;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.FocusAdapter;
    import java.awt.event.FocusEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowFocusListener;
    import java.text.Format;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.time.LocalDate;
    import java.time.LocalDateTime;
    import java.time.chrono.HijrahDate;
    import java.time.format.DateTimeFormatter;
    import java.util.Calendar;
    import java.util.Date;
     
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSpinner;
    import javax.swing.JTextField;
    import javax.swing.SpinnerNumberModel;
    import javax.swing.SwingConstants;
    import javax.swing.SwingUtilities;
    import javax.swing.border.LineBorder;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    import java.time.chrono.HijrahChronology;
    import java.util.Locale;
     
    public class Hijrah extends JTextField {
     
        String testMethod() {                  //  ADDED
            HijrahDate hijrahDate = HijrahDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            String formatted = formatter.format(hijrahDate); // 07/03/1439
            System.out.println("hijrahDate " + formatted);
            return formatted.toString(); //<<<<<< ADDED
        }
        private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd";
        private static final int DIALOG_WIDTH = 250;
        private static final int DIALOG_HEIGHT = 210;
     
        private SimpleDateFormat dateFormat;
        private DatePanel datePanel = null;
        private JDialog dateDialog = null;
     
        public Hijrah() {
            this(new Date());
    //        listener();
        }
     
        public Hijrah(String dateFormatPattern, Date date) {
            this(date);
            DEFAULT_DATE_FORMAT = dateFormatPattern;
     
        }
     
        public Hijrah(Date date) {
            setDate(date);
            setEditable(false);
            setCursor(new Cursor(Cursor.HAND_CURSOR));
            addListeners();
     
        }
     
        private void addListeners() {
            addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent paramMouseEvent) {
     
                    if (datePanel == null) {
                        datePanel = new DatePanel();
                    }
                    Point point = getLocationOnScreen();
                    point.y = point.y + 30;
                    showDateDialog(datePanel, point);
     
                }
            });
        }
     
        private void showDateDialog(DatePanel dateChooser, Point position) {
     
            Frame owner = (Frame) SwingUtilities.getWindowAncestor(Hijrah.this);
            if (dateDialog == null || dateDialog.getOwner() != owner) {
                dateDialog = createDateDialog(owner, dateChooser);
     
            }
            dateDialog.setLocation(getAppropriateLocation(owner, position));
            dateDialog.setVisible(true);
     
        }
     
        private JDialog createDateDialog(Frame owner, JPanel contentPanel) {
            JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal
    //        JDialog dialog = new JDialog(owner, "Date Selected", true);
            dialog.setUndecorated(true);
            dialog.getContentPane().add(contentPanel, BorderLayout.CENTER);
            dialog.pack();
            dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
     
            dialog.addFocusListener(new FocusAdapter() {
     
                @Override
                public void focusGained(java.awt.event.FocusEvent evt) {
                    dateDialogFocusGained(evt);
                }
     
                @Override
                public void focusLost(java.awt.event.FocusEvent evt) {
                    dateDialogFocusLost(evt);
                }
     
                private void dateDialogFocusGained(FocusEvent evt) {
                    System.out.println("dateDialog : Focus Gained");
     
                }
     
                private void dateDialogFocusLost(FocusEvent evt) {
                    System.out.println("dateDialog : Focus Lost");
                    dateDialog.setVisible(false);
     
                }
     
            });
     
            return dialog;
     
        }
     
        private Point getAppropriateLocation(Frame owner, Point position) {
            Point result = new Point(position);
            Point p = owner.getLocation();
            int offsetX = (position.x + DIALOG_WIDTH) - (p.x + owner.getWidth());
            int offsetY = (position.y + DIALOG_HEIGHT) - (p.y + owner.getHeight());
     
            if (offsetX > 0) {
                result.x -= offsetX;
            }
     
            if (offsetY > 0) {
                result.y -= offsetY;
            }
     
            return result;
        }
     
        private SimpleDateFormat getDefaultDateFormat() {
            if (dateFormat == null) {
                dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
            }
            return dateFormat;
        }
     
        public void setText(Date date) {
            setDate(date);
     
        }
     
        public void setDate(Date date) {
    //        super.setText(getDefaultDateFormat().format(date) + " " + testMethod());   //<<<<< Changed
            super.setText(testMethod());   //<<<<< Changed
     
        }
     
        public Date getDate() {
            try {
                return getDefaultDateFormat().parse(getText());
            } catch (ParseException e) {
                return new Date();
            }
        }
     
        private class DatePanel extends JPanel implements ChangeListener {
     
            int startYear = 1380;
            int lastYear = 3050;
     
            Color backGroundColor = Color.gray;
            Color palletTableColor = Color.white;
            Color todayBackColor = Color.orange;
            Color weekFontColor = Color.blue;
            Color dateFontColor = Color.black;
            Color weekendFontColor = Color.red;
     
            Color controlLineColor = Color.GREEN;
            Color controlTextColor = Color.white;
            Color controlMoonnameTextColor = Color.BLUE;
     
            JSpinner yearSpin;
            JSpinner monthSpin;
     
            JButton[][] daysButton = new JButton[6][7];
     
            DatePanel() {
                setLayout(new BorderLayout());
                setBorder(new LineBorder(backGroundColor, 2));
                setBackground(backGroundColor);
     
                JPanel topYearAndMonth = createYearAndMonthPanal();
                add(topYearAndMonth, BorderLayout.NORTH);
                JPanel centerWeekAndDay = createWeekAndDayPanal();
                add(centerWeekAndDay, BorderLayout.CENTER);
     
                reflushWeekAndDay();
            }
     
            private JPanel createYearAndMonthPanal() {
     
                Calendar cal = getCalendar();
                int currentYear = cal.get(Calendar.YEAR);
                int currentMonth = cal.get(Calendar.MONTH) + 1;
     
                JPanel panel = new JPanel();
                panel.setLayout(new FlowLayout());
                panel.setBackground(controlLineColor);
     
                JLabel yearLabel = new JLabel("Year");
                yearLabel.setForeground(controlTextColor);
                panel.add(yearLabel);
     
                yearSpin = new JSpinner(new SpinnerNumberModel(currentYear,
                        startYear, lastYear, 1));
                yearSpin.setPreferredSize(new Dimension(60, 30));
                yearSpin.setName("Year");
                yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####"));
                yearSpin.addChangeListener(this);
                panel.add(yearSpin);
     
                monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1,
                        12, 1));
                monthSpin.setPreferredSize(new Dimension(45, 30));
                monthSpin.setName("Month");
                monthSpin.addChangeListener(this);
     
                if ((Integer) monthSpin.getValue() == 1) {
     
                    JLabel monthNameLabel = new JLabel("محرم");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 2) {
     
                    JLabel monthNameLabel = new JLabel("صفر");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 3) {
     
                    JLabel monthNameLabel = new JLabel("ربیع*الاول");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 4) {
     
                    JLabel monthNameLabel = new JLabel("ربیع*الثانی");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 5) {
     
                    JLabel monthNameLabel = new JLabel("جمادی*الاول");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 6) {
     
                    JLabel monthNameLabel = new JLabel("جمادی*الثانی");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 7) {
     
                    JLabel monthNameLabel = new JLabel("رجب");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 8) {
     
                    JLabel monthNameLabel = new JLabel("شعبان");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 9) {
     
                    JLabel monthNameLabel = new JLabel("رمضان");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 10) {
     
                    JLabel monthNameLabel = new JLabel("شوال");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 11) {
     
                    JLabel monthNameLabel = new JLabel("ذیقعده");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 12) {
     
                    JLabel monthNameLabel = new JLabel("ذیحجه");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                }
    //            monthSpin.addChangeListener(new ChangeListener() {
    //    @Override
    //    public void stateChanged(ChangeEvent e) {
    //        JLabel monthNameLabela = new JLabel();
    //        monthNameLabela.setText((String)monthSpin.getValue());
    //       
    //    }
    //});
     
                panel.add(monthSpin);
     
                JLabel monthLabel = new JLabel("Month");
                monthLabel.setForeground(controlTextColor);
                panel.add(monthLabel);
     
                return panel;
            }
     
            private JPanel createWeekAndDayPanal() {
                String colname[] = {"sun", "Mon", "Tu", "We", "Th", "Fr", "Sa"};
                JPanel panel = new JPanel();
                panel.setFont(new Font("Arial", Font.PLAIN, 10));
                panel.setLayout(new GridLayout(7, 7));
                panel.setBackground(Color.white);
     
                for (int i = 0; i < 7; i++) {
                    JLabel cell = new JLabel(colname[i]);
                    cell.setHorizontalAlignment(JLabel.CENTER);
    //                if (i == 0 || i == 6) {
                    if (i == 5) {
                        cell.setForeground(weekendFontColor);
                    } else {
                        cell.setForeground(weekFontColor);
                    }
                    panel.add(cell);
                }
     
                int actionCommandId = 0;
                for (int i = 0; i < 6; i++) {
                    for (int j = 0; j < 7; j++) {
                        JButton numBtn = new JButton();
                        numBtn.setBorder(null);
                        numBtn.setHorizontalAlignment(SwingConstants.CENTER);
                        numBtn.setActionCommand(String
                                .valueOf(actionCommandId));
                        numBtn.setBackground(palletTableColor);
                        numBtn.setForeground(dateFontColor);
                        numBtn.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent event) {
                                JButton source = (JButton) event.getSource();
                                if (source.getText().length() == 0) {
                                    return;
                                }
                                dayColorUpdate(true);
                                source.setForeground(todayBackColor);
                                int newDay = Integer.parseInt(source.getText());
                                Calendar cal = getCalendar();
                                cal.set(Calendar.DAY_OF_MONTH, newDay);
                                setDate(cal.getTime());
     
                                dateDialog.setVisible(false);
                            }
                        });
     
    //                    if (j == 0 || j == 6) {
                        if (j == 5) {
                            numBtn.setForeground(weekendFontColor);
                        } else {
                            numBtn.setForeground(dateFontColor);
                        }
                        daysButton[i][j] = numBtn;
                        panel.add(numBtn);
                        actionCommandId++;
                    }
                }
     
                return panel;
            }
     
            private Calendar getCalendar() {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(getDate());
     
                return calendar;
     
            }
     
            private int getSelectedYear() {
                return ((Integer) yearSpin.getValue()).intValue();
            }
     
            private int getSelectedMonth() {
                return ((Integer) monthSpin.getValue()).intValue();
            }
     
            private void dayColorUpdate(boolean isOldDay) {
                Calendar cal = getCalendar();
                int day = cal.get(Calendar.DAY_OF_MONTH);
                cal.set(Calendar.DAY_OF_MONTH, 1);
                int actionCommandId = day - 2 + cal.get(Calendar.DAY_OF_WEEK);
                int i = actionCommandId / 7;
                int j = actionCommandId % 7;
                if (isOldDay) {
                    daysButton[i][j].setForeground(dateFontColor);
                } else {
                    daysButton[i][j].setForeground(todayBackColor);
                }
     
            }
     
            private void reflushWeekAndDay() {
                Calendar cal = getCalendar();
                cal.set(Calendar.DAY_OF_MONTH, 1);
                int maxDayNo = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                int dayNo = 2 - cal.get(Calendar.DAY_OF_WEEK);
                for (int i = 0; i < 6; i++) {
                    for (int j = 0; j < 7; j++) {
                        String s = "";
                        if (dayNo >= 1 && dayNo <= maxDayNo) {
                            s = String.valueOf(dayNo);
                        }
                        daysButton[i][j].setText(s);
                        dayNo++;
                    }
                }
                dayColorUpdate(false);
            }
     
            public void stateChanged(ChangeEvent e) {
                dayColorUpdate(true);
     
                JSpinner source = (JSpinner) e.getSource();
                Calendar cal = getCalendar();
                if (source.getName().equals("Year")) {
                    cal.set(Calendar.YEAR, getSelectedYear());
                } else {
                    cal.set(Calendar.MONTH, getSelectedMonth() - 1);
                }
                setDate(cal.getTime());
                reflushWeekAndDay();
            }
     
        }
     
    }
    Last edited by cnmeysam; April 26th, 2021 at 07:59 PM.

  22. #45
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: library for Hijra date chooser

    Look at this site: https://stackoverflow.com/questions/...ide-of-jdialog
    That code shows what listener to use in its code example. Try using that one.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #46

    Default Re: library for Hijra date chooser

    i use addFocusListener in in stackoverflow user addWindowFocusListener but how i must replace it in my code?

  24. #47
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: library for Hijra date chooser

    addFocusListener in in stackoverflow user addWindowFocusListener but how i must replace it in my code?
    Yes. Replace add... with addWindow... as shown in the link.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #48

    Default Re: library for Hijra date chooser

    i fixed whit this
    JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal
    //        JDialog dialog = new JDialog(owner, "Date Selected", true);
            dialog.setUndecorated(true);
            dialog.getContentPane().add(contentPanel, BorderLayout.CENTER);
            dialog.pack();
            dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
     
            dialog.addWindowFocusListener(new WindowFocusListener() {
                public void windowLostFocus(WindowEvent e) {
                    dialog.setVisible(false);
                }
     
                public void windowGainedFocus(WindowEvent e) {
                }
            });


    --- Update ---

    now how i must say start day started from Saturday in dialog box?? this is my all code
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Cursor;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.Frame;
    import java.awt.GridLayout;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.FocusAdapter;
    import java.awt.event.FocusEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowFocusListener;
    import java.text.Format;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.time.LocalDate;
    import java.time.LocalDateTime;
    import java.time.chrono.HijrahDate;
    import java.time.format.DateTimeFormatter;
    import java.util.Calendar;
    import java.util.Date;
     
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSpinner;
    import javax.swing.JTextField;
    import javax.swing.SpinnerNumberModel;
    import javax.swing.SwingConstants;
    import javax.swing.SwingUtilities;
    import javax.swing.border.LineBorder;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    import java.time.chrono.HijrahChronology;
    import java.util.Locale;
     
    public class Hijrah extends JTextField {
     
        String testMethod() {                  //  ADDED
            HijrahDate hijrahDate = HijrahDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            String formatted = formatter.format(hijrahDate); // 07/03/1439
            System.out.println("hijrahDate " + formatted);
            return formatted.toString(); //<<<<<< ADDED
        }
        private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd";
        private static final int DIALOG_WIDTH = 250;
        private static final int DIALOG_HEIGHT = 210;
     
        private SimpleDateFormat dateFormat;
        private DatePanel datePanel = null;
        private JDialog dateDialog = null;
     
        public Hijrah() {
            this(new Date());
    //        listener();
        }
     
        public Hijrah(String dateFormatPattern, Date date) {
            this(date);
            DEFAULT_DATE_FORMAT = dateFormatPattern;
     
        }
     
        public Hijrah(Date date) {
            setDate(date);
            setEditable(false);
            setCursor(new Cursor(Cursor.HAND_CURSOR));
            addListeners();
     
        }
     
        private void addListeners() {
            addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent paramMouseEvent) {
     
                    if (datePanel == null) {
                        datePanel = new DatePanel();
                    }
                    Point point = getLocationOnScreen();
                    point.y = point.y + 30;
                    showDateDialog(datePanel, point);
     
                }
            });
        }
     
        private void showDateDialog(DatePanel dateChooser, Point position) {
     
            Frame owner = (Frame) SwingUtilities.getWindowAncestor(Hijrah.this);
            if (dateDialog == null || dateDialog.getOwner() != owner) {
                dateDialog = createDateDialog(owner, dateChooser);
     
            }
            dateDialog.setLocation(getAppropriateLocation(owner, position));
            dateDialog.setVisible(true);
     
        }
     
        private JDialog createDateDialog(Frame owner, JPanel contentPanel) {
            JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal
    //        JDialog dialog = new JDialog(owner, "Date Selected", true);
            dialog.setUndecorated(true);
            dialog.getContentPane().add(contentPanel, BorderLayout.CENTER);
            dialog.pack();
            dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
     
            dialog.addWindowFocusListener(new WindowFocusListener() {
                public void windowLostFocus(WindowEvent e) {
                    dialog.setVisible(false);
                }
     
                public void windowGainedFocus(WindowEvent e) {
                }
            });
     
            return dialog;
     
        }
     
        private Point getAppropriateLocation(Frame owner, Point position) {
            Point result = new Point(position);
            Point p = owner.getLocation();
            int offsetX = (position.x + DIALOG_WIDTH) - (p.x + owner.getWidth());
            int offsetY = (position.y + DIALOG_HEIGHT) - (p.y + owner.getHeight());
     
            if (offsetX > 0) {
                result.x -= offsetX;
            }
     
            if (offsetY > 0) {
                result.y -= offsetY;
            }
     
            return result;
        }
     
        private SimpleDateFormat getDefaultDateFormat() {
            if (dateFormat == null) {
                dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
            }
            return dateFormat;
        }
     
        public void setText(Date date) {
            setDate(date);
     
        }
     
        public void setDate(Date date) {
    //        super.setText(getDefaultDateFormat().format(date) + " " + testMethod());   //<<<<< Changed
            super.setText(testMethod());   //<<<<< Changed
     
        }
     
        public Date getDate() {
            try {
                return getDefaultDateFormat().parse(getText());
            } catch (ParseException e) {
                return new Date();
            }
        }
     
        private class DatePanel extends JPanel implements ChangeListener {
     
            int startYear = 1380;
            int lastYear = 3050;
     
            Color backGroundColor = Color.gray;
            Color palletTableColor = Color.white;
            Color todayBackColor = Color.orange;
            Color weekFontColor = Color.blue;
            Color dateFontColor = Color.black;
            Color weekendFontColor = Color.red;
     
            Color controlLineColor = Color.GREEN;
            Color controlTextColor = Color.white;
            Color controlMoonnameTextColor = Color.BLUE;
     
            JSpinner yearSpin;
            JSpinner monthSpin;
     
            JButton[][] daysButton = new JButton[6][7];
     
            DatePanel() {
                setLayout(new BorderLayout());
                setBorder(new LineBorder(backGroundColor, 2));
                setBackground(backGroundColor);
     
                JPanel topYearAndMonth = createYearAndMonthPanal();
                add(topYearAndMonth, BorderLayout.NORTH);
                JPanel centerWeekAndDay = createWeekAndDayPanal();
                add(centerWeekAndDay, BorderLayout.CENTER);
     
                reflushWeekAndDay();
            }
     
            private JPanel createYearAndMonthPanal() {
     
                Calendar cal = getCalendar();
                int currentYear = cal.get(Calendar.YEAR);
                int currentMonth = cal.get(Calendar.MONTH) + 1;
     
                JPanel panel = new JPanel();
                panel.setLayout(new FlowLayout());
                panel.setBackground(controlLineColor);
     
                JLabel yearLabel = new JLabel("Year");
                yearLabel.setForeground(controlTextColor);
                panel.add(yearLabel);
     
                yearSpin = new JSpinner(new SpinnerNumberModel(currentYear,
                        startYear, lastYear, 1));
                yearSpin.setPreferredSize(new Dimension(60, 30));
                yearSpin.setName("Year");
                yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####"));
                yearSpin.addChangeListener(this);
                panel.add(yearSpin);
     
                monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1,
                        12, 1));
                monthSpin.setPreferredSize(new Dimension(45, 30));
                monthSpin.setName("Month");
                monthSpin.addChangeListener(this);
     
                if ((Integer) monthSpin.getValue() == 1) {
     
                    JLabel monthNameLabel = new JLabel("محرم");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 2) {
     
                    JLabel monthNameLabel = new JLabel("صفر");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 3) {
     
                    JLabel monthNameLabel = new JLabel("ربیع*الاول");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 4) {
     
                    JLabel monthNameLabel = new JLabel("ربیع*الثانی");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 5) {
     
                    JLabel monthNameLabel = new JLabel("جمادی*الاول");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 6) {
     
                    JLabel monthNameLabel = new JLabel("جمادی*الثانی");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 7) {
     
                    JLabel monthNameLabel = new JLabel("رجب");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 8) {
     
                    JLabel monthNameLabel = new JLabel("شعبان");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 9) {
     
                    JLabel monthNameLabel = new JLabel("رمضان");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 10) {
     
                    JLabel monthNameLabel = new JLabel("شوال");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 11) {
     
                    JLabel monthNameLabel = new JLabel("ذیقعده");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 12) {
     
                    JLabel monthNameLabel = new JLabel("ذیحجه");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                }
    //            monthSpin.addChangeListener(new ChangeListener() {
    //    @Override
    //    public void stateChanged(ChangeEvent e) {
    //        JLabel monthNameLabela = new JLabel();
    //        monthNameLabela.setText((String)monthSpin.getValue());
    //       
    //    }
    //});
     
                panel.add(monthSpin);
     
                JLabel monthLabel = new JLabel("Month");
                monthLabel.setForeground(controlTextColor);
                panel.add(monthLabel);
     
                return panel;
            }
     
            private JPanel createWeekAndDayPanal() {
                String colname[] = {"sun", "Mon", "Tu", "We", "Th", "Fr", "Sa"};
                JPanel panel = new JPanel();
                panel.setFont(new Font("Arial", Font.PLAIN, 10));
                panel.setLayout(new GridLayout(7, 7));
                panel.setBackground(Color.white);
     
                for (int i = 0; i < 7; i++) {
                    JLabel cell = new JLabel(colname[i]);
                    cell.setHorizontalAlignment(JLabel.CENTER);
    //                if (i == 0 || i == 6) {
                    if (i == 5) {
                        cell.setForeground(weekendFontColor);
                    } else {
                        cell.setForeground(weekFontColor);
                    }
                    panel.add(cell);
                }
     
                int actionCommandId = 0;
                for (int i = 0; i < 6; i++) {
                    for (int j = 0; j < 7; j++) {
                        JButton numBtn = new JButton();
                        numBtn.setBorder(null);
                        numBtn.setHorizontalAlignment(SwingConstants.CENTER);
                        numBtn.setActionCommand(String
                                .valueOf(actionCommandId));
                        numBtn.setBackground(palletTableColor);
                        numBtn.setForeground(dateFontColor);
                        numBtn.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent event) {
                                JButton source = (JButton) event.getSource();
                                if (source.getText().length() == 0) {
                                    return;
                                }
                                dayColorUpdate(true);
                                source.setForeground(todayBackColor);
                                int newDay = Integer.parseInt(source.getText());
                                Calendar cal = getCalendar();
                                cal.set(Calendar.DAY_OF_MONTH, newDay);
                                setDate(cal.getTime());
     
                                dateDialog.setVisible(false);
                            }
                        });
     
    //                    if (j == 0 || j == 6) {
                        if (j == 5) {
                            numBtn.setForeground(weekendFontColor);
                        } else {
                            numBtn.setForeground(dateFontColor);
                        }
                        daysButton[i][j] = numBtn;
                        panel.add(numBtn);
                        actionCommandId++;
                    }
                }
     
                return panel;
            }
     
            private Calendar getCalendar() {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(getDate());
     
                return calendar;
     
            }
     
            private int getSelectedYear() {
                return ((Integer) yearSpin.getValue()).intValue();
            }
     
            private int getSelectedMonth() {
                return ((Integer) monthSpin.getValue()).intValue();
            }
     
            private void dayColorUpdate(boolean isOldDay) {
                Calendar cal = getCalendar();
                int day = cal.get(Calendar.DAY_OF_MONTH);
                cal.set(Calendar.DAY_OF_MONTH, 1);
                int actionCommandId = day - 2 + cal.get(Calendar.DAY_OF_WEEK);
                int i = actionCommandId / 7;
                int j = actionCommandId % 7;
                if (isOldDay) {
                    daysButton[i][j].setForeground(dateFontColor);
                } else {
                    daysButton[i][j].setForeground(todayBackColor);
                }
     
            }
     
            private void reflushWeekAndDay() {
                Calendar cal = getCalendar();
                cal.set(Calendar.DAY_OF_MONTH, 1);
                int maxDayNo = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                int dayNo = 2 - cal.get(Calendar.DAY_OF_WEEK);
                for (int i = 0; i < 6; i++) {
                    for (int j = 0; j < 7; j++) {
                        String s = "";
                        if (dayNo >= 1 && dayNo <= maxDayNo) {
                            s = String.valueOf(dayNo);
                        }
                        daysButton[i][j].setText(s);
                        dayNo++;
                    }
                }
                dayColorUpdate(false);
            }
     
            public void stateChanged(ChangeEvent e) {
                dayColorUpdate(true);
     
                JSpinner source = (JSpinner) e.getSource();
                Calendar cal = getCalendar();
                if (source.getName().equals("Year")) {
                    cal.set(Calendar.YEAR, getSelectedYear());
                } else {
                    cal.set(Calendar.MONTH, getSelectedMonth() - 1);
                }
                setDate(cal.getTime());
                reflushWeekAndDay();
            }
     
        }
     
    }
    Last edited by cnmeysam; April 26th, 2021 at 08:12 PM.

  26. #49
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: library for Hijra date chooser

    start day started from Saturday
    Now you need to study the code to see how it sets the start day to Sunday. When you understand how the current code works, you should be able to change the start day.
    If you don't understand my answer, don't ignore it, ask a question.

  27. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (April 26th, 2021)

  28. #50

    Default Re: library for Hijra date chooser

    i fix start day and numbers of days of mon in button in jdialog
    now how i can say when user click on JSpinner moon or year jdialog buttons update?
    this is my code now
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Cursor;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.Frame;
    import java.awt.GridLayout;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.FocusAdapter;
    import java.awt.event.FocusEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowFocusListener;
    import java.text.Format;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.time.LocalDate;
    import java.time.LocalDateTime;
    import java.time.chrono.HijrahDate;
    import java.time.format.DateTimeFormatter;
    import java.util.Calendar;
    import java.util.Date;
     
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSpinner;
    import javax.swing.JTextField;
    import javax.swing.SpinnerNumberModel;
    import javax.swing.SwingConstants;
    import javax.swing.SwingUtilities;
    import javax.swing.border.LineBorder;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    import java.time.chrono.HijrahChronology;
    import java.util.Locale;
     
    public class Hijrah extends JTextField {
     
        String testMethod() {                  //  ADDED
            HijrahDate hijrahDate = HijrahDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            String formatted = formatter.format(hijrahDate); // 07/03/1439
            System.out.println("hijrahDate " + formatted);
            return formatted.toString(); //<<<<<< ADDED
        }
        private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd";
        private static final int DIALOG_WIDTH = 250;
        private static final int DIALOG_HEIGHT = 210;
     
        private SimpleDateFormat dateFormat;
        private DatePanel datePanel = null;
        private JDialog dateDialog = null;
     
        public Hijrah() {
            this(new Date());
        }
     
        public Hijrah(String dateFormatPattern, Date date) {
            this(date);
            DEFAULT_DATE_FORMAT = dateFormatPattern;
        }
     
        public Hijrah(Date date) {
            setDate(date);
            setEditable(false);
            setCursor(new Cursor(Cursor.HAND_CURSOR));
            addListeners();
        }
     
        private void addListeners() {
            addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent paramMouseEvent) {
                    if (datePanel == null) {
                        datePanel = new DatePanel();
                    }
                    Point point = getLocationOnScreen();
                    point.y = point.y + 30;
                    showDateDialog(datePanel, point);
                }
            });
        }
     
        private void showDateDialog(DatePanel dateChooser, Point position) {
            Frame owner = (Frame) SwingUtilities.getWindowAncestor(Hijrah.this);
            if (dateDialog == null || dateDialog.getOwner() != owner) {
                dateDialog = createDateDialog(owner, dateChooser);
     
            }
            dateDialog.setLocation(getAppropriateLocation(owner, position));
            dateDialog.setVisible(true);
        }
     
        private JDialog createDateDialog(Frame owner, JPanel contentPanel) {
            JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal
    //        JDialog dialog = new JDialog(owner, "Date Selected", true);
            dialog.setUndecorated(true);
            dialog.getContentPane().add(contentPanel, BorderLayout.CENTER);
            dialog.pack();
            dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
            dialog.addWindowFocusListener(new WindowFocusListener() {
                public void windowLostFocus(WindowEvent e) {
                    dialog.setVisible(false);
                }
     
                public void windowGainedFocus(WindowEvent e) {
                }
            });
            return dialog;
        }
     
        private Point getAppropriateLocation(Frame owner, Point position) {
            Point result = new Point(position);
            Point p = owner.getLocation();
            int offsetX = (position.x + DIALOG_WIDTH) - (p.x + owner.getWidth());
            int offsetY = (position.y + DIALOG_HEIGHT) - (p.y + owner.getHeight());
     
            if (offsetX > 0) {
                result.x -= offsetX;
            }
     
            if (offsetY > 0) {
                result.y -= offsetY;
            }
     
            return result;
        }
     
        private SimpleDateFormat getDefaultDateFormat() {
            if (dateFormat == null) {
                dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
            }
            return dateFormat;
        }
     
        public void setText(Date date) {
            setDate(date);
     
        }
     
        public void setDate(Date date) {
    //        super.setText(getDefaultDateFormat().format(date) + " " + testMethod());   //<<<<< Changed
            super.setText(testMethod());   //<<<<< Changed
     
        }
     
        public Date getDate() {
            try {
                return getDefaultDateFormat().parse(getText());
            } catch (ParseException e) {
                return new Date();
            }
        }
     
        private class DatePanel extends JPanel implements ChangeListener {
     
            int startYear = 1380;
            int lastYear = 3050;
     
            Color backGroundColor = Color.gray;
            Color palletTableColor = Color.white;
            Color todayBackColor = Color.orange;
            Color weekFontColor = Color.blue;
            Color dateFontColor = Color.black;
            Color weekendFontColor = Color.red;
     
            Color controlLineColor = Color.GREEN;
            Color controlTextColor = Color.white;
            Color controlMoonnameTextColor = Color.BLUE;
     
            JSpinner yearSpin;
            JSpinner monthSpin;
     
            JButton[][] daysButton = new JButton[6][7];
     
            DatePanel() {
                setLayout(new BorderLayout());
                setBorder(new LineBorder(backGroundColor, 2));
                setBackground(backGroundColor);
     
                JPanel topYearAndMonth = createYearAndMonthPanal();
                add(topYearAndMonth, BorderLayout.NORTH);
                JPanel centerWeekAndDay = createWeekAndDayPanal();
                add(centerWeekAndDay, BorderLayout.CENTER);
     
                reflushWeekAndDay();
            }
     
            private JPanel createYearAndMonthPanal() {
     
                Calendar cal = getCalendar();
                int currentYear = cal.get(Calendar.YEAR);
                int currentMonth = cal.get(Calendar.MONTH) + 1;
     
                JPanel panel = new JPanel();
                panel.setLayout(new FlowLayout());
                panel.setBackground(controlLineColor);
     
                JLabel yearLabel = new JLabel("Year");
                yearLabel.setForeground(controlTextColor);
                panel.add(yearLabel);
     
                yearSpin = new JSpinner(new SpinnerNumberModel(currentYear,
                        startYear, lastYear, 1));
                yearSpin.setPreferredSize(new Dimension(60, 30));
                yearSpin.setName("Year");
                yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####"));
                yearSpin.addChangeListener(this);
                panel.add(yearSpin);
     
                monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1,
                        12, 1));
                monthSpin.setPreferredSize(new Dimension(45, 30));
                monthSpin.setName("Month");
                monthSpin.addChangeListener(this);
     
                if ((Integer) monthSpin.getValue() == 1) {
     
                    JLabel monthNameLabel = new JLabel("محرم");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 2) {
     
                    JLabel monthNameLabel = new JLabel("صفر");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 3) {
     
                    JLabel monthNameLabel = new JLabel("ربیع*الاول");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 4) {
     
                    JLabel monthNameLabel = new JLabel("ربیع*الثانی");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 5) {
     
                    JLabel monthNameLabel = new JLabel("جمادی*الاول");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 6) {
     
                    JLabel monthNameLabel = new JLabel("جمادی*الثانی");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 7) {
     
                    JLabel monthNameLabel = new JLabel("رجب");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 8) {
     
                    JLabel monthNameLabel = new JLabel("شعبان");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 9) {
     
                    JLabel monthNameLabel = new JLabel("رمضان");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 10) {
     
                    JLabel monthNameLabel = new JLabel("شوال");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 11) {
     
                    JLabel monthNameLabel = new JLabel("ذیقعده");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                } else if ((Integer) monthSpin.getValue() == 12) {
     
                    JLabel monthNameLabel = new JLabel("ذیحجه");
                    monthNameLabel.setForeground(controlMoonnameTextColor);
                    panel.add(monthNameLabel);
                }
     
                panel.add(monthSpin);
     
                JLabel monthLabel = new JLabel("Month");
                monthLabel.setForeground(controlTextColor);
                panel.add(monthLabel);
     
                return panel;
            }
     
            private JPanel createWeekAndDayPanal() {
                String colname[] = {"Sa", "sun", "Mon", "Tu", "We", "Th", "Fr"};
                JPanel panel = new JPanel();
                panel.setFont(new Font("Arial", Font.PLAIN, 10));
                panel.setLayout(new GridLayout(7, 7));
                panel.setBackground(Color.white);
     
                for (int i = 0; i < 7; i++) {
                    JLabel cell = new JLabel(colname[i]);
                    cell.setHorizontalAlignment(JLabel.CENTER);
    //                if (i == 0 || i == 6) {
                    if (i == 6) {
                        cell.setForeground(weekendFontColor);
                    } else {
                        cell.setForeground(weekFontColor);
                    }
                    panel.add(cell);
                }
     
                int actionCommandId = 0;
                for (int i = 0; i < 6; i++) {
                    for (int j = 0; j < 7; j++) {
                        JButton numBtn = new JButton();
                        numBtn.setBorder(null);
                        numBtn.setHorizontalAlignment(SwingConstants.CENTER);
                        numBtn.setActionCommand(String.valueOf(actionCommandId));
                        numBtn.setBackground(palletTableColor);
                        numBtn.setForeground(dateFontColor);
                        numBtn.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent event) {
                                JButton source = (JButton) event.getSource();
                                if (source.getText().length() == 0) {
                                    return;
                                }
                                dayColorUpdate(true);
                                source.setForeground(todayBackColor);
                                int newDay = Integer.parseInt(source.getText());
                                Calendar cal = getCalendar();
                                cal.set(Calendar.DAY_OF_MONTH, newDay);
                                setDate(cal.getTime());
     
                                dateDialog.setVisible(false);
                            }
                        });
                        if (j == 6) {
                            numBtn.setForeground(weekendFontColor);
                        } else {
                            numBtn.setForeground(dateFontColor);
                        }
                        daysButton[i][j] = numBtn;
                        panel.add(numBtn);
                        actionCommandId++;
                    }
                }
     
                return panel;
            }
     
            private Calendar getCalendar() {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(getDate());
     
                return calendar;
     
            }
     
            private int getSelectedYear() {
                return ((Integer) yearSpin.getValue()).intValue();
            }
     
            private int getSelectedMonth() {
                return ((Integer) monthSpin.getValue()).intValue();
            }
     
            private void dayColorUpdate(boolean isOldDay) {
     
                Locale ar = new Locale("ar");
                DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar);
                DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar);
                HijrahDate hijrahDate = HijrahDate.now();
                String MoondayNum = hijrahDate.format(dayOfMoonNum);
                String WeekdayNum = hijrahDate.format(dayOfweekNum);
                int WeekdayNumb = Integer.parseInt(WeekdayNum);
                int moondayNumb = Integer.parseInt(MoondayNum);
                int moondaysnumber = hijrahDate.lengthOfMonth();
                System.out.println("hijrahDate= " + hijrahDate);
                System.out.println("Number day-of-month   " + MoondayNum);
                System.out.println("Number day-of-Week   " + WeekdayNum);
                System.out.println("Number of days of the month   " + moondaysnumber);
     
                int actionCommandId = moondayNumb - 2 + WeekdayNumb;
                int i = actionCommandId / 7;
                int j = actionCommandId % 7;
                if (isOldDay) {
                    daysButton[i][j].setForeground(dateFontColor);
                } else {
                    daysButton[i][j].setForeground(todayBackColor);
                }
     
            }
     
            private void reflushWeekAndDay() {
                Locale ar = new Locale("ar");
                DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar);
                DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar);
                HijrahDate hijrahDate = HijrahDate.now();
                String MoondayNum = hijrahDate.format(dayOfMoonNum);
                String WeekdayNum = hijrahDate.format(dayOfweekNum);
                int WeekdayNumb = Integer.parseInt(WeekdayNum);
                int moondayNumb = Integer.parseInt(MoondayNum);
                int moondaysnumber = hijrahDate.lengthOfMonth();
                System.out.println("hijrahDate= " + hijrahDate);
                System.out.println("Number day-of-month   " + moondayNumb);
                System.out.println("Number day-of-Week   " + WeekdayNumb);
                System.out.println("Number of days of the month   " + moondaysnumber);
     
                int dayNo = 2 - WeekdayNumb;
                for (int i = 0; i < 6; i++) {
                    for (int j = 0; j < 7; j++) {
                        String s = "";
                        if (dayNo >= 1 && dayNo <= moondaysnumber) {
                            s = String.valueOf(dayNo);
                        }
                        daysButton[i][j].setText(s);
                        dayNo++;
                    }
                }
                dayColorUpdate(false);
            }
     
            public void stateChanged(ChangeEvent e) {
                dayColorUpdate(true);
     
                JSpinner source = (JSpinner) e.getSource();
                Calendar cal = getCalendar();
                if (source.getName().equals("Year")) {
                    cal.set(Calendar.YEAR, getSelectedYear());
                } else {
                    cal.set(Calendar.MONTH, getSelectedMonth() - 1);
                }
                setDate(cal.getTime());
                reflushWeekAndDay();
            }
     
        }
     
    }
    Last edited by cnmeysam; April 26th, 2021 at 09:43 PM.

Page 2 of 12 FirstFirst 1234 ... LastLast

Similar Threads

  1. Splitting date string by date and time and assigning it to 2 variables
    By KaranSaxena in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 17th, 2014, 05:58 AM
  2. Start Date should be exactly 1 week before to End Date
    By bhanuchandar in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 26th, 2013, 08:07 AM
  3. Java Date Format in Date Object
    By Ashr Raza in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 13th, 2012, 10:47 AM
  4. Replies: 1
    Last Post: July 22nd, 2011, 07:08 AM