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 8 of 12 FirstFirst ... 678910 ... LastLast
Results 176 to 200 of 284

Thread: library or component for Hijra date chooser

  1. #176

    Default Re: library for Hijra date chooser

    we can't use this code
    for (int a = 0; a < 6; a++) {
                   for (int b = 0; b < 7; b++) {
                        daysButton[a][b].setForeground(dateFontColor);
                         if (daysButton[a][b].getText().equals(s)) {
                            daysButton[a][b].setForeground(todayBackColor);
                         }
                    }
                 daysButton[a][6].setForeground(weekendFontColor);
    because that time always week end turn red and never first time get orange

    --- Update ---

    where i must set print?

  2. #177
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    where i must set print?
    Next to the setForeground statement that sets the color orange.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #178

    Default Re: library for Hijra date chooser

    you can check my code now and test for see problem
    /*
     * Many thanks to Norm from the www.javaprogrammingforums.com that knew more than me.put more time and energy to create this component
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
     
    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.chrono.HijrahChronology;
    import java.time.chrono.HijrahDate;
    import java.time.format.DateTimeFormatter;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Locale;
     
    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;
     
    public class Hijrah extends JTextField {
     
        int ye;
        int mo;
        int da;
     
        String[] moonNames = {"ٱلْمُحَرَّم‎", "صَفَر‎", "رَبِيع ٱلْأَوَّل‎", "رَبِيع ٱلثَّانِي", "جُمَادَىٰ ٱلْأُولَىٰ", "جُمَادَىٰ ٱلثَّانِيَة", "رَجَب‎", "شَعْبَان‎", "رَمَضَان", "شَوَّال", "ذُو ٱلْقَعْدَة", "ذُو ٱلْحِجَّة"};
        //String[] moonNames = {"al-Muḥarram", "Ṣafar", "Rabīʿ al-ʾAwwal", "Rabīʿ ath-Thānī", "Jumadā al-ʾŪlā", "Jumādā ath-Thāniyah", "Rajab", "Shaʿbān", "Ramaḍān", "Shawwāl", "Ḏū al-Qaʿdah", "Ḏū al-Ḥijjah"};
        String colname[] = {"سب", "اح", "ثن", "ثل", "ار", "خم", "جم"};
    //            String colname[] = {"Sa", "sun", "Mon", "Tu", "We", "Th", "Fr"};
        JLabel cell;
        JLabel monthNameLabel = new JLabel();  // get name from array
     
        private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd";
        private static final int DIALOG_WIDTH = 270;
        private static final int DIALOG_HEIGHT = 210;
     
        private SimpleDateFormat dateFormat;
        private DatePanel datePanel = null;
        private JDialog dateDialog = null;
     
        public void hdate() {
            HijrahDate hijrahDate = HijrahDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            String formatted = formatter.format(hijrahDate); // 07/03/1439 
            ye = Integer.parseInt(formatted.substring(0, 4));
            mo = Integer.parseInt(formatted.substring(5, 7));
            da = Integer.parseInt(formatted.substring(8, 10));
        }
     
    //    public Hijrah() {
    //        this();
    //    }
        public Hijrah(String dateFormatPattern, Date date) {
            this();
            DEFAULT_DATE_FORMAT = dateFormatPattern;
        }
     
        public Hijrah() {
            hdate();
            setDate();
            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() {
            setDate();
        }
     
        public void setDate() {
            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 = 1;
            int lastYear = 99999;
     
            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 = new java.awt.Color(153, 0, 153);
            Color controlMoonnameTextColor = Color.BLUE;
     
            JSpinner yearSpin;
            JSpinner monthSpin;
            JButton[][] daysButton = new JButton[8][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() {
                Font font = new Font("Tahoma", Font.BOLD, 10);
                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("السنة");
                yearLabel.setForeground(controlTextColor);
                yearLabel.setHorizontalAlignment(SwingConstants.CENTER);
                yearLabel.setVerticalAlignment(SwingConstants.CENTER);
                yearLabel.setFont(font);
     
                panel.add(yearLabel);
                yearSpin = new JSpinner(new SpinnerNumberModel(currentYear,
                        startYear, lastYear, 1));
     
                yearSpin.setPreferredSize(new Dimension(60, 30));
                yearSpin.setName("Month");
                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("Year");
                monthSpin.addChangeListener(this);
                monthNameLabel.setPreferredSize(new Dimension(65, 20));
                monthNameLabel.setForeground(controlTextColor);
                monthNameLabel.setHorizontalAlignment(SwingConstants.CENTER);
                monthNameLabel.setVerticalAlignment(SwingConstants.CENTER);
                monthNameLabel.setFont(font);
                monthNameLabel.setText(moonNames[(Integer) monthSpin.getValue() - 1]);  // get name from array
                panel.add(monthNameLabel);
     
                panel.add(monthSpin);
     
                JLabel monthLabel = new JLabel("الشهر");
                monthLabel.setForeground(controlTextColor);
                monthLabel.setHorizontalAlignment(SwingConstants.CENTER);
                monthLabel.setVerticalAlignment(SwingConstants.CENTER);
                monthLabel.setFont(font);
                panel.add(monthLabel);
     
                return panel;
            }
     
            private JPanel createWeekAndDayPanal() {
     
                JPanel panel = new JPanel();
                panel.setFont(new Font("Tahoma", Font.PLAIN, 10));
                panel.setLayout(new GridLayout(7, 7));
                panel.setBackground(Color.white);
     
                for (int i = 0; i < 7; i++) {
                    cell = new JLabel(colname[i]);
                    cell.setHorizontalAlignment(JLabel.CENTER);
                    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) {
                                dayColorUpdate(true);
                                JButton source = (JButton) event.getSource();
                                if (source.getText().length() == 0) {
                                    numBtn.setForeground(dateFontColor);
     
                                    return;
                                }
                                System.out.println("source" + source.getText());
                                System.out.println("numBtn.getText();" + numBtn.getText());
     
                                numBtn.setBackground(palletTableColor);
                                numBtn.setForeground(dateFontColor);
                                source.setForeground(todayBackColor);
     
                                int newDay = Integer.parseInt(source.getText());
                                da = newDay;
     
                                setDate();
     
                                dateDialog.setVisible(false);
                            }
                        });
     
                        daysButton[i][j] = numBtn;
                        panel.add(numBtn);
                        actionCommandId++;
                        if (j == 6) {
                            numBtn.setForeground(weekendFontColor);
                        } else {
                            numBtn.setForeground(dateFontColor);
                        }
                    }
                }
     
                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) {
     
    //            ye = (Integer) yearSpin.getValue();
    //            mo = (Integer) monthSpin.getValue();
    //            System.out.println("spinner ye " + ye);
    //            System.out.println("spinner mo " + mo);
    //            Locale ar = new Locale("ar");
    //            DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar);
    //            DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar);
    //            HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(ye, mo, da);
    //            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);
    //                daysButton[i][6].setForeground(weekendFontColor);
    //            } else {
    //                daysButton[i][j].setForeground(dateFontColor);
    //                daysButton[i][6].setForeground(weekendFontColor);
    //                daysButton[i][j].setForeground(todayBackColor);
    //            }
                String s = String.valueOf(da);
                System.out.println("dayNumbdayNumbdayNumbdayNumb" + s);
                for (int a = 0; a < 6; a++) {
                    for (int b = 0; b < 7; b++) {
                        daysButton[a][b].setForeground(dateFontColor);
                        daysButton[a][6].setForeground(weekendFontColor);
                        if (daysButton[a][b].getText().equals(s)) {
                            daysButton[a][b].setForeground(todayBackColor);
                        }
                    }
                }
            }
     
            private void reflushWeekAndDay() {
     
                ye = (Integer) yearSpin.getValue();
                mo = (Integer) monthSpin.getValue();
                System.out.println("spinner ye " + ye);
                System.out.println("spinner mo " + mo);
     
                Locale ar = new Locale("ar");
                DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar);
                DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar);
                HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(ye, mo, da);
                HijrahChronology crono = hijrahDate.getChronology();
                HijrahDate date = crono.date(ye, mo, da);
                System.out.println("crono date= " + date);
                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 maxDayNo = moondaysnumber;
                System.out.println("Number of days of the month   " + moondaysnumber);
                int dayNo = -2 - WeekdayNumb;
    //            int dayNo = 0;
                System.out.println("dayNo   " + dayNo);
                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++;
                        if (daysButton[i][j].getText().equals("")) {
                            daysButton[i][j].setVisible(false);
                        } else {
                            daysButton[i][j].setVisible(true);
     
                        }
     
                    }
     
                }
                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();
                reflushWeekAndDay();
                mothnam();
            }
     
        }
     
        public void mothnam() {
            monthNameLabel.setText(moonNames[mo - 1]);
        }
     
        String testMethod() {                  //  ADDED
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            HijrahDate today = HijrahChronology.INSTANCE.date(ye, mo, da);
            String formatted = formatter.format(today); // 07/03/1439
    //        System.out.println("hijrahDate " + formatted);
            return formatted.toString(); //<<<<<< ADDED
        }
     
    }


    --- Update ---

    i must print what?

    --- Update ---

    I think we should change the value of the number da to the day it was clicked
    Last edited by cnmeysam; April 30th, 2021 at 02:21 PM.

  4. #179
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    i must print what?
    Start with: a, b and s
    If you don't understand my answer, don't ignore it, ask a question.

  5. #180

    Default Re: library for Hijra date chooser

    Its value of a and b is always one step behind
    after load
    aaaaaaa=3
    bbbbbbb=6
    after click
    aaaaaaa=3
    bbbbbbb=6
    after secound click
    aaaaaaa=2
    bbbbbbb=6

    --- Update ---

    it's fix now this part of code must be changed
    dayColorUpdate(true); must go to end of actionPerformed
    public void actionPerformed(ActionEvent event) {
     
                                JButton source = (JButton) event.getSource();
                                System.out.println("sourcesourcesourcesource" + source);
                                if (source.getText().length() == 0) {
                                    numBtn.setForeground(dateFontColor);
     
                                    return;
                                }
     
                                numBtn.setBackground(palletTableColor);
                                numBtn.setForeground(dateFontColor);
                                source.setForeground(todayBackColor);
     
                                int newDay = Integer.parseInt(source.getText());
                                da = newDay;
     
                                setDate();
                                dayColorUpdate(true);
                                dateDialog.setVisible(false);
                            }
                        });


    --- Update ---

    i think To make the program better, if the other 1 items change, it will be much better

    Instead of clicking on the text and opening the jdialog, add a button to do so
    now how i must do this?
    Last edited by cnmeysam; April 29th, 2021 at 06:09 PM.

  6. #181

    Default Re: library for Hijra date chooser

    how i can add a button after JTextField and check JTextField is empty?
     
    /*
     * Many thanks to Norm from the www.javaprogrammingforums.com that knew more than me.put more time and energy to create this component 
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
     
    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.chrono.HijrahChronology;
    import java.time.chrono.HijrahDate;
    import java.time.format.DateTimeFormatter;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Locale;
     
    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;
     
    public class Hijrah extends JTextField {
     
        int ye;
        int mo;
        int da;
        String days;
        String[] moonNames = {"ٱلْمُحَرَّم‎", "صَفَر‎", "رَبِيع ٱلْأَوَّل‎", "رَبِيع ٱلثَّانِي", "جُمَادَىٰ ٱلْأُولَىٰ", "جُمَادَىٰ ٱلثَّانِيَة", "رَجَب‎", "شَعْبَان‎", "رَمَضَان", "شَوَّال", "ذُو ٱلْقَعْدَة", "ذُو ٱلْحِجَّة"};
        //String[] moonNames = {"al-Muḥarram", "Ṣafar", "Rabīʿ al-ʾAwwal", "Rabīʿ ath-Thānī", "Jumadā al-ʾŪlā", "Jumādā ath-Thāniyah", "Rajab", "Shaʿbān", "Ramaḍān", "Shawwāl", "Ḏū al-Qaʿdah", "Ḏū al-Ḥijjah"};
        String colname[] = {"سب", "اح", "ثن", "ثل", "ار", "خم", "جم"};
    //            String colname[] = {"Sa", "sun", "Mon", "Tu", "We", "Th", "Fr"};
        JLabel cell;
        JLabel monthNameLabel = new JLabel();  // get name from array
     
        private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd";
        private static final int DIALOG_WIDTH = 270;
        private static final int DIALOG_HEIGHT = 210;
     
        private SimpleDateFormat dateFormat;
        private DatePanel datePanel = null;
        private JDialog dateDialog = null;
     
        public void hdate() {
            HijrahDate hijrahDate = HijrahDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            String formatted = formatter.format(hijrahDate); // 07/03/1439 
            ye = Integer.parseInt(formatted.substring(0, 4));
            mo = Integer.parseInt(formatted.substring(5, 7));
            da = Integer.parseInt(formatted.substring(8, 10));
        }
     
    //    public Hijrah() {
    //        this();
    //    }
        public Hijrah(String dateFormatPattern, Date date) {
            this();
            DEFAULT_DATE_FORMAT = dateFormatPattern;
        }
     
        public Hijrah() {
            hdate();
            setDate();
    //        setEditable(false);
            setCursor(new Cursor(Cursor.HAND_CURSOR));
            addListeners();
        }
     
        private void addListeners() {
            addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent paramMouseEvent) {
                     days = String.valueOf(da);
                    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() {
            setDate();
        }
     
        public void setDate() {
            super.setText(testMethod());   //<<<<< Changed
        }
     
        public Date getDate() {
     
            try {
                return getDefaultDateFormat().parse(getText());
            } catch (ParseException e) {
                return new Date();
            }
        }
     
        void setDate(int i) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
        private class DatePanel extends JPanel implements ChangeListener {
     
            int startYear = 1;
            int lastYear = 99999;
     
            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 = new java.awt.Color(153, 0, 153);
            Color controlMoonnameTextColor = Color.BLUE;
     
            JSpinner yearSpin;
            JSpinner monthSpin;
            JButton[][] daysButton = new JButton[8][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() {
                Font font = new Font("Tahoma", Font.BOLD, 10);
                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("السنّة");
                yearLabel.setForeground(controlTextColor);
                yearLabel.setHorizontalAlignment(SwingConstants.CENTER);
                yearLabel.setVerticalAlignment(SwingConstants.CENTER);
                yearLabel.setFont(font);
     
                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);
                monthNameLabel.setPreferredSize(new Dimension(65, 20));
                monthNameLabel.setForeground(controlTextColor);
                monthNameLabel.setHorizontalAlignment(SwingConstants.CENTER);
                monthNameLabel.setVerticalAlignment(SwingConstants.CENTER);
                monthNameLabel.setFont(font);
                monthNameLabel.setText(moonNames[(Integer) monthSpin.getValue() - 1]);  // get name from array
                panel.add(monthNameLabel);
     
                panel.add(monthSpin);
     
                JLabel monthLabel = new JLabel("الشَّهْر");
                monthLabel.setForeground(controlTextColor);
                monthLabel.setHorizontalAlignment(SwingConstants.CENTER);
                monthLabel.setVerticalAlignment(SwingConstants.CENTER);
                monthLabel.setFont(font);
                panel.add(monthLabel);
     
                return panel;
            }
     
            private JPanel createWeekAndDayPanal() {
     
                JPanel panel = new JPanel();
                panel.setFont(new Font("Tahoma", Font.PLAIN, 10));
                panel.setLayout(new GridLayout(7, 7));
                panel.setBackground(Color.white);
     
                for (int i = 0; i < 7; i++) {
                    cell = new JLabel(colname[i]);
                    cell.setHorizontalAlignment(JLabel.CENTER);
                    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) {
                                    numBtn.setForeground(dateFontColor);
     
                                    return;
                                }
     
                                numBtn.setBackground(palletTableColor);
                                numBtn.setForeground(dateFontColor);
                                source.setForeground(todayBackColor);
     
                                int newDay = Integer.parseInt(source.getText());
                                da = newDay;
    //                            reflushWeekAndDay();
                                setDate();
                                dayColorUpdate(true);
                                dateDialog.setVisible(false);
                            }
                        });
     
                        daysButton[i][j] = numBtn;
                        panel.add(numBtn);
                        actionCommandId++;
                        if (j == 6) {
                            numBtn.setForeground(weekendFontColor);
                        } else {
                            numBtn.setForeground(dateFontColor);
                        }
                    }
                }
     
                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) {
     
                days = String.valueOf(da);
                for (int a = 0; a < 6; a++) {
                    for (int b = 0; b < 7; b++) {
                        daysButton[a][b].setForeground(dateFontColor);
                        daysButton[a][6].setForeground(weekendFontColor);
     
                        if (daysButton[a][b].getText().equals(days)) {
     
                            daysButton[a][b].setForeground(todayBackColor);
     
                        }
                    }
                }
            }
     
            private void reflushWeekAndDay() {
     
                ye = (Integer) yearSpin.getValue();
                mo = (Integer) monthSpin.getValue();
     
                Locale ar = new Locale("ar");
     
                DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar);
                HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(ye, mo, da);
     
                String WeekdayNum = hijrahDate.format(dayOfweekNum);
                int WeekdayNumb = Integer.parseInt(WeekdayNum);
     
                int moondaysnumber = hijrahDate.lengthOfMonth();
     
                int maxDayNo = 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 <= maxDayNo) {
                            s = String.valueOf(dayNo);
                        }
                        daysButton[i][j].setText(s);
                        dayNo++;
                        if (daysButton[i][j].getText().equals("")) {
                            daysButton[i][j].setVisible(false);
                        } else {
                            daysButton[i][j].setVisible(true);
     
                        }
     
                    }
     
                }
                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);
                }
                reflushWeekAndDay();
                setDate();
     
                mothnam();
            }
     
        }
     
        public void mothnam() {
            monthNameLabel.setText(moonNames[mo - 1]);
        }
     
        String testMethod() {                  //  ADDED
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            HijrahDate today = HijrahChronology.INSTANCE.date(ye, mo, da);
            String formatted = formatter.format(today); // 07/03/1439
     
            return formatted.toString(); //<<<<<< ADDED
        }
     
    }
    Last edited by cnmeysam; April 30th, 2021 at 02:26 PM.

  7. #182
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    You could change the class to extend JPanel and add the JTextField and JButton to the JPanel.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #183

    Default Re: library for Hijra date chooser

    i extend JPanel like this but i don't know it's true or not and create JButton choosdate = new JButton(); now i don't know how add jbutton after text
    /*
     * Many thanks to Norm from the www.javaprogrammingforums.com that knew more than me.put more time and energy to create this component 
     * Maysam Soleymani Qorabaee www.programsfuture.com
     * and open the template in the editor.
     */
     
    import newpackage.*;
    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.chrono.HijrahChronology;
    import java.time.chrono.HijrahDate;
    import java.time.format.DateTimeFormatter;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Locale;
     
    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 javax.swing.JPanel;
     
    /*
     * Many thanks to Norm from the www.javaprogrammingforums.com that knew more than me.put more time and energy to create this component 
     * Maysam Soleymani Qorabaee www.programsfuture.com  * 
     */
    /**
     *
     * @author Admin
     */
    public class Hijrah extends JPanel {
     
        int ye;
        int mo;
        int da;
     
        String[] moonNames = {"ٱلْمُحَرَّم‎", "صَفَر‎", "رَبِيع ٱلْأَوَّل‎", "رَبِيع ٱلثَّانِي", "جُمَادَىٰ ٱلْأُولَىٰ", "جُمَادَىٰ ٱلثَّانِيَة", "رَجَب‎", "شَعْبَان‎", "رَمَضَان", "شَوَّال", "ذُو ٱلْقَعْدَة", "ذُو ٱلْحِجَّة"};
        //String[] moonNames = {"al-Muḥarram", "Ṣafar", "Rabīʿ al-ʾAwwal", "Rabīʿ ath-Thānī", "Jumadā al-ʾŪlā", "Jumādā ath-Thāniyah", "Rajab", "Shaʿbān", "Ramaḍān", "Shawwāl", "Ḏū al-Qaʿdah", "Ḏū al-Ḥijjah"};
        String colname[] = {"سب", "اح", "ثن", "ثل", "ار", "خم", "جم"};
    //            String colname[] = {"Sa", "sun", "Mon", "Tu", "We", "Th", "Fr"};
        JLabel cell;
        JLabel monthNameLabel = new JLabel();  // get name from array
     
        JTextField datetext = new JTextField();
        JButton choosdate = new JButton();
     
        private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd";
        private static final int DIALOG_WIDTH = 270;
        private static final int DIALOG_HEIGHT = 210;
     
        private SimpleDateFormat dateFormat;
        private Hijrah.DatePanel datePanel = null;
        private JDialog dateDialog = null;
     
        public void hdate() {
            HijrahDate hijrahDate = HijrahDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            String formatted = formatter.format(hijrahDate); // 07/03/1439 
            ye = Integer.parseInt(formatted.substring(0, 4));
            mo = Integer.parseInt(formatted.substring(5, 7));
            da = Integer.parseInt(formatted.substring(8, 10));
        }
     
        public Hijrah(String dateFormatPattern, Date date) {
            this();
            DEFAULT_DATE_FORMAT = dateFormatPattern;
        }
     
        public Hijrah() {
            hdate();
            setDate();
    //        datetext.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() {
            setDate();
        }
     
        public void setDate() {
            datetext.setText(testMethod());   //<<<<< Changed
        }
         public Date getDate() {
     
            try {
                return getDefaultDateFormat().parse(datetext.getText());
            } catch (ParseException e) {
                return new Date();
            }
        }
     
     
        private class DatePanel extends JPanel implements ChangeListener {
     
            int startYear = 1;
            int lastYear = 99999;
     
            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 = new java.awt.Color(153, 0, 153);
            Color controlMoonnameTextColor = Color.BLUE;
     
            JSpinner yearSpin;
            JSpinner monthSpin;
            JButton[][] daysButton = new JButton[8][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() {
                Font font = new Font("Tahoma", Font.BOLD, 10);
                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("السنة");
                yearLabel.setForeground(controlTextColor);
                yearLabel.setHorizontalAlignment(SwingConstants.CENTER);
                yearLabel.setVerticalAlignment(SwingConstants.CENTER);
                yearLabel.setFont(font);
     
                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);
                monthNameLabel.setPreferredSize(new Dimension(65, 20));
                monthNameLabel.setForeground(controlTextColor);
                monthNameLabel.setHorizontalAlignment(SwingConstants.CENTER);
                monthNameLabel.setVerticalAlignment(SwingConstants.CENTER);
                monthNameLabel.setFont(font);
                monthNameLabel.setText(moonNames[(Integer) monthSpin.getValue() - 1]);  // get name from array
                panel.add(monthNameLabel);
     
                panel.add(monthSpin);
     
                JLabel monthLabel = new JLabel("الشهر");
                monthLabel.setForeground(controlTextColor);
                monthLabel.setHorizontalAlignment(SwingConstants.CENTER);
                monthLabel.setVerticalAlignment(SwingConstants.CENTER);
                monthLabel.setFont(font);
                panel.add(monthLabel);
     
                return panel;
            }
     
            private JPanel createWeekAndDayPanal() {
     
                JPanel panel = new JPanel();
                panel.setFont(new Font("Tahoma", Font.PLAIN, 10));
                panel.setLayout(new GridLayout(7, 7));
                panel.setBackground(Color.white);
     
                for (int i = 0; i < 7; i++) {
                    cell = new JLabel(colname[i]);
                    cell.setHorizontalAlignment(JLabel.CENTER);
                    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) {
                                    numBtn.setForeground(dateFontColor);
     
                                    return;
                                }
     
                                numBtn.setBackground(palletTableColor);
                                numBtn.setForeground(dateFontColor);
                                source.setForeground(todayBackColor);
     
                                int newDay = Integer.parseInt(source.getText());
                                da = newDay;
     
                                setDate();
                                dayColorUpdate(true);
                                dateDialog.setVisible(false);
                            }
                        });
     
                        daysButton[i][j] = numBtn;
                        panel.add(numBtn);
                        actionCommandId++;
                        if (j == 6) {
                            numBtn.setForeground(weekendFontColor);
                        } else {
                            numBtn.setForeground(dateFontColor);
                        }
                    }
                }
     
                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) {
     
                String s = String.valueOf(da);
                for (int a = 0; a < 6; a++) {
                    for (int b = 0; b < 7; b++) {
                        daysButton[a][b].setForeground(dateFontColor);
                        daysButton[a][6].setForeground(weekendFontColor);
     
                        if (daysButton[a][b].getText().equals(s)) {
     
                            daysButton[a][b].setForeground(todayBackColor);
     
                        }
                    }
                }
            }
     
            private void reflushWeekAndDay() {
     
                ye = (Integer) yearSpin.getValue();
                mo = (Integer) monthSpin.getValue();
     
                Locale ar = new Locale("ar");
     
                DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar);
                HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(ye, mo, da);
     
                String WeekdayNum = hijrahDate.format(dayOfweekNum);
                int WeekdayNumb = Integer.parseInt(WeekdayNum);
     
                int moondaysnumber = hijrahDate.lengthOfMonth();
     
                int maxDayNo = 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 <= maxDayNo) {
                            s = String.valueOf(dayNo);
                        }
                        daysButton[i][j].setText(s);
                        dayNo++;
                        if (daysButton[i][j].getText().equals("")) {
                            daysButton[i][j].setVisible(false);
                        } else {
                            daysButton[i][j].setVisible(true);
     
                        }
     
                    }
     
                }
                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);
                }
                reflushWeekAndDay();
                setDate();
     
                mothnam();
            }
     
        }
     
        public void mothnam() {
            monthNameLabel.setText(moonNames[mo - 1]);
        }
     
        String testMethod() {                  //  ADDED
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            HijrahDate today = HijrahChronology.INSTANCE.date(ye, mo, da);
            String formatted = formatter.format(today); // 07/03/1439
     
            return formatted.toString(); //<<<<<< ADDED
        }
     
     
    }
    Last edited by cnmeysam; April 30th, 2021 at 03:32 PM.

  9. #184
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    how add jbutton after text
    Look at the API doc for the layout managers. FlowLayout is the simplest.
    Or maybe your IDE will write the java code for adding the JTextField and JButton to a JPanel.
    Look at what was done in the NewJFrame class.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #185

    Default Re: library for Hijra date chooser

    I could not find where the text was added to the extended panel So I can't add the button next to it
    I also do not know how to add FlowLayout to the extended panel
    Because the codes in the extended panel is different from with new JPanel

  11. #186
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    Look at your code; there are examples of the use of the FlowLayout manager.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #187

    Default Re: library for Hijra date chooser

    JPanel panel = new JPanel();
                panel.setLayout(new FlowLayout());
    i change this part of code but not working
    public Hijrah() {
    setPreferredSize(new Dimension(640, 480));
           setLayout(new FlowLayout()); //for FlowLayout panel
            hdate();
     
            add(choosdate);//for add button to panel
            setDate();
    //        datetext.setEditable(false);
            setCursor(new Cursor(Cursor.HAND_CURSOR));
            addListeners();
        }
    Last edited by cnmeysam; April 30th, 2021 at 04:14 PM.

  13. #188
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    but not working
    Please explain.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #189

    Default Re: library for Hijra date chooser

    i need add button after text but i cant i change JTextField extends to jpannel extends and in public Hijrah() i setLayout for pannel and create a button and add in public Hijrah() but button com after jtextfild it is my all code but not woking and i can't see button after text
     
    /*
     * Many thanks to Norm from the www.javaprogrammingforums.com that knew more than me.put more time and energy to create this component 
     * Maysam Soleymani Qorabaee www.programsfuture.com  * 
     */
     
    /**
     *
     * @author Admin
     */
    import newpackage.*;
    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.Image;
    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.io.IOException;
    import java.text.Format;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.time.LocalDate;
    import java.time.chrono.HijrahChronology;
    import java.time.chrono.HijrahDate;
    import java.time.format.DateTimeFormatter;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Locale;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
     
    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 javax.swing.JPanel;
     
    public class Hijrah extends JPanel {
     
        int ye;
        int mo;
        int da;
     
        String[] moonNames = {"ٱلْمُحَرَّم‎", "صَفَر‎", "رَبِيع ٱلْأَوَّل‎", "رَبِيع ٱلثَّانِي", "جُمَادَىٰ ٱلْأُولَىٰ", "جُمَادَىٰ ٱلثَّانِيَة", "رَجَب‎", "شَعْبَان‎", "رَمَضَان", "شَوَّال", "ذُو ٱلْقَعْدَة", "ذُو ٱلْحِجَّة"};
        //String[] moonNames = {"al-Muḥarram", "Ṣafar", "Rabīʿ al-ʾAwwal", "Rabīʿ ath-Thānī", "Jumadā al-ʾŪlā", "Jumādā ath-Thāniyah", "Rajab", "Shaʿbān", "Ramaḍān", "Shawwāl", "Ḏū al-Qaʿdah", "Ḏū al-Ḥijjah"};
        String colname[] = {"سب", "اح", "ثن", "ثل", "ار", "خم", "جم"};
    //            String colname[] = {"Sa", "sun", "Mon", "Tu", "We", "Th", "Fr"};
        JLabel cell;
        JLabel monthNameLabel = new JLabel();  // get name from array
     
        Image img;
     
        private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd";
        private static final int DIALOG_WIDTH = 270;
        private static final int DIALOG_HEIGHT = 210;
     
        private SimpleDateFormat dateFormat;
        private Hijrah.DatePanel datePanel = null;
        private JDialog dateDialog = null;
     
        private JButton choosdate;
        private JTextField datetext;
     
        public void hdate() {
            HijrahDate hijrahDate = HijrahDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            String formatted = formatter.format(hijrahDate); // 07/03/1439 
            ye = Integer.parseInt(formatted.substring(0, 4));
            mo = Integer.parseInt(formatted.substring(5, 7));
            da = Integer.parseInt(formatted.substring(8, 10));
        }
     
        public Hijrah(String dateFormatPattern, Date date) {
            this();
            DEFAULT_DATE_FORMAT = dateFormatPattern;
        }
     
        public Hijrah() {
            setPreferredSize(new Dimension(640, 480));
            setLayout(new FlowLayout());
            hdate();
            try {
                img = ImageIO.read(getClass().getResource("cleander.png"));
                choosdate.setIcon(new ImageIcon(img));
            } catch (IOException ex) {
            }
     
            setDate();
    //        datetext.setEditable(false);
            setCursor(new Cursor(Cursor.HAND_CURSOR));
            addListeners();
            add(choosdate);
        }
     
        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() {
            setDate();
        }
     
        public void setDate() {
            datetext.setText(testMethod());   //<<<<< Changed
     
        }
     
        public Date getDate() {
     
            try {
                return getDefaultDateFormat().parse(datetext.getText());
            } catch (ParseException e) {
                return new Date();
            }
        }
     
        private class DatePanel extends JPanel implements ChangeListener {
     
            int startYear = 1;
            int lastYear = 99999;
     
            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 = new java.awt.Color(153, 0, 153);
            Color controlMoonnameTextColor = Color.BLUE;
     
            JSpinner yearSpin;
            JSpinner monthSpin;
            JButton[][] daysButton = new JButton[8][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() {
                Font font = new Font("Tahoma", Font.BOLD, 10);
                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("السنة");
                yearLabel.setForeground(controlTextColor);
                yearLabel.setHorizontalAlignment(SwingConstants.CENTER);
                yearLabel.setVerticalAlignment(SwingConstants.CENTER);
                yearLabel.setFont(font);
     
                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);
                monthNameLabel.setPreferredSize(new Dimension(65, 20));
                monthNameLabel.setForeground(controlTextColor);
                monthNameLabel.setHorizontalAlignment(SwingConstants.CENTER);
                monthNameLabel.setVerticalAlignment(SwingConstants.CENTER);
                monthNameLabel.setFont(font);
                monthNameLabel.setText(moonNames[(Integer) monthSpin.getValue() - 1]);  // get name from array
                panel.add(monthNameLabel);
     
                panel.add(monthSpin);
     
                JLabel monthLabel = new JLabel("الشهر");
                monthLabel.setForeground(controlTextColor);
                monthLabel.setHorizontalAlignment(SwingConstants.CENTER);
                monthLabel.setVerticalAlignment(SwingConstants.CENTER);
                monthLabel.setFont(font);
                panel.add(monthLabel);
     
                return panel;
            }
     
            private JPanel createWeekAndDayPanal() {
     
                JPanel panel = new JPanel();
                panel.setFont(new Font("Tahoma", Font.PLAIN, 10));
                panel.setLayout(new GridLayout(7, 7));
                panel.setBackground(Color.white);
     
                for (int i = 0; i < 7; i++) {
                    cell = new JLabel(colname[i]);
                    cell.setHorizontalAlignment(JLabel.CENTER);
                    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) {
                                    numBtn.setForeground(dateFontColor);
     
                                    return;
                                }
     
                                numBtn.setBackground(palletTableColor);
                                numBtn.setForeground(dateFontColor);
                                source.setForeground(todayBackColor);
     
                                int newDay = Integer.parseInt(source.getText());
                                da = newDay;
     
                                setDate();
                                dayColorUpdate(true);
                                dateDialog.setVisible(false);
                            }
                        });
     
                        daysButton[i][j] = numBtn;
                        panel.add(numBtn);
                        actionCommandId++;
                        if (j == 6) {
                            numBtn.setForeground(weekendFontColor);
                        } else {
                            numBtn.setForeground(dateFontColor);
                        }
                    }
                }
     
                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) {
     
                String s = String.valueOf(da);
                for (int a = 0; a < 6; a++) {
                    for (int b = 0; b < 7; b++) {
                        daysButton[a][b].setForeground(dateFontColor);
                        daysButton[a][6].setForeground(weekendFontColor);
     
                        if (daysButton[a][b].getText().equals(s)) {
     
                            daysButton[a][b].setForeground(todayBackColor);
     
                        }
                    }
                }
            }
     
            private void reflushWeekAndDay() {
     
                ye = (Integer) yearSpin.getValue();
                mo = (Integer) monthSpin.getValue();
     
                Locale ar = new Locale("ar");
     
                DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar);
                HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(ye, mo, da);
     
                String WeekdayNum = hijrahDate.format(dayOfweekNum);
                int WeekdayNumb = Integer.parseInt(WeekdayNum);
     
                int moondaysnumber = hijrahDate.lengthOfMonth();
     
                int maxDayNo = 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 <= maxDayNo) {
                            s = String.valueOf(dayNo);
                        }
                        daysButton[i][j].setText(s);
                        dayNo++;
                        if (daysButton[i][j].getText().equals("")) {
                            daysButton[i][j].setVisible(false);
                        } else {
                            daysButton[i][j].setVisible(true);
     
                        }
     
                    }
     
                }
                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);
                }
                reflushWeekAndDay();
                setDate();
     
                mothnam();
            }
     
        }
     
        public void mothnam() {
            monthNameLabel.setText(moonNames[mo - 1]);
        }
     
        String testMethod() {                  //  ADDED
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            HijrahDate today = HijrahChronology.INSTANCE.date(ye, mo, da);
            String formatted = formatter.format(today); // 07/03/1439
     
            return formatted.toString(); //<<<<<< ADDED
        }
     
    }

  15. #190
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    Where do you add the text field and the button to the panel?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #191

    Default Re: library for Hijra date chooser

    i add button here
    public Hijrah() {
            setPreferredSize(new Dimension(640, 480));
            setLayout(new FlowLayout());
            hdate();
            try {
                img = ImageIO.read(getClass().getResource("cleander.png"));
                choosdate.setIcon(new ImageIcon(img));
            } catch (IOException ex) {
            }
     
            setDate();
    //        datetext.setEditable(false);
            setCursor(new Cursor(Cursor.HAND_CURSOR));
            addListeners();
            add(choosdate);// this is my button
        }
    but i cant find where textfild add in extended panel

  17. #192
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    where textfild add
    Add the textfield Before the button if you want it to the left of the button
    Add the textfield After the button if you want it to the right of the button
    If you don't understand my answer, don't ignore it, ask a question.

  18. #193

    Default Re: library for Hijra date chooser

    but i can't find textfild for add there ( not working)
    public Hijrah() {
            setPreferredSize(new Dimension(640, 480));
            setLayout(new FlowLayout());
            hdate();
            try {
                img = ImageIO.read(getClass().getResource("cleander.png"));
                choosdate.setIcon(new ImageIcon(img));
            } catch (IOException ex) {
            }
     
            setDate();
    //        datetext.setEditable(false);
            setCursor(new Cursor(Cursor.HAND_CURSOR));
            addListeners();
            add(choosdate)//add button
            add(datetext);//add text
        }


    --- Update ---

    can you add for me?

    --- Update ---

    /*
     * Many thanks to Norm from the www.javaprogrammingforums.com that knew more than me.put more time and energy to create this component 
     * Maysam Soleymani Qorabaee www.programsfuture.com  * 
     */
     
    /**
     *
     * @author Admin
     */
    import newpackage.*;
    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.Image;
    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.io.IOException;
    import java.text.Format;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.time.LocalDate;
    import java.time.chrono.HijrahChronology;
    import java.time.chrono.HijrahDate;
    import java.time.format.DateTimeFormatter;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Locale;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
     
    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 javax.swing.JPanel;
     
    public class Hijrah extends JPanel {
     
        int ye;
        int mo;
        int da;
     
        String[] moonNames = {"ٱلْمُحَرَّم‎", "صَفَر‎", "رَبِيع ٱلْأَوَّل‎", "رَبِيع ٱلثَّانِي", "جُمَادَىٰ ٱلْأُولَىٰ", "جُمَادَىٰ ٱلثَّانِيَة", "رَجَب‎", "شَعْبَان‎", "رَمَضَان", "شَوَّال", "ذُو ٱلْقَعْدَة", "ذُو ٱلْحِجَّة"};
        //String[] moonNames = {"al-Muḥarram", "Ṣafar", "Rabīʿ al-ʾAwwal", "Rabīʿ ath-Thānī", "Jumadā al-ʾŪlā", "Jumādā ath-Thāniyah", "Rajab", "Shaʿbān", "Ramaḍān", "Shawwāl", "Ḏū al-Qaʿdah", "Ḏū al-Ḥijjah"};
        String colname[] = {"سب", "اح", "ثن", "ثل", "ار", "خم", "جم"};
    //            String colname[] = {"Sa", "sun", "Mon", "Tu", "We", "Th", "Fr"};
        JLabel cell;
        JLabel monthNameLabel = new JLabel();  // get name from array
     
        Image img;
     
        private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd";
        private static final int DIALOG_WIDTH = 270;
        private static final int DIALOG_HEIGHT = 210;
     
        private SimpleDateFormat dateFormat;
        private Hijrah.DatePanel datePanel = null;
        private JDialog dateDialog = null;
     
        private JButton choosdate;
        private JTextField datetext;
     
        public void hdate() {
            HijrahDate hijrahDate = HijrahDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            String formatted = formatter.format(hijrahDate); // 07/03/1439 
            ye = Integer.parseInt(formatted.substring(0, 4));
            mo = Integer.parseInt(formatted.substring(5, 7));
            da = Integer.parseInt(formatted.substring(8, 10));
        }
     
        public Hijrah(String dateFormatPattern, Date date) {
            this();
            DEFAULT_DATE_FORMAT = dateFormatPattern;
        }
     
        public Hijrah() {
            setPreferredSize(new Dimension(640, 480));
            setLayout(new FlowLayout());
            hdate();
            try {
                img = ImageIO.read(getClass().getResource("cleander.png"));
                choosdate.setIcon(new ImageIcon(img));
            } catch (IOException ex) {
            }
     
            setDate();
    //        datetext.setEditable(false);
            setCursor(new Cursor(Cursor.HAND_CURSOR));
            addListeners();
            add(choosdate);
            add(datetext);
        }
     
        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() {
            setDate();
        }
     
        public void setDate() {
            datetext.setText(testMethod());   //<<<<< Changed
     
        }
     
        public Date getDate() {
     
            try {
                return getDefaultDateFormat().parse(datetext.getText());
            } catch (ParseException e) {
                return new Date();
            }
        }
     
        private class DatePanel extends JPanel implements ChangeListener {
     
            int startYear = 1;
            int lastYear = 99999;
     
            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 = new java.awt.Color(153, 0, 153);
            Color controlMoonnameTextColor = Color.BLUE;
     
            JSpinner yearSpin;
            JSpinner monthSpin;
            JButton[][] daysButton = new JButton[8][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() {
                Font font = new Font("Tahoma", Font.BOLD, 10);
                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("السنة");
                yearLabel.setForeground(controlTextColor);
                yearLabel.setHorizontalAlignment(SwingConstants.CENTER);
                yearLabel.setVerticalAlignment(SwingConstants.CENTER);
                yearLabel.setFont(font);
     
                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);
                monthNameLabel.setPreferredSize(new Dimension(65, 20));
                monthNameLabel.setForeground(controlTextColor);
                monthNameLabel.setHorizontalAlignment(SwingConstants.CENTER);
                monthNameLabel.setVerticalAlignment(SwingConstants.CENTER);
                monthNameLabel.setFont(font);
                monthNameLabel.setText(moonNames[(Integer) monthSpin.getValue() - 1]);  // get name from array
                panel.add(monthNameLabel);
     
                panel.add(monthSpin);
     
                JLabel monthLabel = new JLabel("الشهر");
                monthLabel.setForeground(controlTextColor);
                monthLabel.setHorizontalAlignment(SwingConstants.CENTER);
                monthLabel.setVerticalAlignment(SwingConstants.CENTER);
                monthLabel.setFont(font);
                panel.add(monthLabel);
     
                return panel;
            }
     
            private JPanel createWeekAndDayPanal() {
     
                JPanel panel = new JPanel();
                panel.setFont(new Font("Tahoma", Font.PLAIN, 10));
                panel.setLayout(new GridLayout(7, 7));
                panel.setBackground(Color.white);
     
                for (int i = 0; i < 7; i++) {
                    cell = new JLabel(colname[i]);
                    cell.setHorizontalAlignment(JLabel.CENTER);
                    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) {
                                    numBtn.setForeground(dateFontColor);
     
                                    return;
                                }
     
                                numBtn.setBackground(palletTableColor);
                                numBtn.setForeground(dateFontColor);
                                source.setForeground(todayBackColor);
     
                                int newDay = Integer.parseInt(source.getText());
                                da = newDay;
     
                                setDate();
                                dayColorUpdate(true);
                                dateDialog.setVisible(false);
                            }
                        });
     
                        daysButton[i][j] = numBtn;
                        panel.add(numBtn);
                        actionCommandId++;
                        if (j == 6) {
                            numBtn.setForeground(weekendFontColor);
                        } else {
                            numBtn.setForeground(dateFontColor);
                        }
                    }
                }
     
                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) {
     
                String s = String.valueOf(da);
                for (int a = 0; a < 6; a++) {
                    for (int b = 0; b < 7; b++) {
                        daysButton[a][b].setForeground(dateFontColor);
                        daysButton[a][6].setForeground(weekendFontColor);
     
                        if (daysButton[a][b].getText().equals(s)) {
     
                            daysButton[a][b].setForeground(todayBackColor);
     
                        }
                    }
                }
            }
     
            private void reflushWeekAndDay() {
     
                ye = (Integer) yearSpin.getValue();
                mo = (Integer) monthSpin.getValue();
     
                Locale ar = new Locale("ar");
     
                DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar);
                HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(ye, mo, da);
     
                String WeekdayNum = hijrahDate.format(dayOfweekNum);
                int WeekdayNumb = Integer.parseInt(WeekdayNum);
     
                int moondaysnumber = hijrahDate.lengthOfMonth();
     
                int maxDayNo = 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 <= maxDayNo) {
                            s = String.valueOf(dayNo);
                        }
                        daysButton[i][j].setText(s);
                        dayNo++;
                        if (daysButton[i][j].getText().equals("")) {
                            daysButton[i][j].setVisible(false);
                        } else {
                            daysButton[i][j].setVisible(true);
     
                        }
     
                    }
     
                }
                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);
                }
                reflushWeekAndDay();
                setDate();
     
                mothnam();
            }
     
        }
     
        public void mothnam() {
            monthNameLabel.setText(moonNames[mo - 1]);
        }
     
        String testMethod() {                  //  ADDED
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            HijrahDate today = HijrahChronology.INSTANCE.date(ye, mo, da);
            String formatted = formatter.format(today); // 07/03/1439
     
            return formatted.toString(); //<<<<<< ADDED
        }
     
    }

  19. #194
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    That looks right. What happens when that code is compiled and executed?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #195

    Default Re: library for Hijra date chooser

    nothing just show jtext and inside of text

    /*
     * Many thanks to Norm from the www.javaprogrammingforums.com that knew more than me.put more time and energy to create this component 
     * Maysam Soleymani Qorabaee www.programsfuture.com  * 
     */
     
    /**
     *
     * @author Admin
     */
    import newpackage.*;
    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.Image;
    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.io.IOException;
    import java.text.Format;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.time.LocalDate;
    import java.time.chrono.HijrahChronology;
    import java.time.chrono.HijrahDate;
    import java.time.format.DateTimeFormatter;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Locale;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
     
    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 javax.swing.JPanel;
     
    public class Hijrah extends JPanel {
     
        int ye;
        int mo;
        int da;
     
        String[] moonNames = {"ٱلْمُحَرَّم‎", "صَفَر‎", "رَبِيع ٱلْأَوَّل‎", "رَبِيع ٱلثَّانِي", "جُمَادَىٰ ٱلْأُولَىٰ", "جُمَادَىٰ ٱلثَّانِيَة", "رَجَب‎", "شَعْبَان‎", "رَمَضَان", "شَوَّال", "ذُو ٱلْقَعْدَة", "ذُو ٱلْحِجَّة"};
        //String[] moonNames = {"al-Muḥarram", "Ṣafar", "Rabīʿ al-ʾAwwal", "Rabīʿ ath-Thānī", "Jumadā al-ʾŪlā", "Jumādā ath-Thāniyah", "Rajab", "Shaʿbān", "Ramaḍān", "Shawwāl", "Ḏū al-Qaʿdah", "Ḏū al-Ḥijjah"};
        String colname[] = {"سب", "اح", "ثن", "ثل", "ار", "خم", "جم"};
    //            String colname[] = {"Sa", "sun", "Mon", "Tu", "We", "Th", "Fr"};
        JLabel cell;
        JLabel monthNameLabel = new JLabel();  // get name from array
     
        Image img;
     
        private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd";
        private static final int DIALOG_WIDTH = 270;
        private static final int DIALOG_HEIGHT = 210;
     
        private SimpleDateFormat dateFormat;
        private Hijrah.DatePanel datePanel = null;
        private JDialog dateDialog = null;
     
        private JButton choosdate;
        private JTextField datetext;
        private JPanel datePanels;
     
        public void hdate() {
            HijrahDate hijrahDate = HijrahDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            String formatted = formatter.format(hijrahDate); // 07/03/1439 
            ye = Integer.parseInt(formatted.substring(0, 4));
            mo = Integer.parseInt(formatted.substring(5, 7));
            da = Integer.parseInt(formatted.substring(8, 10));
        }
     
        public Hijrah(String dateFormatPattern, Date date) {
            this();
            DEFAULT_DATE_FORMAT = dateFormatPattern;
        }
     
        public Hijrah() {
            add(datePanels);
            datePanels.setPreferredSize(new Dimension(640, 480));
            datePanels.setLayout(new FlowLayout());
            hdate();
            try {
                img = ImageIO.read(getClass().getResource("cleander.png"));
                choosdate.setIcon(new ImageIcon(img));
            } catch (IOException ex) {
            }
     
            setDate();
    //        datetext.setEditable(false);
            setCursor(new Cursor(Cursor.HAND_CURSOR));
            addListeners();
            datePanels.add(choosdate);
            datePanels.add(datetext);
        }
     
        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() {
            setDate();
        }
     
        public void setDate() {
            datetext.setText(testMethod());   //<<<<< Changed
     
        }
     
        public Date getDate() {
     
            try {
                return getDefaultDateFormat().parse(datetext.getText());
            } catch (ParseException e) {
                return new Date();
            }
        }
     
        private class DatePanel extends JPanel implements ChangeListener {
     
            int startYear = 1;
            int lastYear = 99999;
     
            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 = new java.awt.Color(153, 0, 153);
            Color controlMoonnameTextColor = Color.BLUE;
     
            JSpinner yearSpin;
            JSpinner monthSpin;
            JButton[][] daysButton = new JButton[8][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() {
                Font font = new Font("Tahoma", Font.BOLD, 10);
                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("السنة");
                yearLabel.setForeground(controlTextColor);
                yearLabel.setHorizontalAlignment(SwingConstants.CENTER);
                yearLabel.setVerticalAlignment(SwingConstants.CENTER);
                yearLabel.setFont(font);
     
                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);
                monthNameLabel.setPreferredSize(new Dimension(65, 20));
                monthNameLabel.setForeground(controlTextColor);
                monthNameLabel.setHorizontalAlignment(SwingConstants.CENTER);
                monthNameLabel.setVerticalAlignment(SwingConstants.CENTER);
                monthNameLabel.setFont(font);
                monthNameLabel.setText(moonNames[(Integer) monthSpin.getValue() - 1]);  // get name from array
                panel.add(monthNameLabel);
     
                panel.add(monthSpin);
     
                JLabel monthLabel = new JLabel("الشهر");
                monthLabel.setForeground(controlTextColor);
                monthLabel.setHorizontalAlignment(SwingConstants.CENTER);
                monthLabel.setVerticalAlignment(SwingConstants.CENTER);
                monthLabel.setFont(font);
                panel.add(monthLabel);
     
                return panel;
            }
     
            private JPanel createWeekAndDayPanal() {
     
                JPanel panel = new JPanel();
                panel.setFont(new Font("Tahoma", Font.PLAIN, 10));
                panel.setLayout(new GridLayout(7, 7));
                panel.setBackground(Color.white);
     
                for (int i = 0; i < 7; i++) {
                    cell = new JLabel(colname[i]);
                    cell.setHorizontalAlignment(JLabel.CENTER);
                    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) {
                                    numBtn.setForeground(dateFontColor);
     
                                    return;
                                }
     
                                numBtn.setBackground(palletTableColor);
                                numBtn.setForeground(dateFontColor);
                                source.setForeground(todayBackColor);
     
                                int newDay = Integer.parseInt(source.getText());
                                da = newDay;
     
                                setDate();
                                dayColorUpdate(true);
                                dateDialog.setVisible(false);
                            }
                        });
     
                        daysButton[i][j] = numBtn;
                        panel.add(numBtn);
                        actionCommandId++;
                        if (j == 6) {
                            numBtn.setForeground(weekendFontColor);
                        } else {
                            numBtn.setForeground(dateFontColor);
                        }
                    }
                }
     
                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) {
     
                String s = String.valueOf(da);
                for (int a = 0; a < 6; a++) {
                    for (int b = 0; b < 7; b++) {
                        daysButton[a][b].setForeground(dateFontColor);
                        daysButton[a][6].setForeground(weekendFontColor);
     
                        if (daysButton[a][b].getText().equals(s)) {
     
                            daysButton[a][b].setForeground(todayBackColor);
     
                        }
                    }
                }
            }
     
            private void reflushWeekAndDay() {
     
                ye = (Integer) yearSpin.getValue();
                mo = (Integer) monthSpin.getValue();
     
                Locale ar = new Locale("ar");
     
                DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar);
                HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(ye, mo, da);
     
                String WeekdayNum = hijrahDate.format(dayOfweekNum);
                int WeekdayNumb = Integer.parseInt(WeekdayNum);
     
                int moondaysnumber = hijrahDate.lengthOfMonth();
     
                int maxDayNo = 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 <= maxDayNo) {
                            s = String.valueOf(dayNo);
                        }
                        daysButton[i][j].setText(s);
                        dayNo++;
                        if (daysButton[i][j].getText().equals("")) {
                            daysButton[i][j].setVisible(false);
                        } else {
                            daysButton[i][j].setVisible(true);
     
                        }
     
                    }
     
                }
                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);
                }
                reflushWeekAndDay();
                setDate();
     
                mothnam();
            }
     
        }
     
        public void mothnam() {
            monthNameLabel.setText(moonNames[mo - 1]);
        }
     
        String testMethod() {                  //  ADDED
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            HijrahDate today = HijrahChronology.INSTANCE.date(ye, mo, da);
            String formatted = formatter.format(today); // 07/03/1439
     
            return formatted.toString(); //<<<<<< ADDED
        }
     
    }
    Last edited by cnmeysam; April 30th, 2021 at 05:48 PM.

  21. #196
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    The posted code should not execute without errors because the choosdate and datetext variables do not have values.

    What is this
    import newpackage.*;
    If you don't understand my answer, don't ignore it, ask a question.

  22. #197

    Default Re: library for Hijra date chooser

    but i tested and work's fine without problem just don't load button after text

    --- Update ---

    you can test it
     
    public class NewJFrame extends javax.swing.JFrame {
     
        public NewJFrame() {
            initComponents();
        }
     
     
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            gregorian1 = new Gregorian();
            hijrah1 = new newpackage.Hijrah();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(131, 131, 131)
                    .addComponent(gregorian1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(hijrah1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(139, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(20, 20, 20)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(gregorian1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(hijrah1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(260, Short.MAX_VALUE))
            );
     
            setSize(new java.awt.Dimension(416, 339));
            setLocationRelativeTo(null);
        }// </editor-fold>                        
     
        public static void main(String args[]) {
     
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
     
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewJFrame().setVisible(true);
                }
            });
        }
     
        // Variables declaration - do not modify                     
        private Gregorian gregorian1;
        private newpackage.Hijrah hijrah1;
        // End of variables declaration                   
    }


    --- Update ---

    this block of code set date for ye and mo and da
    hdate();

  23. #198
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    work's fine without problem
    Strange. I don't see how the posted code can work with those two variables not having values.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #199

    Default Re: library for Hijra date chooser

    you must use Top code next to the this class
    /*
     * Many thanks to Norm from the www.javaprogrammingforums.com that knew more than me.put more time and energy to create this component 
     * Maysam Soleymani Qorabaee www.programsfuture.com  * 
     */
     
    /**
     *
     * @author Admin
     */
    import newpackage.*;
    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.Image;
    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.io.IOException;
    import java.text.Format;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.time.LocalDate;
    import java.time.chrono.HijrahChronology;
    import java.time.chrono.HijrahDate;
    import java.time.format.DateTimeFormatter;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Locale;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
     
    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 javax.swing.JPanel;
     
    public class Hijrah extends JPanel {
     
        int ye;
        int mo;
        int da;
     
        String[] moonNames = {"ٱلْمُحَرَّم‎", "صَفَر‎", "رَبِيع ٱلْأَوَّل‎", "رَبِيع ٱلثَّانِي", "جُمَادَىٰ ٱلْأُولَىٰ", "جُمَادَىٰ ٱلثَّانِيَة", "رَجَب‎", "شَعْبَان‎", "رَمَضَان", "شَوَّال", "ذُو ٱلْقَعْدَة", "ذُو ٱلْحِجَّة"};
        //String[] moonNames = {"al-Muḥarram", "Ṣafar", "Rabīʿ al-ʾAwwal", "Rabīʿ ath-Thānī", "Jumadā al-ʾŪlā", "Jumādā ath-Thāniyah", "Rajab", "Shaʿbān", "Ramaḍān", "Shawwāl", "Ḏū al-Qaʿdah", "Ḏū al-Ḥijjah"};
        String colname[] = {"سب", "اح", "ثن", "ثل", "ار", "خم", "جم"};
    //            String colname[] = {"Sa", "sun", "Mon", "Tu", "We", "Th", "Fr"};
        JLabel cell;
        JLabel monthNameLabel = new JLabel();  // get name from array
     
        Image img;
     
        private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd";
        private static final int DIALOG_WIDTH = 270;
        private static final int DIALOG_HEIGHT = 210;
     
        private SimpleDateFormat dateFormat;
        private Hijrah.DatePanel datePanel = null;
        private JDialog dateDialog = null;
     
        private JButton choosdate;
        private JTextField datetext;
        private JPanel datePanels;
     
        public void hdate() {
            HijrahDate hijrahDate = HijrahDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            String formatted = formatter.format(hijrahDate); // 07/03/1439 
            ye = Integer.parseInt(formatted.substring(0, 4));
            mo = Integer.parseInt(formatted.substring(5, 7));
            da = Integer.parseInt(formatted.substring(8, 10));
        }
     
        public Hijrah(String dateFormatPattern, Date date) {
            this();
            DEFAULT_DATE_FORMAT = dateFormatPattern;
        }
     
        public Hijrah() {
            hdate();
            add(datePanels);
            datePanels.setPreferredSize(new Dimension(640, 480));
            datePanels.setLayout(new FlowLayout());
     
            try {
                img = ImageIO.read(getClass().getResource("cleander.png"));
                choosdate.setIcon(new ImageIcon(img));
            } catch (IOException ex) {
            }
     
            setDate();
    //        datetext.setEditable(false);
            setCursor(new Cursor(Cursor.HAND_CURSOR));
            addListeners();
            datePanels.add(choosdate);
            datePanels.add(datetext);
        }
     
        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() {
            setDate();
        }
     
        public void setDate() {
            datetext.setText(testMethod());   //<<<<< Changed
     
        }
     
        public Date getDate() {
     
            try {
                return getDefaultDateFormat().parse(datetext.getText());
            } catch (ParseException e) {
                return new Date();
            }
        }
     
        private class DatePanel extends JPanel implements ChangeListener {
     
            int startYear = 1;
            int lastYear = 99999;
     
            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 = new java.awt.Color(153, 0, 153);
            Color controlMoonnameTextColor = Color.BLUE;
     
            JSpinner yearSpin;
            JSpinner monthSpin;
            JButton[][] daysButton = new JButton[8][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() {
                Font font = new Font("Tahoma", Font.BOLD, 10);
                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("السنة");
                yearLabel.setForeground(controlTextColor);
                yearLabel.setHorizontalAlignment(SwingConstants.CENTER);
                yearLabel.setVerticalAlignment(SwingConstants.CENTER);
                yearLabel.setFont(font);
     
                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);
                monthNameLabel.setPreferredSize(new Dimension(65, 20));
                monthNameLabel.setForeground(controlTextColor);
                monthNameLabel.setHorizontalAlignment(SwingConstants.CENTER);
                monthNameLabel.setVerticalAlignment(SwingConstants.CENTER);
                monthNameLabel.setFont(font);
                monthNameLabel.setText(moonNames[(Integer) monthSpin.getValue() - 1]);  // get name from array
                panel.add(monthNameLabel);
     
                panel.add(monthSpin);
     
                JLabel monthLabel = new JLabel("الشهر");
                monthLabel.setForeground(controlTextColor);
                monthLabel.setHorizontalAlignment(SwingConstants.CENTER);
                monthLabel.setVerticalAlignment(SwingConstants.CENTER);
                monthLabel.setFont(font);
                panel.add(monthLabel);
     
                return panel;
            }
     
            private JPanel createWeekAndDayPanal() {
     
                JPanel panel = new JPanel();
                panel.setFont(new Font("Tahoma", Font.PLAIN, 10));
                panel.setLayout(new GridLayout(7, 7));
                panel.setBackground(Color.white);
     
                for (int i = 0; i < 7; i++) {
                    cell = new JLabel(colname[i]);
                    cell.setHorizontalAlignment(JLabel.CENTER);
                    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) {
                                    numBtn.setForeground(dateFontColor);
     
                                    return;
                                }
     
                                numBtn.setBackground(palletTableColor);
                                numBtn.setForeground(dateFontColor);
                                source.setForeground(todayBackColor);
     
                                int newDay = Integer.parseInt(source.getText());
                                da = newDay;
     
                                setDate();
                                dayColorUpdate(true);
                                dateDialog.setVisible(false);
                            }
                        });
     
                        daysButton[i][j] = numBtn;
                        panel.add(numBtn);
                        actionCommandId++;
                        if (j == 6) {
                            numBtn.setForeground(weekendFontColor);
                        } else {
                            numBtn.setForeground(dateFontColor);
                        }
                    }
                }
     
                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) {
     
                String s = String.valueOf(da);
                for (int a = 0; a < 6; a++) {
                    for (int b = 0; b < 7; b++) {
                        daysButton[a][b].setForeground(dateFontColor);
                        daysButton[a][6].setForeground(weekendFontColor);
     
                        if (daysButton[a][b].getText().equals(s)) {
     
                            daysButton[a][b].setForeground(todayBackColor);
     
                        }
                    }
                }
            }
     
            private void reflushWeekAndDay() {
     
                ye = (Integer) yearSpin.getValue();
                mo = (Integer) monthSpin.getValue();
     
                Locale ar = new Locale("ar");
     
                DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar);
                HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(ye, mo, da);
     
                String WeekdayNum = hijrahDate.format(dayOfweekNum);
                int WeekdayNumb = Integer.parseInt(WeekdayNum);
     
                int moondaysnumber = hijrahDate.lengthOfMonth();
     
                int maxDayNo = 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 <= maxDayNo) {
                            s = String.valueOf(dayNo);
                        }
                        daysButton[i][j].setText(s);
                        dayNo++;
                        if (daysButton[i][j].getText().equals("")) {
                            daysButton[i][j].setVisible(false);
                        } else {
                            daysButton[i][j].setVisible(true);
     
                        }
     
                    }
     
                }
                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);
                }
                reflushWeekAndDay();
                setDate();
     
                mothnam();
            }
     
        }
     
        public void mothnam() {
            monthNameLabel.setText(moonNames[mo - 1]);
        }
     
        String testMethod() {                  //  ADDED
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            HijrahDate today = HijrahChronology.INSTANCE.date(ye, mo, da);
            String formatted = formatter.format(today); // 07/03/1439
     
            return formatted.toString(); //<<<<<< ADDED
        }
     
    }


    --- Update ---

    what variables not having values.?

    --- Update ---

    first load this part and set values
    4public void hdate() {
            HijrahDate hijrahDate = HijrahDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            String formatted = formatter.format(hijrahDate); // 07/03/1439 
            ye = Integer.parseInt(formatted.substring(0, 4));
            mo = Integer.parseInt(formatted.substring(5, 7));
            da = Integer.parseInt(formatted.substring(8, 10));
        }

  25. #200
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    what variables not having values.?
    See post#196


    you must use Top code
    What is that? I do not see a Top class.
    If you don't understand my answer, don't ignore it, ask a question.

Page 8 of 12 FirstFirst ... 678910 ... 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