Look at how NewClass worked. When the year spinner was clicked, the value shown in the textfield was immediately updated.
The new code prints out a message but does not update the text field.
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.
Look at how NewClass worked. When the year spinner was clicked, the value shown in the textfield was immediately updated.
The new code prints out a message but does not update the text field.
If you don't understand my answer, don't ignore it, ask a question.
cnmeysam (April 28th, 2021)
Print this before and after the call to that method: cal.getTime()I do not understand exactly what this part of the code does
The first will be some date, the second will be for the 1st day of the month
--- Update ---
Run it in the constructor.how to run this part before anything else
--- Update ---
This method should use an array:public void mothnam() { if (mo == 1) { monthNameLabel.setText("ٱلْمُحَرَّم"); .... monthNameLabel.setText(monthNames[mo]); // get name from monthNames arrray
If you don't understand my answer, don't ignore it, ask a question.
cnmeysam (April 28th, 2021)
this part dont work after mo ==11 in mo==12 show this error
monthNameLabel.setText(monthNames[mo]);
now update text and dialogbox after push spinnersException in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 12 at Hijrah.mothnam(Hijrah.java:419) at Hijrah$DatePanel.stateChanged(Hijrah.java:413) at javax.swing.JSpinner.fireStateChanged(JSpinner.java:458) at javax.swing.JSpinner$ModelListener.stateChanged(JSpinner.java:386) at javax.swing.AbstractSpinnerModel.fireStateChanged(AbstractSpinnerModel.java:119) at javax.swing.SpinnerNumberModel.setValue(SpinnerNumberModel.java:447) at javax.swing.JSpinner.setValue(JSpinner.java:354) at javax.swing.plaf.basic.BasicSpinnerUI$ArrowButtonHandler.actionPerformed(BasicSpinnerUI.java:654) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289) at java.awt.Component.processMouseEvent(Component.java:6533) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6298) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
The second case is how to run this part before anything else hdate() i need this part for set year monyh and day in ye mo da for load current date before app opened
/* * 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 ; int ye = 1442; int mo = 10; int da = 16; 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"}; 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(new Date()); } public Hijrah(String dateFormatPattern, Date date) { this(date); DEFAULT_DATE_FORMAT = dateFormatPattern; } public Hijrah(Date date) { setDate(date); setEditable(false); setCursor(new Cursor(Cursor.HAND_CURSOR)); addListeners(); } private void addListeners() { addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent paramMouseEvent) { if (datePanel == null) { datePanel = new DatePanel(); } Point point = getLocationOnScreen(); point.y = point.y + 30; showDateDialog(datePanel, point); } }); } private void showDateDialog(DatePanel dateChooser, Point position) { Frame owner = (Frame) SwingUtilities .getWindowAncestor(Hijrah.this); if (dateDialog == null || dateDialog.getOwner() != owner) { dateDialog = createDateDialog(owner, dateChooser); } dateDialog.setLocation(getAppropriateLocation(owner, position)); dateDialog.setVisible(true); } private JDialog createDateDialog(Frame owner, JPanel contentPanel) { JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal // JDialog dialog = new JDialog(owner, "Date Selected", true); dialog.setUndecorated(true); dialog.getContentPane().add(contentPanel, BorderLayout.CENTER); dialog.pack(); dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT); dialog.addWindowFocusListener(new WindowFocusListener() { public void windowLostFocus(WindowEvent e) { dialog.setVisible(false); } public void windowGainedFocus(WindowEvent e) { } }); return dialog; } private Point getAppropriateLocation(Frame owner, Point position) { Point result = new Point(position); Point p = owner.getLocation(); int offsetX = (position.x + DIALOG_WIDTH) - (p.x + owner.getWidth()); int offsetY = (position.y + DIALOG_HEIGHT) - (p.y + owner.getHeight()); if (offsetX > 0) { result.x -= offsetX; } if (offsetY > 0) { result.y -= offsetY; } return result; } private SimpleDateFormat getDefaultDateFormat() { if (dateFormat == null) { dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT); } return dateFormat; } public void setText(Date date) { setDate(date); } public void setDate(Date date) { super.setText(testMethod()); //<<<<< Changed } public Date getDate() { try { return getDefaultDateFormat().parse(getText()); } catch (ParseException e) { return new Date(); } } private class DatePanel extends JPanel implements ChangeListener { int startYear = 1380; int lastYear = 3050; Color backGroundColor = Color.gray; Color palletTableColor = Color.white; Color todayBackColor = Color.orange; Color weekFontColor = Color.blue; Color dateFontColor = Color.black; Color weekendFontColor = Color.red; Color controlLineColor = Color.GREEN; Color controlTextColor = Color.white; Color controlMoonnameTextColor = Color.BLUE; JSpinner yearSpin; JSpinner monthSpin; JButton[][] daysButton = new JButton[6][7]; DatePanel() { setLayout(new BorderLayout()); setBorder(new LineBorder(backGroundColor, 2)); setBackground(backGroundColor); JPanel topYearAndMonth = createYearAndMonthPanal(); add(topYearAndMonth, BorderLayout.NORTH); JPanel centerWeekAndDay = createWeekAndDayPanal(); add(centerWeekAndDay, BorderLayout.CENTER); reflushWeekAndDay(); } private JPanel createYearAndMonthPanal() { Calendar cal = getCalendar(); int currentYear = cal.get(Calendar.YEAR); int currentMonth = cal.get(Calendar.MONTH) + 1; JPanel panel = new JPanel(); panel.setLayout(new FlowLayout()); panel.setBackground(controlLineColor); JLabel yearLabel = new JLabel("Year"); yearLabel.setForeground(controlTextColor); panel.add(yearLabel); yearSpin = new JSpinner(new SpinnerNumberModel(currentYear, startYear, lastYear, 1)); yearSpin.setPreferredSize(new Dimension(60, 30)); yearSpin.setName("Year"); yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####")); yearSpin.addChangeListener(this); panel.add(yearSpin); monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1, 12, 1)); monthSpin.setPreferredSize(new Dimension(45, 30)); monthSpin.setName("Month"); monthSpin.addChangeListener(this); monthNameLabel.setPreferredSize(new Dimension(60, 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[] = {"سب", "اح", "ثن", "ثل", "ار", "خم", "جم"}; // String colname[] = {"Sa", "sun", "Mon", "Tu", "We", "Th", "Fr"}; JPanel panel = new JPanel(); panel.setFont(new Font("Arial", Font.PLAIN, 10)); panel.setLayout(new GridLayout(7, 7)); panel.setBackground(Color.white); for (int i = 0; i < 7; i++) { JLabel cell = new JLabel(colname[i]); cell.setHorizontalAlignment(JLabel.CENTER); if (i == 6) { cell.setForeground(weekendFontColor); } else { cell.setForeground(weekFontColor); } panel.add(cell); } int actionCommandId = 0; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { JButton numBtn = new JButton(); numBtn.setBorder(null); numBtn.setHorizontalAlignment(SwingConstants.CENTER); numBtn.setActionCommand(String.valueOf(actionCommandId)); numBtn.setBackground(palletTableColor); numBtn.setForeground(dateFontColor); numBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JButton source = (JButton) event.getSource(); if (source.getText().length() == 0) { return; } dayColorUpdate(true); source.setForeground(todayBackColor); int newDay = Integer.parseInt(source.getText()); Calendar cal = getCalendar(); cal.set(Calendar.DAY_OF_MONTH, newDay); setDate(cal.getTime()); dateDialog.setVisible(false); } }); if (j == 6) { numBtn.setForeground(weekendFontColor); } else { numBtn.setForeground(dateFontColor); } daysButton[i][j] = numBtn; panel.add(numBtn); actionCommandId++; } } return panel; } private Calendar getCalendar() { Calendar calendar = Calendar.getInstance(); calendar.setTime(getDate()); return calendar; } private int getSelectedYear() { return ((Integer) yearSpin.getValue()).intValue(); } private int getSelectedMonth() { return ((Integer) monthSpin.getValue()).intValue(); } private void dayColorUpdate(boolean isOldDay) { 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 - 3 + WeekdayNumb; int i = actionCommandId / 7; int j = actionCommandId % 7; if (isOldDay) { daysButton[i][j].setForeground(dateFontColor); } else { daysButton[i][j].setForeground(todayBackColor); } } private void reflushWeekAndDay() { 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; int dayNo = 3 - WeekdayNumb; 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++; } } 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[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 ---
must be use like this monthNameLabel.setText(monthNames[mo-1]);
Last edited by cnmeysam; April 28th, 2021 at 01:10 PM.
If the array has 12 items then the valid indexes into the array are 0 to 11. If you want to use 1 to 12 as the indexes, then either subtract 1 to get 0 to 11 or add an empty value are the front of the array so there are elements at indexes 1 to 12.Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 12
Are you saying that the hdate() method needs to be run before anything else?how to run this part before anything else hdate()
Add a call to hdate() in the class's constructor.
If you don't understand my answer, don't ignore it, ask a question.
cnmeysam (April 28th, 2021)
how i can Add a call to hdate() in the class's constructor? can you write a Example fo me?
i put it evywer in my class but dont work
--- Update ---
you mean like this?public Hijrah(Date date) { hdate(); setDate(date); setEditable(false); setCursor(new Cursor(Cursor.HAND_CURSOR)); addListeners(); }
Call it just like the code calls the addListeners() method.
What is in the Date object passed to the constructor? What is it used for?
If you don't understand my answer, don't ignore it, ask a question.
cnmeysam (April 28th, 2021)
now i have just 2 problem and finish
first i ask important question
In the second and eleventh month date start from the second day
The program starts filling in the values from the first row. and in these 2 months starts from the zero row .
Please pay attention to the photo
https://s19.picofile.com/file/8432117034/Capture.JPG
--- Update ---
/* * 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"}; 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(new Date()); } public Hijrah(String dateFormatPattern, Date date) { this(date); DEFAULT_DATE_FORMAT = dateFormatPattern; } public Hijrah(Date date) { hdate(); setDate(date); setEditable(false); setCursor(new Cursor(Cursor.HAND_CURSOR)); addListeners(); } private void addListeners() { addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent paramMouseEvent) { if (datePanel == null) { datePanel = new DatePanel(); } Point point = getLocationOnScreen(); point.y = point.y + 30; showDateDialog(datePanel, point); } }); } private void showDateDialog(DatePanel dateChooser, Point position) { Frame owner = (Frame) SwingUtilities .getWindowAncestor(Hijrah.this); if (dateDialog == null || dateDialog.getOwner() != owner) { dateDialog = createDateDialog(owner, dateChooser); } dateDialog.setLocation(getAppropriateLocation(owner, position)); dateDialog.setVisible(true); } private JDialog createDateDialog(Frame owner, JPanel contentPanel) { JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal // JDialog dialog = new JDialog(owner, "Date Selected", true); dialog.setUndecorated(true); dialog.getContentPane().add(contentPanel, BorderLayout.CENTER); dialog.pack(); dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT); dialog.addWindowFocusListener(new WindowFocusListener() { public void windowLostFocus(WindowEvent e) { dialog.setVisible(false); } public void windowGainedFocus(WindowEvent e) { } }); return dialog; } private Point getAppropriateLocation(Frame owner, Point position) { Point result = new Point(position); Point p = owner.getLocation(); int offsetX = (position.x + DIALOG_WIDTH) - (p.x + owner.getWidth()); int offsetY = (position.y + DIALOG_HEIGHT) - (p.y + owner.getHeight()); if (offsetX > 0) { result.x -= offsetX; } if (offsetY > 0) { result.y -= offsetY; } return result; } private SimpleDateFormat getDefaultDateFormat() { if (dateFormat == null) { dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT); } return dateFormat; } public void setText(Date date) { setDate(date); } public void setDate(Date date) { super.setText(testMethod()); //<<<<< Changed } public Date getDate() { try { return getDefaultDateFormat().parse(getText()); } catch (ParseException e) { return new Date(); } } private class DatePanel extends JPanel implements ChangeListener { int startYear = 1380; int lastYear = 3050; Color backGroundColor = Color.gray; Color palletTableColor = Color.white; Color todayBackColor = Color.orange; Color weekFontColor = Color.blue; Color dateFontColor = Color.black; Color weekendFontColor = Color.red; Color controlLineColor = Color.GREEN; Color controlTextColor = Color.white; Color controlMoonnameTextColor = Color.BLUE; JSpinner yearSpin; JSpinner monthSpin; JButton[][] daysButton = new JButton[6][7]; DatePanel() { setLayout(new BorderLayout()); setBorder(new LineBorder(backGroundColor, 2)); setBackground(backGroundColor); JPanel topYearAndMonth = createYearAndMonthPanal(); add(topYearAndMonth, BorderLayout.NORTH); JPanel centerWeekAndDay = createWeekAndDayPanal(); add(centerWeekAndDay, BorderLayout.CENTER); reflushWeekAndDay(); } private JPanel createYearAndMonthPanal() { Calendar cal = getCalendar(); int currentYear = cal.get(Calendar.YEAR); int currentMonth = cal.get(Calendar.MONTH) + 1; JPanel panel = new JPanel(); panel.setLayout(new FlowLayout()); panel.setBackground(controlLineColor); JLabel yearLabel = new JLabel("Year"); yearLabel.setForeground(controlTextColor); panel.add(yearLabel); yearSpin = new JSpinner(new SpinnerNumberModel(currentYear, startYear, lastYear, 1)); yearSpin.setPreferredSize(new Dimension(60, 30)); yearSpin.setName("Year"); yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####")); yearSpin.addChangeListener(this); panel.add(yearSpin); monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1, 12, 1)); monthSpin.setPreferredSize(new Dimension(45, 30)); monthSpin.setName("Month"); monthSpin.addChangeListener(this); monthNameLabel.setPreferredSize(new Dimension(60, 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[] = {"سب", "اح", "ثن", "ثل", "ار", "خم", "جم"}; // String colname[] = {"Sa", "sun", "Mon", "Tu", "We", "Th", "Fr"}; JPanel panel = new JPanel(); panel.setFont(new Font("Arial", Font.PLAIN, 10)); panel.setLayout(new GridLayout(7, 7)); panel.setBackground(Color.white); for (int i = 0; i < 7; i++) { JLabel cell = new JLabel(colname[i]); cell.setHorizontalAlignment(JLabel.CENTER); if (i == 6) { cell.setForeground(weekendFontColor); } else { cell.setForeground(weekFontColor); } panel.add(cell); } int actionCommandId = 0; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { JButton numBtn = new JButton(); numBtn.setBorder(null); numBtn.setHorizontalAlignment(SwingConstants.CENTER); numBtn.setActionCommand(String.valueOf(actionCommandId)); numBtn.setBackground(palletTableColor); numBtn.setForeground(dateFontColor); numBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JButton source = (JButton) event.getSource(); if (source.getText().length() == 0) { return; } dayColorUpdate(true); source.setForeground(todayBackColor); int newDay = Integer.parseInt(source.getText()); Calendar cal = getCalendar(); cal.set(Calendar.DAY_OF_MONTH, newDay); setDate(cal.getTime()); dateDialog.setVisible(false); } }); if (j == 6) { numBtn.setForeground(weekendFontColor); } else { numBtn.setForeground(dateFontColor); } daysButton[i][j] = numBtn; panel.add(numBtn); actionCommandId++; } } return panel; } private Calendar getCalendar() { Calendar calendar = Calendar.getInstance(); calendar.setTime(getDate()); return calendar; } private int getSelectedYear() { return ((Integer) yearSpin.getValue()).intValue(); } private int getSelectedMonth() { return ((Integer) monthSpin.getValue()).intValue(); } private void dayColorUpdate(boolean isOldDay) { 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 - 3 + WeekdayNumb; int i = actionCommandId / 7; int j = actionCommandId % 7; if (isOldDay) { daysButton[i][j].setForeground(dateFontColor); } else { daysButton[i][j].setForeground(todayBackColor); } } private void reflushWeekAndDay() { 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; int dayNo = 3 - WeekdayNumb; 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++; } } 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[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 ---
It is very interesting to me that there is no component for selecting the day for Hijri. I wonder if there is no Java programmer in the Arabs?
Last edited by cnmeysam; April 28th, 2021 at 01:44 PM.
How are you trying to debug the code to see what happens? Add some print statements that print out the values of the variables that are used in the code that fills in the month days so you can see what is happening.In the second and eleventh month date start from the second day
The program starts filling in the values from the first row. and in these 2 months starts from the zero row .
What value should dayNo have for the day of 1 to be in the last column?
Why is there a Date object passed to the constructor and to the setDate method?
Its value is never used. It should be removed.
If you don't understand my answer, don't ignore it, ask a question.
when i remove setDate(date); i see this errors
Exception in thread "AWT-EventQueue-0" java.time.DateTimeException: Invalid Hijrah date, year: 2021, month: 4 at java.time.chrono.HijrahChronology.getEpochDay(HijrahChronology.java:674) at java.time.chrono.HijrahDate.<init>(HijrahDate.java:270) at java.time.chrono.HijrahDate.of(HijrahDate.java:156) at java.time.chrono.HijrahChronology.date(HijrahChronology.java:453) at Hijrah$DatePanel.reflushWeekAndDay(Hijrah.java:369) at Hijrah$DatePanel.<init>(Hijrah.java:211) at Hijrah$1.mouseClicked(Hijrah.java:96) at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270) at java.awt.Component.processMouseEvent(Component.java:6536) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6298) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Last edited by cnmeysam; April 28th, 2021 at 02:28 PM.
What did you remove? I suggested removing the Date object. So the call to setDate would be:setDate(); // without Date object
And also remove the Date from the constructor:
public Hijrah5(String dateFormatPattern) { this(); DEFAULT_DATE_FORMAT = dateFormatPattern; } public Hijrah5() { hdate(); setDate(); setEditable(false); setCursor(new Cursor(Cursor.HAND_CURSOR)); addListeners(); }
If you don't understand my answer, don't ignore it, ask a question.
I was able to add another row to the buttons but I do not know how to enter the number of days in them correctly.
That is, the months that start from 2 start their first day from the first line and the ones that are right now currect from the next line
Is the problem that the row with a 1 at the end is not shown?
And the the first number on the first row is a 2?
What value needs to be in dayNo for the 1 to be placed in the correct column on the first row?
If you don't understand my answer, don't ignore it, ask a question.
when i remove date form setDate(date); i see this
method setDate in class Hijrah cannot be applied to given types;
required: Date
found: no arguments
reason: actual and formal argument lists differ in length
--- Update ---
yes the problem that the row with a 1 at the end is not shown And the the first number on the first row is a 2
What value needs to be in dayNo for the 1 to be placed in the correct column on the first row? i don't know
Remove the Date object from the setDate method. The Date object is not used.method setDate in class Hijrah cannot be applied to given types;
Study the code to see how the value in dayNo is used to determine what column the 1 is placed in and how the 2 goes in the next column, etcWhat value needs to be in dayNo for the 1 to be placed in the correct column on the first row? i don't know
If you don't understand my answer, don't ignore it, ask a question.
i think i must edit this part of code for days but i don't know how
private JPanel createWeekAndDayPanal() { String colname[] = {"سب", "اح", "ثن", "ثل", "ار", "خم", "جم"}; // String colname[] = {"Sa", "sun", "Mon", "Tu", "We", "Th", "Fr"}; JPanel panel = new JPanel(); panel.setFont(new Font("Arial", Font.PLAIN, 10)); panel.setLayout(new GridLayout(7, 7)); panel.setBackground(Color.white); for (int i = 0; i < 7; i++) { JLabel cell = new JLabel(colname[i]); cell.setHorizontalAlignment(JLabel.CENTER); if (i == 6) { cell.setForeground(weekendFontColor); } else { cell.setForeground(weekFontColor); } panel.add(cell); } int actionCommandId = 0; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { JButton numBtn = new JButton(); numBtn.setBorder(null); numBtn.setHorizontalAlignment(SwingConstants.CENTER); numBtn.setActionCommand(String.valueOf(actionCommandId)); numBtn.setBackground(palletTableColor); numBtn.setForeground(dateFontColor); numBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JButton source = (JButton) event.getSource(); if (source.getText().length() == 0) { return; } dayColorUpdate(true); source.setForeground(todayBackColor); int newDay = Integer.parseInt(source.getText()); Calendar cal = getCalendar(); cal.set(Calendar.DAY_OF_MONTH, newDay); setDate(cal.getTime()); dateDialog.setVisible(false); } }); if (j == 6) { numBtn.setForeground(weekendFontColor); } else { numBtn.setForeground(dateFontColor); } daysButton[i][j] = numBtn; panel.add(numBtn); actionCommandId++; } } return panel; }
Before you edit any code, you need to understand what the desired results are. What does the code need to do?i think i must edit this part of code for days but i don't know how
How can the code be modified to do it?
You need to do this:
Study the code to see how the value in dayNo is used to determine what column the 1 is placed in and how the 2 goes in the next column, etc
Use a paper an pencil with 7 columns to determine the value of dayNo for the 1 to go in each column. Look at the debug print outs the code makes now to see what the dayNo value is for each placement of the 1 on the first row.
If you don't understand my answer, don't ignore it, ask a question.
int dayNo = 4 - WeekdayNumb;
That is, on which day of the week we are on, 4 days are reduced and the remaining 3 days are the number of days of the day, so that each number is displayed on its own day.
But when I lower it, the values are still not in place
Last edited by cnmeysam; April 28th, 2021 at 03:21 PM.
What value needs to be in dayNo for the 1 to be placed in the last column?
Look at the code to see how dayNo is used.
If you don't understand my answer, don't ignore it, ask a question.
For the sixth month, when the day starts from 3, 11 numbers should be added, that is, instead of 4, we should write +11.
But when I use the + sign even with 0, the number of days is too much to go back
--- Update ---
i think must write like this
int dayNo = -4 - WeekdayNumb;
You keep skipping answering this question. I'll repeat it:
What value needs to be in dayNo for the 1 to be placed in the last column?
If you don't understand my answer, don't ignore it, ask a question.
i think zero
I think 1 should be in the first column, not the last, so I'm confused about your question
--- Update ---
I think there are only 2 problems left
When we go to the next month or before with a spinner, the color of the buttons does not work properly
And whether the text value can be empty when loading and the user can click on the button instead of text to enter
--- Update ---
/* * 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"}; 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(new Date()); // } 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 = 1380; int lastYear = 3050; Color backGroundColor = Color.gray; Color palletTableColor = Color.white; Color todayBackColor = Color.orange; Color weekFontColor = Color.blue; Color dateFontColor = Color.black; Color weekendFontColor = Color.red; Color controlLineColor = Color.GREEN; Color controlTextColor = Color.white; Color controlMoonnameTextColor = Color.BLUE; JSpinner yearSpin; JSpinner monthSpin; JButton[][] daysButton = new JButton[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() { 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(60, 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[] = {"سب", "اح", "ثن", "ثل", "ار", "خم", "جم"}; // String colname[] = {"Sa", "sun", "Mon", "Tu", "We", "Th", "Fr"}; JPanel panel = new JPanel(); panel.setFont(new Font("Arial", Font.PLAIN, 10)); panel.setLayout(new GridLayout(7, 7)); panel.setBackground(Color.white); for (int i = 0; i < 7; i++) { JLabel cell = new JLabel(colname[i]); cell.setHorizontalAlignment(JLabel.CENTER); if (i == 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(); dateDialog.setVisible(false); } }); if (j == 6) { numBtn.setForeground(weekendFontColor); } else { numBtn.setForeground(dateFontColor); } daysButton[i][j] = numBtn; panel.add(numBtn); actionCommandId++; } } return panel; } private Calendar getCalendar() { Calendar calendar = Calendar.getInstance(); calendar.setTime(getDate()); return calendar; } private int getSelectedYear() { return ((Integer) yearSpin.getValue()).intValue(); } private int getSelectedMonth() { return ((Integer) monthSpin.getValue()).intValue(); } private void dayColorUpdate(boolean isOldDay) { 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 + 4 + WeekdayNumb; int i = actionCommandId / 7; int j = actionCommandId % 7; if (isOldDay) { daysButton[i][j].setForeground(dateFontColor); } else { daysButton[i][j].setForeground(todayBackColor); } } private void reflushWeekAndDay() { 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 = -4 - WeekdayNumb; 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++; } } 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 } }
If dayNo is 1 what column will have the 1?
Add a test line of code that sets the value of dayNo then run the code and see what column the 1 is in.
For example:Then change the 1 to a 0 and see what happens.int dayNo = 3 - WeekdayNumb; dayNo = 1; //*** Testing what column will 1 be in ???
Only work on one problem at a time. Currently want the 1 to be in the correct column.there are only 2 problems left
If you don't understand my answer, don't ignore it, ask a question.
when dayNo =1 start day is 1 and when dayNo =0 start day is empty
now When we go to the next month or before with a spinner, the color of the buttons does not work properly
That is strange. For me when I set dayNo to 0, the 1 in placed in the second column.when dayNo =0 start day is empty
Do you mean that the first column is blank and the 1 is in the second column?
Isn't that what is needed if the first day of the month is on the second day of the week?
What if the first day of the month is on the last day of the week? What value does dayNo need to put the 1 in the last column?
If you don't understand my answer, don't ignore it, ask a question.
it is becuse of this
int dayNo = -4 - WeekdayNumb; int actionCommandId = moondayNumb + 4 + WeekdayNumb;
Because the settings have changed after applying changes in the code to delete the date this is my new codes
now When we go to the next month or before with a spinner, the color of the buttons does not work properly/* * 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"}; 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(new Date()); // } 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 = 1380; int lastYear = 3050; Color backGroundColor = Color.gray; Color palletTableColor = Color.white; Color todayBackColor = Color.orange; Color weekFontColor = Color.blue; Color dateFontColor = Color.black; Color weekendFontColor = Color.red; Color controlLineColor = Color.GREEN; Color controlTextColor = 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, 12); 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(60, 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() { String colname[] = {"سب", "اح", "ثن", "ثل", "ار", "خم", "جم"}; // String colname[] = {"Sa", "sun", "Mon", "Tu", "We", "Th", "Fr"}; JPanel panel = new JPanel(); panel.setFont(new Font("Arial", Font.PLAIN, 10)); panel.setLayout(new GridLayout(7, 7)); panel.setBackground(Color.white); for (int i = 0; i < 7; i++) { JLabel cell = new JLabel(colname[i]); cell.setHorizontalAlignment(JLabel.CENTER); if (i == 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(); dateDialog.setVisible(false); } }); if (j == 6) { numBtn.setForeground(weekendFontColor); } else { numBtn.setForeground(dateFontColor); } daysButton[i][j] = numBtn; panel.add(numBtn); actionCommandId++; } } return panel; } private Calendar getCalendar() { Calendar calendar = Calendar.getInstance(); calendar.setTime(getDate()); return calendar; } private int getSelectedYear() { return ((Integer) yearSpin.getValue()).intValue(); } private int getSelectedMonth() { return ((Integer) monthSpin.getValue()).intValue(); } private void dayColorUpdate(boolean isOldDay) { 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 + 4 + WeekdayNumb; int i = actionCommandId / 7; int j = actionCommandId % 7; if (isOldDay) { daysButton[i][j].setForeground(dateFontColor); } else { daysButton[i][j].setForeground(todayBackColor); } } private void reflushWeekAndDay() { 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 = -4 - 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++; } } 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 } }
how i can fix that?