i know but how
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.
i know but how
Creating a class is the basic part of java programming. I am not a tutor so I can not begin to write a manual on creating a class. There are many on the internet.
I'd start with these steps:
Make a list of what data the class will hold
and what methods it will have to access/update the data.
Add methods as the need comes.
If you don't understand my answer, don't ignore it, ask a question.
something like this?
import java.time.chrono.HijrahDate; import java.time.format.DateTimeFormatter; public class hijridate { public void hijridate() { HijrahDate hijrahDate = HijrahDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); String formattedhijri = formatter.format(hijrahDate); // 07/03/1439 System.out.println(formattedhijri); } public static void main(String args[]) { hijridate hijridate = new hijridate(); hijridate.hijridate(); } public void tst() { hijridate(); // no errors } } }
Last edited by cnmeysam; April 27th, 2021 at 02:54 PM.
You need to write down the design for the class before coding it.
The statements in that example class might be useful for the design, I don't know.
If you don't understand my answer, don't ignore it, ask a question.
you main like last post? it must print hiji date?
--- Update ---
or like this?
import java.time.chrono.HijrahChronology; import java.time.chrono.HijrahDate; import java.time.format.DateTimeFormatter; public class hijridate { int year ; int month; int day; public void hijridate() { HijrahDate today = HijrahChronology.INSTANCE.date(year, month, day); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); String formattedhijri = formatter.format(today); // 07/03/1439 System.out.println(formattedhijri); } public static void main(String args[]) { hijridate hijridate = new hijridate(); hijridate.hijridate(); } public void tst() { hijridate(); // no errors } }
Can you describe what that class is for? What data does it hold?
How can the data it holds be accessed? What methods would it need to be useful in your project.
Notes:
Java class names should start with an uppercase letter.
There is no need for a main method in this class. It will never be executed. Its purpose is to contain data and provide methods to access that data.
If you don't understand my answer, don't ignore it, ask a question.
in The gregory use Calendar and Date libraries , so there is no need to create a class. Why should a new class be created in the Hijrah that uses the HijrahDate library?
Ok, if everything you need is in an existing class, there would not be any need for you to create one.
Now having all the classes and methods you need, what problems are you having using the dialog to update the date that is displayed in the text field?
If you don't understand my answer, don't ignore it, ask a question.
i need year month number and day number and in library i have them but i cant update jbox after click on spinner year or month
Locale ar = new Locale("ar"); HijrahDate hijrahDate = HijrahDate.now(); System.out.println("hijrahDate= " + hijrahDate);// this is show full date // month DateTimeFormatter MoonNum = DateTimeFormatter.ofPattern("M", ar); String MoondayNum = hijrahDate.format(MoonNum); int monthnumber = Integer.parseInt(MoondayNum); System.out.println("Month Number " + monthnumber);// this is show Month Number DateTimeFormatter MonthName = DateTimeFormatter.ofPattern("L", ar); String MonthNames = hijrahDate.format(MonthName); System.out.println("Month Name " + MonthNames);// this is show Month Names DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar); String dayMoondayNum = hijrahDate.format(dayOfMoonNum); int daymoondayNumb = Integer.parseInt(dayMoondayNum); System.out.println("Number day-of-month " + daymoondayNumb);// this is show days of month number (Today's issue this month) //week DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar); String WeekdayNum = hijrahDate.format(dayOfweekNum); int WeekdayNumb = Integer.parseInt(WeekdayNum); System.out.println("Number day-of-Week " + WeekdayNumb);// this is show day of week number DateTimeFormatter DayName = DateTimeFormatter.ofPattern("E", ar); String DayNames = hijrahDate.format(DayName); System.out.println("Day Name " + DayNames);// this is show Month Number //year DateTimeFormatter YearNum = DateTimeFormatter.ofPattern("u", ar); String HijriYearNum = hijrahDate.format(YearNum); int yearnumber = Integer.parseInt(HijriYearNum); System.out.println("Number of year " + yearnumber);// this is show year number int moondaysnumber = hijrahDate.lengthOfMonth(); System.out.println("Number of days of the month " + moondaysnumber);// this is show month length number
you can see all hijra Meaning here
and i can set date like thisSymbol Meaning Presentation Examples ------ ------- ------------ ------- G era text AD; Anno Domini; A u year year 2004; 04 y year-of-era year 2004; 04 D day-of-year number 189 M/L month-of-year number/text 7; 07; Jul; July; J d day-of-month number 10 Q/q quarter-of-year number/text 3; 03; Q3; 3rd quarter Y week-based-year year 1996; 96 w week-of-week-based-year number 27 W week-of-month number 4 E day-of-week text Tue; Tuesday; T e/c localized day-of-week number/text 2; 02; Tue; Tuesday; T F week-of-month number 3 a am-pm-of-day text PM h clock-hour-of-am-pm (1-12) number 12 K hour-of-am-pm (0-11) number 0 k clock-hour-of-am-pm (1-24) number 0 H hour-of-day (0-23) number 0 m minute-of-hour number 30 s second-of-minute number 55 S fraction-of-second fraction 978 A milli-of-day number 1234 n nano-of-second number 987654321 N nano-of-day number 1234000000 V time-zone ID zone-id America/Los_Angeles; Z; -08:30 z time-zone name zone-name Pacific Standard Time; PST O localized zone-offset offset-O GMT+8; GMT+08:00; UTC-08:00; X zone-offset 'Z' for zero offset-X Z; -08; -0830; -08:30; -083015; -08:30:15; x zone-offset offset-x +0000; -08; -0830; -08:30; -083015; -08:30:15; Z zone-offset offset-Z +0000; -0800; -08:00; p pad next pad modifier 1 ' escape for text delimiter '' single quote literal ' [ optional section start ] optional section end # reserved for future use { reserved for future use } reserved for future use
HijrahDate today = HijrahChronology.INSTANCE.date(year, month, day);
Sorry, I don't know what a jbox is. Please explain.cant update jbox
What value do you want to update?
If you don't understand my answer, don't ignore it, ask a question.
sorry i mean dialogBox
I still not sure what you want to update. The dialogBox has two spinners can be changed by the user.
Are you asking about how to update the days shown in the calendar so that the first day of the month starts in the correct column?
What is wrong with the code in the reflushWeekAndDay method that does that now?
I don't know the hijrah calendar to know where the first day of the current month is.
If you don't understand my answer, don't ignore it, ask a question.
when i use spinner dialog box not update
whet first time i open dialog box load current month and days Properly and when i push spinner year or month dialog still show current month dialog box will not be updated at all
this is my class all code
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Frame; import java.awt.GridLayout; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowEvent; import java.awt.event.WindowFocusListener; import java.text.Format; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.chrono.HijrahDate; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.JTextField; import javax.swing.SpinnerNumberModel; import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import javax.swing.border.LineBorder; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import java.time.chrono.HijrahChronology; import java.util.Locale; public class Hijrah extends JTextField { String testMethod() { // ADDED HijrahDate hijrahDate = HijrahDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); String formatted = formatter.format(hijrahDate); // 07/03/1439 System.out.println("hijrahDate " + formatted); return formatted.toString(); //<<<<<< ADDED } Object hijri(){ HijrahDate hijrahDate = HijrahDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); String formattedhijri = formatter.format(hijrahDate); // 07/03/1439 System.out.println("hijrahDate " + formattedhijri); return formattedhijri; } private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd"; private static final int DIALOG_WIDTH = 250; private static final int DIALOG_HEIGHT = 210; private SimpleDateFormat dateFormat; private DatePanel datePanel = null; private JDialog dateDialog = null; public Hijrah() { this(new Date()); } public Hijrah(String dateFormatPattern, Date date) { this(date); DEFAULT_DATE_FORMAT = dateFormatPattern; } public Hijrah(Date date) { setDate(date); setEditable(false); setCursor(new Cursor(Cursor.HAND_CURSOR)); addListeners(); } private void addListeners() { addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent paramMouseEvent) { if (datePanel == null) { datePanel = new DatePanel(); } Point point = getLocationOnScreen(); point.y = point.y + 30; showDateDialog(datePanel, point); } }); } private void showDateDialog(DatePanel dateChooser, Point position) { Frame owner = (Frame) SwingUtilities.getWindowAncestor(Hijrah.this); if (dateDialog == null || dateDialog.getOwner() != owner) { dateDialog = createDateDialog(owner, dateChooser); } dateDialog.setLocation(getAppropriateLocation(owner, position)); dateDialog.setVisible(true); } private JDialog createDateDialog(Frame owner, JPanel contentPanel) { JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal // JDialog dialog = new JDialog(owner, "Date Selected", true); dialog.setUndecorated(true); dialog.getContentPane().add(contentPanel, BorderLayout.CENTER); dialog.pack(); dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT); dialog.addWindowFocusListener(new WindowFocusListener() { public void windowLostFocus(WindowEvent e) { dialog.setVisible(false); } public void windowGainedFocus(WindowEvent e) { } }); return dialog; } private Point getAppropriateLocation(Frame owner, Point position) { Point result = new Point(position); Point p = owner.getLocation(); int offsetX = (position.x + DIALOG_WIDTH) - (p.x + owner.getWidth()); int offsetY = (position.y + DIALOG_HEIGHT) - (p.y + owner.getHeight()); if (offsetX > 0) { result.x -= offsetX; } if (offsetY > 0) { result.y -= offsetY; } return result; } private SimpleDateFormat getDefaultDateFormat() { if (dateFormat == null) { dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT); } return dateFormat; } public void setText(Date date) { setDate(date); } public void setDate(Date date) { // super.setText(getDefaultDateFormat().format(date) + " " + testMethod()); //<<<<< Changed super.setText(testMethod()); //<<<<< Changed } public Date getDate() { try { return getDefaultDateFormat().parse(getText()); } catch (ParseException e) { return new Date(); } } private class DatePanel extends JPanel implements ChangeListener { int startYear = 1380; int lastYear = 3050; Color backGroundColor = Color.gray; Color palletTableColor = Color.white; Color todayBackColor = Color.orange; Color weekFontColor = Color.blue; Color dateFontColor = Color.black; Color weekendFontColor = Color.red; Color controlLineColor = Color.GREEN; Color controlTextColor = Color.white; Color controlMoonnameTextColor = Color.BLUE; JSpinner yearSpin; JSpinner monthSpin; JButton[][] daysButton = new JButton[6][7]; DatePanel() { setLayout(new BorderLayout()); setBorder(new LineBorder(backGroundColor, 2)); setBackground(backGroundColor); JPanel topYearAndMonth = createYearAndMonthPanal(); add(topYearAndMonth, BorderLayout.NORTH); JPanel centerWeekAndDay = createWeekAndDayPanal(); add(centerWeekAndDay, BorderLayout.CENTER); reflushWeekAndDay(); } private JPanel createYearAndMonthPanal() { Calendar cal = getCalendar(); int currentYear = cal.get(Calendar.YEAR); int currentMonth = cal.get(Calendar.MONTH) + 1; JPanel panel = new JPanel(); panel.setLayout(new FlowLayout()); panel.setBackground(controlLineColor); JLabel yearLabel = new JLabel("Year"); yearLabel.setForeground(controlTextColor); panel.add(yearLabel); yearSpin = new JSpinner(new SpinnerNumberModel(currentYear, startYear, lastYear, 1)); yearSpin.setPreferredSize(new Dimension(60, 30)); yearSpin.setName("Year"); yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####")); yearSpin.addChangeListener(this); panel.add(yearSpin); monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1, 12, 1)); monthSpin.setPreferredSize(new Dimension(45, 30)); monthSpin.setName("Month"); monthSpin.addChangeListener(this); 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(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 == 0 || i == 6) { if (i == 6) { cell.setForeground(weekendFontColor); } else { cell.setForeground(weekFontColor); } panel.add(cell); } int actionCommandId = 0; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { JButton numBtn = new JButton(); numBtn.setBorder(null); numBtn.setHorizontalAlignment(SwingConstants.CENTER); numBtn.setActionCommand(String.valueOf(actionCommandId)); numBtn.setBackground(palletTableColor); numBtn.setForeground(dateFontColor); numBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JButton source = (JButton) event.getSource(); if (source.getText().length() == 0) { return; } dayColorUpdate(true); source.setForeground(todayBackColor); int newDay = Integer.parseInt(source.getText()); Calendar cal = getCalendar(); cal.set(Calendar.DAY_OF_MONTH, newDay); setDate(cal.getTime()); dateDialog.setVisible(false); } }); if (j == 6) { numBtn.setForeground(weekendFontColor); } else { numBtn.setForeground(dateFontColor); } daysButton[i][j] = numBtn; panel.add(numBtn); actionCommandId++; } } return panel; } private Calendar getCalendar() { Calendar calendar = Calendar.getInstance(); calendar.setTime(getDate()); return calendar; } private int getSelectedYear() { return ((Integer) yearSpin.getValue()).intValue(); } private int getSelectedMonth() { return ((Integer) monthSpin.getValue()).intValue(); } private void dayColorUpdate(boolean isOldDay) { Locale ar = new Locale("ar"); DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar); DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar); HijrahDate hijrahDate = HijrahDate.now(); String MoondayNum = hijrahDate.format(dayOfMoonNum); String WeekdayNum = hijrahDate.format(dayOfweekNum); int WeekdayNumb = Integer.parseInt(WeekdayNum); int moondayNumb = Integer.parseInt(MoondayNum); int moondaysnumber = hijrahDate.lengthOfMonth(); System.out.println("hijrahDate= " + hijrahDate); System.out.println("Number day-of-month " + MoondayNum); System.out.println("Number day-of-Week " + WeekdayNum); System.out.println("Number of days of the month " + moondaysnumber); int actionCommandId = moondayNumb - 2 + WeekdayNumb; int i = actionCommandId / 7; int j = actionCommandId % 7; if (isOldDay) { daysButton[i][j].setForeground(dateFontColor); } else { daysButton[i][j].setForeground(todayBackColor); } } private void reflushWeekAndDay() { Locale ar = new Locale("ar"); DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar); DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar); HijrahDate hijrahDate = HijrahDate.now(); String MoondayNum = hijrahDate.format(dayOfMoonNum); String WeekdayNum = hijrahDate.format(dayOfweekNum); int WeekdayNumb = Integer.parseInt(WeekdayNum); int moondayNumb = Integer.parseInt(MoondayNum); int moondaysnumber = hijrahDate.lengthOfMonth(); System.out.println("hijrahDate= " + hijrahDate); System.out.println("Number day-of-month " + moondayNumb); System.out.println("Number day-of-Week " + WeekdayNumb); System.out.println("Number of days of the month " + moondaysnumber); int dayNo = 2 - WeekdayNumb; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { String s = ""; if (dayNo >= 1 && dayNo <= moondaysnumber) { s = String.valueOf(dayNo); } daysButton[i][j].setText(s); dayNo++; } } dayColorUpdate(false); } public void stateChanged(ChangeEvent e) { dayColorUpdate(true); JSpinner source = (JSpinner) e.getSource(); Locale ar = new Locale("ar"); DateTimeFormatter YearNum = DateTimeFormatter.ofPattern("u", ar); DateTimeFormatter numOfMoonNum = DateTimeFormatter.ofPattern("M", ar); DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar); HijrahDate hijrahDate = HijrahDate.now(); HijrahChronology crono = hijrahDate.getChronology(); String HijriYearNum = hijrahDate.format(YearNum); System.out.println("YearNum " + HijriYearNum); String MoonOfYear = hijrahDate.format(numOfMoonNum); System.out.println("month-of-year " + MoonOfYear); System.out.println("crono " + crono); String dayOfMoonNums = hijrahDate.format(dayOfMoonNum); int moondayNumb = Integer.parseInt(dayOfMoonNums); System.out.println("crono " + crono); HijrahDate date; int HijriMoonOfYearb = Integer.parseInt(MoonOfYear); if (source.getName().equals("Year")) { date = crono.date(getSelectedYear(), HijriMoonOfYearb, moondayNumb); hijrahDate=date; System.out.println("date Year " + date); } else { date = crono.date(getSelectedYear(), getSelectedMonth(), moondayNumb); hijrahDate=date; System.out.println("date MONTH " + date); } hijrahDate=date; reflushWeekAndDay(); } } }
https://en.wikipedia.org/wiki/Islamic_calendar
Last edited by cnmeysam; April 27th, 2021 at 04:25 PM.
Where is the date being stored? Where is the method that updates that date?
The date displayed in the text field comes from the testMethod which always gets the current date(HijrahDate.now()) and not the date that should be available for updating.
So the program is only using the current date. It does not have a date that can be updated. Therefore the date that is displayed is always the same.
If you don't understand my answer, don't ignore it, ask a question.
That is, define a global variable and record the date of the variable for tax?like this
int ye = 1442; int mo = 10; int da = 10; String testMethod() { // ADDED HijrahDate hijrahDate = HijrahDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); String formattedd = formatter.format(hijrahDate); // 07/03/1439 ye = Integer.parseInt(formattedd.substring(0, 4)); mo = Integer.parseInt(formattedd.substring(5, 7)); da = Integer.parseInt(formattedd.substring(8, 10)); 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 }
Last edited by cnmeysam; April 27th, 2021 at 05:39 PM.
Does that code solve the problem?
If you don't understand my answer, don't ignore it, ask a question.
i dont know how i must value of sppiner set to ye and mo
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Frame; import java.awt.GridLayout; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowEvent; import java.awt.event.WindowFocusListener; import java.text.Format; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.chrono.HijrahDate; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.JTextField; import javax.swing.SpinnerNumberModel; import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import javax.swing.border.LineBorder; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import java.time.chrono.HijrahChronology; import java.util.Locale; public class Hijrah extends JTextField { int ye = 1442; int mo = 10; int da = 10; JSpinner yearSpin; JSpinner monthSpin; Object hijri() { HijrahDate hijrahDate = HijrahDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); String formattedhijri = formatter.format(hijrahDate); // 07/03/1439 System.out.println("hijrahDate " + formattedhijri); return formattedhijri; } private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd"; private static final int DIALOG_WIDTH = 250; private static final int DIALOG_HEIGHT = 210; private SimpleDateFormat dateFormat; private DatePanel datePanel = null; private JDialog dateDialog = null; public Hijrah() { this(new Date()); } public Hijrah(String dateFormatPattern, Date date) { this(date); DEFAULT_DATE_FORMAT = dateFormatPattern; } public Hijrah(Date date) { setDate(date); setEditable(false); setCursor(new Cursor(Cursor.HAND_CURSOR)); addListeners(); } private void addListeners() { addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent paramMouseEvent) { if (datePanel == null) { datePanel = new DatePanel(); } Point point = getLocationOnScreen(); point.y = point.y + 30; showDateDialog(datePanel, point); } }); } private void showDateDialog(DatePanel dateChooser, Point position) { Frame owner = (Frame) SwingUtilities.getWindowAncestor(Hijrah.this); if (dateDialog == null || dateDialog.getOwner() != owner) { dateDialog = createDateDialog(owner, dateChooser); } dateDialog.setLocation(getAppropriateLocation(owner, position)); dateDialog.setVisible(true); } private JDialog createDateDialog(Frame owner, JPanel contentPanel) { JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal // JDialog dialog = new JDialog(owner, "Date Selected", true); dialog.setUndecorated(true); dialog.getContentPane().add(contentPanel, BorderLayout.CENTER); dialog.pack(); dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT); dialog.addWindowFocusListener(new WindowFocusListener() { public void windowLostFocus(WindowEvent e) { dialog.setVisible(false); } public void windowGainedFocus(WindowEvent e) { } }); return dialog; } private Point getAppropriateLocation(Frame owner, Point position) { Point result = new Point(position); Point p = owner.getLocation(); int offsetX = (position.x + DIALOG_WIDTH) - (p.x + owner.getWidth()); int offsetY = (position.y + DIALOG_HEIGHT) - (p.y + owner.getHeight()); if (offsetX > 0) { result.x -= offsetX; } if (offsetY > 0) { result.y -= offsetY; } return result; } private SimpleDateFormat getDefaultDateFormat() { if (dateFormat == null) { dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT); } return dateFormat; } public void setText(Date date) { setDate(date); } public Date getDate() { try { return getDefaultDateFormat().parse(getText()); } catch (ParseException e) { return new Date(); } } String testMethod() { // ADDED HijrahDate hijrahDate = HijrahDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); String formattedd = formatter.format(hijrahDate); // 07/03/1439 ye = Integer.parseInt(formattedd.substring(0, 4)); mo = Integer.parseInt(formattedd.substring(5, 7)); da = Integer.parseInt(formattedd.substring(8, 10)); 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 } public void setDate(Date date) { // super.setText(getDefaultDateFormat().format(date) + " " + testMethod()); //<<<<< Changed super.setText(testMethod()); //<<<<< Changed } 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; 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); 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(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 == 0 || i == 6) { if (i == 6) { cell.setForeground(weekendFontColor); } else { cell.setForeground(weekFontColor); } panel.add(cell); } int actionCommandId = 0; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { JButton numBtn = new JButton(); numBtn.setBorder(null); numBtn.setHorizontalAlignment(SwingConstants.CENTER); numBtn.setActionCommand(String.valueOf(actionCommandId)); numBtn.setBackground(palletTableColor); numBtn.setForeground(dateFontColor); numBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JButton source = (JButton) event.getSource(); if (source.getText().length() == 0) { return; } dayColorUpdate(true); source.setForeground(todayBackColor); int newDay = Integer.parseInt(source.getText()); Calendar cal = getCalendar(); cal.set(Calendar.DAY_OF_MONTH, newDay); setDate(cal.getTime()); dateDialog.setVisible(false); } }); if (j == 6) { numBtn.setForeground(weekendFontColor); } else { numBtn.setForeground(dateFontColor); } daysButton[i][j] = numBtn; panel.add(numBtn); actionCommandId++; } } return panel; } private Calendar getCalendar() { Calendar calendar = Calendar.getInstance(); calendar.setTime(getDate()); return calendar; } private int getSelectedYear() { return ((Integer) yearSpin.getValue()).intValue(); } private int getSelectedMonth() { return ((Integer) monthSpin.getValue()).intValue(); } private void dayColorUpdate(boolean isOldDay) { Locale ar = new Locale("ar"); DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar); DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar); HijrahDate hijrahDate = HijrahDate.now(); String MoondayNum = hijrahDate.format(dayOfMoonNum); String WeekdayNum = hijrahDate.format(dayOfweekNum); int WeekdayNumb = Integer.parseInt(WeekdayNum); int moondayNumb = Integer.parseInt(MoondayNum); int moondaysnumber = hijrahDate.lengthOfMonth(); System.out.println("hijrahDate= " + hijrahDate); System.out.println("Number day-of-month " + MoondayNum); System.out.println("Number day-of-Week " + WeekdayNum); System.out.println("Number of days of the month " + moondaysnumber); int actionCommandId = moondayNumb - 2 + WeekdayNumb; int i = actionCommandId / 7; int j = actionCommandId % 7; if (isOldDay) { daysButton[i][j].setForeground(dateFontColor); } else { daysButton[i][j].setForeground(todayBackColor); } } private void reflushWeekAndDay() { Locale ar = new Locale("ar"); DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar); DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar); HijrahDate hijrahDate = HijrahDate.now(); String MoondayNum = hijrahDate.format(dayOfMoonNum); String WeekdayNum = hijrahDate.format(dayOfweekNum); int WeekdayNumb = Integer.parseInt(WeekdayNum); int moondayNumb = Integer.parseInt(MoondayNum); int moondaysnumber = hijrahDate.lengthOfMonth(); System.out.println("hijrahDate= " + hijrahDate); System.out.println("Number day-of-month " + moondayNumb); System.out.println("Number day-of-Week " + WeekdayNumb); System.out.println("Number of days of the month " + moondaysnumber); int dayNo = 2 - WeekdayNumb; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { String s = ""; if (dayNo >= 1 && dayNo <= moondaysnumber) { s = String.valueOf(dayNo); } daysButton[i][j].setText(s); dayNo++; } } dayColorUpdate(false); } public void stateChanged(ChangeEvent e) { dayColorUpdate(true); JSpinner source = (JSpinner) e.getSource(); Locale ar = new Locale("ar"); DateTimeFormatter YearNum = DateTimeFormatter.ofPattern("u", ar); DateTimeFormatter numOfMoonNum = DateTimeFormatter.ofPattern("M", ar); DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar); HijrahDate hijrahDate = HijrahDate.now(); HijrahChronology crono = hijrahDate.getChronology(); String HijriYearNum = hijrahDate.format(YearNum); System.out.println("YearNum " + HijriYearNum); String MoonOfYear = hijrahDate.format(numOfMoonNum); System.out.println("month-of-year " + MoonOfYear); System.out.println("crono " + crono); String dayOfMoonNums = hijrahDate.format(dayOfMoonNum); int moondayNumb = Integer.parseInt(dayOfMoonNums); System.out.println("crono " + crono); HijrahDate date; int HijriMoonOfYearb = Integer.parseInt(MoonOfYear); if (source.getName().equals("Year")) { date = crono.date(getSelectedYear(), HijriMoonOfYearb, moondayNumb); hijrahDate = date; System.out.println("date Year " + date); } else { date = crono.date(getSelectedYear(), getSelectedMonth(), moondayNumb); hijrahDate = date; System.out.println("date MONTH " + date); } hijrahDate = date; reflushWeekAndDay(); } } }
Did the code in the NewClass class correctly get and use values from the spinners for year and month?
Can the technique used in NewClass be used in your new class?
If you don't understand my answer, don't ignore it, ask a question.
cnmeysam (April 27th, 2021)
in the Gregory works currently but in Hijra i don't know but numbers is correct and spin work
That is probably code that you need to write.but in Hijra i don't know
But first you need to write down (design) what you want the code to do. Once you have a definition, then write code.
If you don't understand my answer, don't ignore it, ask a question.
in spinner year and month load hijra date ( year and month ) load correctly
but when i change value of spinners in text month and year and in jdialog date not updated
in define 2 global Variable named ye and mo
But after add spinner values to them, updates are not done
I do not know how to put these values in the variables
i change code like this
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Frame; import java.awt.GridLayout; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowEvent; import java.awt.event.WindowFocusListener; import java.text.Format; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.chrono.HijrahDate; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.JTextField; import javax.swing.SpinnerNumberModel; import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import javax.swing.border.LineBorder; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import java.time.chrono.HijrahChronology; import java.util.Locale; public class Hijrah extends JTextField { int ye = 1442; int mo = 10; int da = 10; JSpinner yearSpin; JSpinner monthSpin; private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd"; private static final int DIALOG_WIDTH = 250; private static final int DIALOG_HEIGHT = 210; private SimpleDateFormat dateFormat; private DatePanel datePanel = null; private JDialog dateDialog = null; 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; 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)); ye = (Integer) yearSpin.getValue(); 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)); mo = (Integer) monthSpin.getValue(); monthSpin.setPreferredSize(new Dimension(45, 30)); monthSpin.setName("Month"); monthSpin.addChangeListener(this); 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(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 == 0 || i == 6) { if (i == 6) { cell.setForeground(weekendFontColor); } else { cell.setForeground(weekFontColor); } panel.add(cell); } int actionCommandId = 0; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { JButton numBtn = new JButton(); numBtn.setBorder(null); numBtn.setHorizontalAlignment(SwingConstants.CENTER); numBtn.setActionCommand(String.valueOf(actionCommandId)); numBtn.setBackground(palletTableColor); numBtn.setForeground(dateFontColor); numBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JButton source = (JButton) event.getSource(); if (source.getText().length() == 0) { return; } dayColorUpdate(true); source.setForeground(todayBackColor); int newDay = Integer.parseInt(source.getText()); Calendar cal = getCalendar(); cal.set(Calendar.DAY_OF_MONTH, newDay); setDate(cal.getTime()); dateDialog.setVisible(false); } }); if (j == 6) { numBtn.setForeground(weekendFontColor); } else { numBtn.setForeground(dateFontColor); } daysButton[i][j] = numBtn; panel.add(numBtn); actionCommandId++; } } return panel; } private Calendar getCalendar() { Calendar calendar = Calendar.getInstance(); calendar.setTime(getDate()); return calendar; } private int getSelectedYear() { return ye; // return ((Integer) yearSpin.getValue()).intValue(); } private int getSelectedMonth() { return mo; // return ((Integer) monthSpin.getValue()).intValue(); } private void dayColorUpdate(boolean isOldDay) { Locale ar = new Locale("ar"); DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar); DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar); HijrahDate hijrahDate = HijrahDate.now(); String MoondayNum = hijrahDate.format(dayOfMoonNum); String WeekdayNum = hijrahDate.format(dayOfweekNum); int WeekdayNumb = Integer.parseInt(WeekdayNum); int moondayNumb = Integer.parseInt(MoondayNum); int moondaysnumber = hijrahDate.lengthOfMonth(); System.out.println("hijrahDate= " + hijrahDate); System.out.println("Number day-of-month " + MoondayNum); System.out.println("Number day-of-Week " + WeekdayNum); System.out.println("Number of days of the month " + moondaysnumber); int actionCommandId = moondayNumb - 2 + WeekdayNumb; int i = actionCommandId / 7; int j = actionCommandId % 7; if (isOldDay) { daysButton[i][j].setForeground(dateFontColor); } else { daysButton[i][j].setForeground(todayBackColor); } } private void reflushWeekAndDay() { Locale ar = new Locale("ar"); DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar); DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar); HijrahDate hijrahDate = HijrahDate.now(); String MoondayNum = hijrahDate.format(dayOfMoonNum); String WeekdayNum = hijrahDate.format(dayOfweekNum); int WeekdayNumb = Integer.parseInt(WeekdayNum); int moondayNumb = Integer.parseInt(MoondayNum); int moondaysnumber = hijrahDate.lengthOfMonth(); System.out.println("hijrahDate= " + hijrahDate); System.out.println("Number day-of-month " + moondayNumb); System.out.println("Number day-of-Week " + WeekdayNumb); System.out.println("Number of days of the month " + moondaysnumber); int dayNo = 2 - WeekdayNumb; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { String s = ""; if (dayNo >= 1 && dayNo <= moondaysnumber) { s = String.valueOf(dayNo); } daysButton[i][j].setText(s); dayNo++; } } dayColorUpdate(false); } public void stateChanged(ChangeEvent e) { dayColorUpdate(true); JSpinner source = (JSpinner) e.getSource(); Locale ar = new Locale("ar"); DateTimeFormatter YearNum = DateTimeFormatter.ofPattern("u", ar); DateTimeFormatter numOfMoonNum = DateTimeFormatter.ofPattern("M", ar); DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar); // HijrahDate hijrahDate = HijrahDate.now(); HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(ye, mo, da); HijrahChronology crono = hijrahDate.getChronology(); String HijriYearNum = hijrahDate.format(YearNum); System.out.println("YearNum " + HijriYearNum); String MoonOfYear = hijrahDate.format(numOfMoonNum); System.out.println("month-of-year " + MoonOfYear); System.out.println("crono " + crono); String dayOfMoonNums = hijrahDate.format(dayOfMoonNum); int moondayNumb = Integer.parseInt(dayOfMoonNums); System.out.println("crono " + crono); HijrahDate date; int HijriMoonOfYearb = Integer.parseInt(MoonOfYear); if (source.getName().equals("Year")) { date = crono.date(getSelectedYear(), HijriMoonOfYearb, moondayNumb); hijrahDate = date; System.out.println("date Year " + date); } else { date = crono.date(getSelectedYear(), getSelectedMonth(), moondayNumb); hijrahDate = date; System.out.println("date MONTH " + date); } hijrahDate = date; reflushWeekAndDay(); } } 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 Date getDate() { try { return getDefaultDateFormat().parse(getText()); } catch (ParseException e) { return new Date(); } } String testMethod() { // ADDED // HijrahDate hijrahDate = HijrahDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); // String formattedd = formatter.format(hijrahDate); // 07/03/1439 // // ye = Integer.parseInt(formattedd.substring(0, 4)); // mo = Integer.parseInt(formattedd.substring(5, 7)); // da = Integer.parseInt(formattedd.substring(8, 10)); 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 } public void setDate(Date date) { // super.setText(getDefaultDateFormat().format(date) + " " + testMethod()); //<<<<< Changed super.setText(testMethod()); //<<<<< Changed } }
Last edited by cnmeysam; April 27th, 2021 at 06:17 PM.
Where is the ye value set in the stateChanged method that is called when the spinner is used?
The reflushWeekAndDay method does not use the value in ye, it still uses: HijrahDate hijrahDate = HijrahDate.now();
Describe how the value selected by the spinner makes it to the reflushWeekAndDay method.
The setDate method is passed a Date object which is does not use? Why pass a value if the method does not use it?
getSelectedYear returns ye and not the selected spinner value? Why?
Too many errors. You need to start at the beginning and work through the code and trace how a value goes from the spinner to what is displayed in the text field.
If you don't understand my answer, don't ignore it, ask a question.
The amount of data get from hijrayearSpin = new JSpinner(new SpinnerNumberModel(currentYear, startYear, lastYear, 1)); ye = (Integer) yearSpin.getValue(); monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1, 12, 1)); mo = (Integer) monthSpin.getValue();
i fix getSelectedYear but don't happened anything
--- Update ---
can you fix that and send here?
Try debugging the code to see what happens to the year value selected by the spinner.
Add a print statement at each method where the ye is used that prints the name of the method and the value of ye or the object that contains the value of ye like a String or a date object.
Start at the stateChanged event and continue through where the text field's setText method is called.
The print outs should show you where the value of ye is not being used or is missing.
Make the print outs unique so they are easy to find amongst all the other print outs.
If you don't understand my answer, don't ignore it, ask a question.
this is my last code
now update text and dialogbox after push spinners/* * 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() { if (mo == 1) { monthNameLabel.setText("ٱلْمُحَرَّم"); } else if (mo == 2) { monthNameLabel.setText("صَفَر"); } else if (mo == 3) { monthNameLabel.setText("رَبِيع ٱلْأَوَّل"); } else if (mo == 4) { monthNameLabel.setText("رَبِيع ٱلثَّانِي"); } else if (mo == 5) { monthNameLabel.setText("جُمَادَىٰ ٱلْأُولَىٰ"); } else if (mo == 6) { monthNameLabel.setText("جُمَادَىٰ ٱلثَّانِيَة"); } else if (mo == 7) { monthNameLabel.setText("رَجَب"); } else if (mo == 8) { monthNameLabel.setText("شَعْبَان"); } else if (mo == 9) { monthNameLabel.setText("رَمَضَان"); } else if (mo == 10) { monthNameLabel.setText("شَوَّال"); } else if (mo == 11) { monthNameLabel.setText("ذُو ٱلْقَعْدَة"); } else if (mo == 12) { monthNameLabel.setText("ذُو ٱلْحِجَّة"); } } 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 } }
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
Last edited by cnmeysam; April 28th, 2021 at 11:56 AM.