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 10 of 12 FirstFirst ... 89101112 LastLast
Results 226 to 250 of 284

Thread: library or component for Hijra date chooser

  1. #226
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: library for Hijra date chooser

    know how fix datePanels to extends JPanel
    It does not need to extend JPanel. It is a JPanel
    If you don't understand my answer, don't ignore it, ask a question.

  2. #227

    Default Re: library for Hijra date chooser

    You do not understand what I mean
    I mean, when the size of the extends JPanel changes, the datePanels remain the old size And it does not get bigger or smaller That's why the size of the text box does not get bigger It does not matter if this problem is not solved


    --- Update ---


    But now the user can not set date in text Hijri from the new from
    For example, by clicking on the new button on new form update date in the hiji text

  3. #228
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: library for Hijra date chooser

    The changing of the GUI is controlled by the layout manager. What layout manager are you using for those components and their containers?
    I don't work with layout managers and can't help you.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #229

    Default Re: library for Hijra date chooser

    in original date chooser use gregorian date and for set date in new form we use gregorian1.setDate(new Date());
    this is original date chooser
     
     
    /*
     * 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.HijrahDate;
    import java.time.format.DateTimeFormatter;
    import java.util.Calendar;
    import java.util.Date;
     
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSpinner;
    import javax.swing.JTextField;
    import javax.swing.SpinnerNumberModel;
    import javax.swing.SwingConstants;
    import javax.swing.SwingUtilities;
    import javax.swing.border.LineBorder;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
     
    public class Gregorian extends JTextField {
     
        String[] moonNames = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        JLabel monthNameLabel = new JLabel();  // get name from array
        JSpinner yearSpin;
        JSpinner monthSpin;
     
        private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd";
        private static final int DIALOG_WIDTH = 280;
        private static final int DIALOG_HEIGHT = 210;
     
        private SimpleDateFormat dateFormat;
        private DatePanel datePanel = null;
        private JDialog dateDialog = null;
     
        public Gregorian() {
            this(new Date());
     
        }
     
        public Gregorian(String dateFormatPattern, Date date) {
            this(date);
            DEFAULT_DATE_FORMAT = dateFormatPattern;
     
        }
     
        public Gregorian(Date date) {
            setDate(date);
    //        setEditable(false);
            setCursor(new Cursor(Cursor.HAND_CURSOR));
            addListeners();
     
        }
     
        private void addListeners() {
            addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent paramMouseEvent) {
                    if (datePanel == null) {
                        datePanel = new DatePanel();
                    }
                    Point point = getLocationOnScreen();
                    point.y = point.y + 30;
                    showDateDialog(datePanel, point);
                }
            });
        }
     
        private void showDateDialog(DatePanel dateChooser, Point position) {
     
            Frame owner = (Frame) SwingUtilities
                    .getWindowAncestor(Gregorian.this);
            if (dateDialog == null || dateDialog.getOwner() != owner) {
                dateDialog = createDateDialog(owner, dateChooser);
            }
            dateDialog.setLocation(getAppropriateLocation(owner, position));
            dateDialog.setVisible(true);
     
        }
     
        private JDialog createDateDialog(Frame owner, JPanel contentPanel) {
            JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal
    //        JDialog dialog = new JDialog(owner, "Date Selected", true);
            dialog.setUndecorated(true);
            dialog.getContentPane().add(contentPanel, BorderLayout.CENTER);
            dialog.pack();
            dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
     
            dialog.addWindowFocusListener(new WindowFocusListener() {
                public void windowLostFocus(WindowEvent e) {
                    dialog.setVisible(false);
                }
     
                public void windowGainedFocus(WindowEvent e) {
                }
            });
     
            return dialog;
     
        }
     
        private Point getAppropriateLocation(Frame owner, Point position) {
            Point result = new Point(position);
            Point p = owner.getLocation();
            int offsetX = (position.x + DIALOG_WIDTH) - (p.x + owner.getWidth());
            int offsetY = (position.y + DIALOG_HEIGHT) - (p.y + owner.getHeight());
     
            if (offsetX > 0) {
                result.x -= offsetX;
            }
     
            if (offsetY > 0) {
                result.y -= offsetY;
            }
     
            return result;
        }
     
        private SimpleDateFormat getDefaultDateFormat() {
            if (dateFormat == null) {
                dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
            }
            return dateFormat;
        }
     
        public void setText(Date date) {
            setDate(date);
        }
     
        public void setDate(Date date) {
            super.setText(getDefaultDateFormat().format(date));
        }
     
        public Date getDate() {
            try {
                return getDefaultDateFormat().parse(getText());
            } catch (ParseException e) {
                return new Date();
            }
        }
     
        private class DatePanel extends JPanel implements ChangeListener {
     
            int startYear = 1380;
            int lastYear = 3050;
     
            Color backGroundColor = Color.gray;
            Color palletTableColor = Color.white;
            Color todayBackColor = Color.orange;
            Color weekFontColor = Color.blue;
            Color dateFontColor = Color.black;
            Color weekendFontColor = Color.red;
     
            Color controlLineColor = Color.pink;
            Color controlTextColor = Color.white;
            Color controlMoonnameTextColor = Color.BLUE;
     
            JButton[][] daysButton = new JButton[6][7];
     
            DatePanel() {
                setLayout(new BorderLayout());
                setBorder(new LineBorder(backGroundColor, 2));
                setBackground(backGroundColor);
     
                JPanel topYearAndMonth = createYearAndMonthPanal();
                add(topYearAndMonth, BorderLayout.NORTH);
                JPanel centerWeekAndDay = createWeekAndDayPanal();
                add(centerWeekAndDay, BorderLayout.CENTER);
     
                reflushWeekAndDay();
            }
     
            private JPanel createYearAndMonthPanal() {
                Calendar cal = getCalendar();
                int currentYear = cal.get(Calendar.YEAR);
                int currentMonth = cal.get(Calendar.MONTH) + 1;
     
                JPanel panel = new JPanel();
                panel.setLayout(new FlowLayout());
                panel.setBackground(controlLineColor);
                JLabel yearLabel = new JLabel("Year");
                yearLabel.setForeground(controlTextColor);
                panel.add(yearLabel);
                yearSpin = new JSpinner(new SpinnerNumberModel(currentYear,
                        startYear, lastYear, 1));
                yearSpin.setPreferredSize(new Dimension(60, 30));
                yearSpin.setName("Year");
                yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####"));
                yearSpin.addChangeListener(this);
                panel.add(yearSpin);
     
                monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1,
                        12, 1));
     
                monthSpin.setPreferredSize(new Dimension(45, 30));
                monthSpin.setName("Month");
                monthSpin.addChangeListener(this);
     
                monthNameLabel.setPreferredSize(new Dimension(70, 20));
                monthNameLabel.setHorizontalAlignment(SwingConstants.CENTER);
                monthNameLabel.setVerticalAlignment(SwingConstants.CENTER);
                monthNameLabel.setText(moonNames[(Integer) monthSpin.getValue() - 1]);  // get name from array
                panel.add(monthNameLabel);
     
                panel.add(monthSpin);
     
                JLabel monthLabel = new JLabel("Month");
                monthLabel.setForeground(controlTextColor);
                panel.add(monthLabel);
     
                return panel;
            }
     
            private JPanel createWeekAndDayPanal() {
                String colname[] = {"sun", "Mon", "Tu", "We", "Th", "Fr", "Sa"};
                JPanel panel = new JPanel();
                panel.setFont(new Font("Arial", Font.PLAIN, 10));
                panel.setLayout(new GridLayout(7, 7));
                panel.setBackground(Color.white);
     
                for (int i = 0; i < 7; i++) {
                    JLabel cell = new JLabel(colname[i]);
                    cell.setHorizontalAlignment(JLabel.CENTER);
    //                if (i == 0 || i == 6) {
                        if (i == 6) {
                        cell.setForeground(weekendFontColor);
                    } else {
                        cell.setForeground(weekFontColor);
                    }
                    panel.add(cell);
                }
     
                int actionCommandId = 0;
                for (int i = 0; i < 6; i++) {
                    for (int j = 0; j < 7; j++) {
                        JButton numBtn = new JButton();
                        numBtn.setBorder(null);
                        numBtn.setHorizontalAlignment(SwingConstants.CENTER);
                        numBtn.setActionCommand(String
                                .valueOf(actionCommandId));
                        numBtn.setBackground(palletTableColor);
                        numBtn.setForeground(dateFontColor);
                        numBtn.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent event) {
                                JButton source = (JButton) event.getSource();
                                if (source.getText().length() == 0) {
                                    return;
                                }
     
                                dayColorUpdate(true);
                                source.setForeground(todayBackColor);
                                int newDay = Integer.parseInt(source.getText());
                                Calendar cal = getCalendar();
                                cal.set(Calendar.DAY_OF_MONTH, newDay);
                                setDate(cal.getTime());
     
                                dateDialog.setVisible(false);
                            }
                        });
     
    //                    if (j == 0 || j == 6) {
                            if (j == 6) {
                            numBtn.setForeground(weekendFontColor);
                        } else {
                            numBtn.setForeground(dateFontColor);
                        }
                        daysButton[i][j] = numBtn;
                        panel.add(numBtn);
                        actionCommandId++;
                    }
                }
     
                return panel;
            }
     
            private Calendar getCalendar() {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(getDate());
                return calendar;
            }
     
            private int getSelectedYear() {
                return ((Integer) yearSpin.getValue()).intValue();
            }
     
            private int getSelectedMonth() {
                return ((Integer) monthSpin.getValue()).intValue();
            }
     
            private void dayColorUpdate(boolean isOldDay) {
                Calendar cal = getCalendar();
                int day = cal.get(Calendar.DAY_OF_MONTH);
     
                cal.set(Calendar.DAY_OF_MONTH, 1);
                int actionCommandId = day - 2 + cal.get(Calendar.DAY_OF_WEEK);
                int i = actionCommandId / 7;
     
                int j = actionCommandId % 7;
     
                if (isOldDay) {
                    daysButton[i][j].setForeground(dateFontColor);
                    daysButton[i][6].setForeground(weekendFontColor);
                } else {
                    daysButton[i][j].setForeground(todayBackColor);
                }
            }
     
            private void reflushWeekAndDay() {
                Calendar cal = getCalendar();
                cal.set(Calendar.DAY_OF_MONTH, 1);
     
                int maxDayNo = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
     
                int dayNo = 2 - cal.get(Calendar.DAY_OF_WEEK);
     
                for (int i = 0; i < 6; i++) {
                    for (int j = 0; j < 7; j++) {
                        String s = "";
                        if (dayNo >= 1 && dayNo <= maxDayNo) {
                            s = String.valueOf(dayNo);
                        }
                        daysButton[i][j].setText(s);
                        dayNo++;
                        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(cal.getTime());
     
                reflushWeekAndDay();
                mothnam();
            }
     
        }
     
        public void mothnam() {
            monthNameLabel.setText(moonNames[(Integer) monthSpin.getValue() - 1]);
        }
     
    }

    in hijri date when i use hijri.setDate(new Date());
    add to Hijrah class this code but i don't know how i must editn it for work and set date in jtext
    void setDate(Date date) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

  5. #230
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: library for Hijra date chooser

    Are you trying to add a setDate() method to your Hijrah class?
    What is that method supposed to do?
    What argument/value needs to be passed to that method for it to do what you want?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #231

    Default Re: library for Hijra date chooser

    this is hijri date class
    package GHDate;
     
     
     
    /*
     * 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 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.GridBagLayout;
    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.JFormattedTextField;
    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;
    import javax.swing.SpringLayout;
     
    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 = new JButton();
        private JFormattedTextField datetext = new JFormattedTextField();
     
        private JPanel datePanels = new JPanel();
        String formatted;
     
        //Build a block to give a value to the year and month and day when the class loads
        public void hdate() {
            HijrahDate hijrahDate = HijrahDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            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();
     
     try {
                datetext.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("####/##/##")));
            } catch (java.text.ParseException ex) {
                ex.printStackTrace();
            }
     
             javax.swing.GroupLayout datePanelsLayout = new javax.swing.GroupLayout(datePanels);
            datePanels.setLayout(datePanelsLayout);
            datePanelsLayout.setHorizontalGroup(
                datePanelsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(datePanelsLayout.createSequentialGroup()
                    .addComponent(datetext, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, 0)
                    .addComponent(choosdate, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE))
            );
            datePanelsLayout.setVerticalGroup(
                datePanelsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(datePanelsLayout.createSequentialGroup()
                    .addGroup(datePanelsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(datetext, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(choosdate, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(0, 0, Short.MAX_VALUE))
            );
     
     
            add(datePanels);
     
            try {
                img = ImageIO.read(getClass().getResource("cleander.png"));
                choosdate.setIcon(new ImageIcon(img));
            } catch (IOException ex) {
            }
     
            setDate();
            choosdate.setCursor(new Cursor(Cursor.HAND_CURSOR));
            datePanels.add(datetext);
            datePanels.add(choosdate);
            choosdate.setVisible(true);
            choosdate.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    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(formatted);
            } 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];
     
            // Fill the days of the month in the buttons on the desired day
            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();
            }
     
            //Build a panel for daylight months and sppiners and labels
            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;
            }
     
            //Fill the days of the month in the buttons on the desired day
            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();
            }
     
            // Paint the days of the week and today
            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);
     
                        }
                    }
                }
            }
    // Specify the number of days of a cat and start on what day and end on what day
     
            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 = -0 - 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);
            }
     
            // set year when click on jdialog button's and spinner and textfild
            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();
            }
     
        }
     
        // set month when click on jdialog button's and spinner and textfild
        public void mothnam() {
            monthNameLabel.setText(moonNames[mo - 1]);
        }
     
        //set date for jtext and jdialog....
        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 ---

    Must add a new value to ye mo da
    after thar run testMethod() and datetext.setText(testMethod());

  7. #232
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: library for Hijra date chooser

    Must add a new value to ye mo da
    Can your new method take three arguments? For example: setDate(ye,mo,da);

    Note: the name: testMethod needs to be changed to describe what that method does. For example getHDateString
    If you don't understand my answer, don't ignore it, ask a question.

  8. #233

    Default Re: library for Hijra date chooser

    Quote Originally Posted by Norm View Post
    Can your new method take three arguments? For example: setDate(ye,mo,da);
    i don't think so
    Because the user passes string the date to the text in this way "2021/10/15" and convent string to date
    Last edited by cnmeysam; May 2nd, 2021 at 11:41 AM.

  9. #234
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: library for Hijra date chooser

    the user passes the date to the text in this way 2021/10/15
    Are you saying the setDate method will have one argument: a String. For example: setDate("2021/10/15");
    What is the setDate method supposed to do with that String?
    For example
    Store it in the textfield?
    Convert its contents by some rules and store the converted values in ye,mo,da?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #235

    Default Re: library for Hijra date chooser

    user must write like this like Gregorian. in Gregorian user use this method for Gregorian
    String sDate1="1402/09/20";  
        Date date1=new SimpleDateFormat("yyyy/MM/dd").parse(sDate1);  
        hijri1.setDate(date1);
       gregorian1.setDate(date1);
    then i think Must add a new value to ye mo da
    after thar run testMethod() and datetext.setText(testMethod());

  11. #236
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: library for Hijra date chooser

    the name: testMethod needs to be changed to describe what that method does. For example getHDateString

    user must write like this like Gregorian.
    Who is the user? Where does he get the String to pass to setDate?


    Must add a new value to ye mo da
    Where do those values come from? Can they be computed from the String passed to setDate?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #237

    Default Re: library for Hijra date chooser

    Quote Originally Posted by Norm View Post
    the name: testMethod needs to be changed to describe what that method does. For example getHDateString
    i fix that
    Quote Originally Posted by Norm View Post
    Who is the user? Where does he get the String to pass to setDate?
    someone like me and you from text or database or set date manually


    Where do those values come from? from text or database or set date manually
    Quote Originally Posted by Norm View Post
    Can they be computed from the String passed to setDate?
    i don't know but It should be

    --- Update ---

    now i see a new bug
    when system date change after 12 Am The value of days of the month that are stored in the buttons is not considered on the day , for example, 1402/09/20 is transferred to Wednesday instead of Sunday.

  13. #238
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: library for Hijra date chooser

    That doesn't sound too difficult. I am sure you can work out the logic.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #239

    Default Re: library for Hijra date chooser

    i fond logic and reason it is because of this tow parts of code and must change to hiji date but i don't have any idea how change them
      private Calendar getCalendar() {
     
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(getDate());
                return calendar;
            }
    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();
            }


    --- Update ---

    do you have any solution?
    Last edited by cnmeysam; May 2nd, 2021 at 01:26 PM.

  15. #240
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: library for Hijra date chooser

    In the second posted code I do not see where the value in the cal variable is used. It looks like the stateChanged method could be simplified to this
          public void stateChanged(ChangeEvent e) {
                dayColorUpdate(true);
                reflushWeekAndDay();
                setDate();
     
                mothnam();
          }

    What code uses the getCalendar() method?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #241

    Default Re: library for Hijra date chooser

    i change them like this
     private HijrahDate getCalendar() {
     
                HijrahDate today = HijrahChronology.INSTANCE.date(ye, mo, da);
     
                return today;
            }
    and
    public void stateChanged(ChangeEvent e) {
                dayColorUpdate(true);
                JSpinner source = (JSpinner) e.getSource();
                if (source.getName().equals("Year")) {
                    ye = (getSelectedYear());
                } else {
                    mo = (getSelectedMonth());
                }
                getCalendar();
                reflushWeekAndDay();
                setDate();
                mothnam();
            }
    but not fix when i change system date but The number of days of the month stored in the buttons on the desired day is not considered
    Could there be a problem with this part of code ?
    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 = -0 - WeekdayNumb;//set days in are in place
     
                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);
            }


    --- Update ---

    As I change every day, I have to change this part
    int dayNo = -0 - WeekdayNumb;

  17. #242
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: library for Hijra date chooser

    when i change system date but The number of days of the month stored in the buttons on the desired day is not considered
    How do you determine the number of days in a month in the Hijrah calendar?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #243

    Default Re: library for Hijra date chooser

    Quote Originally Posted by Norm View Post
    How do you determine the number of days in a month in the Hijrah calendar?
    first month
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    The second month
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 (30)
    Third month
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    The fourth month
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 (30)
    The fifth month
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    The sixth month
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 (30)
    Seventh month
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    The eighth month
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 (29)
    The ninth month
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    Tenth month
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 (30)
    Eleventh month
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    Twelfth month
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 (30)

    and in java To find out if a month has thirty days or twenty-nine days, we use the following command
    HijrahDate date = HijrahChronology.INSTANCE.date(1402,9, 10);
    System.out.println(date.lengthOfMonth());
    or this code
    HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(1442, 09, 10);
     int moondaysnumber = hijrahDate.lengthOfMonth();
                System.out.println("int MonthdayNum "+moondaysnumber);

  19. #244
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: library for Hijra date chooser

    when i change system date but The number of days of the month stored in the buttons on the desired day is not considered
    Ok, so you know the answer to your question.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #245

    Default Re: library for Hijra date chooser

    but i don't know how i must fix this bug

  21. #246
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: library for Hijra date chooser

    Trace through the code to see where the data starts from, how it is handled and passed, and where it is handled incorrectly.
    Then design a way to handle the data correctly.

    I do not understand what the problem is, so I can not help.
    You need to describe, step by step what happens and what you see.
    And the describe what you want to happen differently.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #247

    Default Re: library for Hijra date chooser

    Quote Originally Posted by Norm View Post
    I do not understand what the problem is, so I can not .
    when i fix today date hijridate get in own place like "1442/09/20" Displays Sunday and change system date to 2021-05-03 hijridate show "1442/09/20" Monday
    And if another day goes by the system calendar like 2021-05-04 hijridate show "1442/09/20" Tuesday
    Every day that goes back the system calendar goes back one day hijridate Every day that goes by, the system calendar goes by one day hijridate From your place in the batons For example, if it is in Baton [1][6], it goes to Baton[1][7] on the 20 Day of the month 9 or Baton [1][5], it goes to Baton[1][6] on the 20 Day of the month 9

  23. #248
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: library for Hijra date chooser

    Now look at the code to see where the problem is. Start at where a good date is and trace it through the code until it is not a valid date.

    You have been working with this code for a long time and should be able to trace what happens as it executes.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #249

    Default Re: library for Hijra date chooser

    The problem is that until the system date is not changed, the code works perfectly. I do not understand why changing the system date causes all these problems when all the functions follow the Hijri date.

    --- Update ---

    Even when using spinners, the number of days is in the right place

  25. #250
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: library for Hijra date chooser

    why changing the system date causes all these problems
    How is the system date changed? The computer's clock runs continuously.
    If you don't understand my answer, don't ignore it, ask a question.

Page 10 of 12 FirstFirst ... 89101112 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