that is not a class in IDe we can create a package (like new folder) and put our class and forms or picture inside of them To categorize and Grouping them i just forgot when restore class remove it from top of class
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.
that is not a class in IDe we can create a package (like new folder) and put our class and forms or picture inside of them To categorize and Grouping them i just forgot when restore class remove it from top of class
Last edited by cnmeysam; April 30th, 2021 at 07:06 PM.
the choosdate and datetext variables do not have values.
If you don't understand my answer, don't ignore it, ask a question.
choosdate is button don't need variable and value for click and load jdialog
datetext variable is ye mo da and this variable fill from here
and i call it here top of load classpublic 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() { hdate();// load ye mo da for variable's choosdate.setText("choos date"); add(datePanels); datePanels.setPreferredSize(new Dimension(640, 480)); datePanels.setLayout(new FlowLayout()); try { img = ImageIO.read(getClass().getResource("cleander.png")); choosdate.setIcon(new ImageIcon(img)); } catch (IOException ex) { } setDate(); // datetext.setEditable(false); setCursor(new Cursor(Cursor.HAND_CURSOR)); addListeners(); datePanels.add(choosdate); datePanels.add(datetext); }
Those two variables will have a null value until they are assigned a value by the program. Using those variables while they have a null value will cause a NullPointerException.
Where does the program assign a value to those two variables? The code needs to use an assignment statement to assign a value to a variable:
theVariable = theValue; // assign theValue to theVariable
If you don't understand my answer, don't ignore it, ask a question.
Which variables do you mean? if you mean ye mo da it get value from hdate() when form loaded else you say Which variables do you mean?
See post#202 and post#196Which variables do you mean?
the choosdate and datetext variables do not have values.
If you don't understand my answer, don't ignore it, ask a question.
import newpackage.*; i remove this part and you should remove it That part is extra and unused
the choosdate is button Does the baton need the value?
and datetext get value from datetext.setText(testMethod());
and testMethod() set value for ye mo da ok?
and in testMethod() ye, mo, da fill from hdate(); when form or class load before all before need value those variable and filled ye, mo, da With year, month and day of Hijri date now Why do you think its value is null?String testMethod() { // ADDED DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); HijrahDate today = HijrahChronology.INSTANCE.date(ye, mo, da); String formatted = formatter.format(today); // 07/03/1439 return formatted.toString(); //<<<<<< ADDED }
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)); }
--- Update ---
if you like you can set any date for them but those variable always fill by today date when form or class loading fist time
Last edited by cnmeysam; May 1st, 2021 at 01:00 PM.
All GUI component variables need to be assigned a value before they are used.
Here are some examples from your code:JDialog dialog = new JDialog(owner, "Date Selected", false); datePanel = new DatePanel(); dateDialog = createDateDialog(owner, dateChooser);
You must be using different code from what you posted if the code you execute does not get a NullPointerException.
If you don't understand my answer, don't ignore it, ask a question.
ok i now use netbeans IDE and use this component , inside of GUI form and work's without any problems You can test it yourself
Because in the said GUI program, when calling or loading the component or class, set it values those variables when class or component loading before every think else
--- Update ---
in the DatePanel() use reflushWeekAndDay(); and in the reflushWeekAndDay() value of ye and mo get from spiners and da get value from today or clicked day
private void reflushWeekAndDay() { ye = (Integer) yearSpin.getValue(); mo = (Integer) monthSpin.getValue(); Locale ar = new Locale("ar"); DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar); HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(ye, mo, da); String WeekdayNum = hijrahDate.format(dayOfweekNum); int WeekdayNumb = Integer.parseInt(WeekdayNum); int moondaysnumber = hijrahDate.lengthOfMonth(); int maxDayNo = moondaysnumber; int dayNo = -2 - WeekdayNumb; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { String s = ""; if (dayNo >= 1 && dayNo <= maxDayNo) { s = String.valueOf(dayNo); } daysButton[i][j].setText(s); dayNo++; if (daysButton[i][j].getText().equals("")) { daysButton[i][j].setVisible(false); } else { daysButton[i][j].setVisible(true); } } } dayColorUpdate(false); }
--- Update ---
Now we have no problem with this part of the codes. Our problem now is that a button is added after the jtext.
--- Update ---
How do we do this?
--- Update ---
this is all my code and i can't add button after jtextfild
/* * Many thanks to Norm from the www.javaprogrammingforums.com that knew more than me.put more time and energy to create this component * Maysam Soleymani Qorabaee www.programsfuture.com * */ /** * * @author Admin */ import java.awt.BorderLayout; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Frame; import java.awt.GridLayout; import java.awt.Image; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowEvent; import java.awt.event.WindowFocusListener; import java.io.IOException; import java.text.Format; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.chrono.HijrahChronology; import java.time.chrono.HijrahDate; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.JTextField; import javax.swing.SpinnerNumberModel; import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import javax.swing.border.LineBorder; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import javax.swing.JPanel; public class Hijrah extends JPanel { int ye; int mo; int da; String[] moonNames = {"ٱلْمُحَرَّم", "صَفَر", "رَبِيع ٱلْأَوَّل", "رَبِيع ٱلثَّانِي", "جُمَادَىٰ ٱلْأُولَىٰ", "جُمَادَىٰ ٱلثَّانِيَة", "رَجَب", "شَعْبَان", "رَمَضَان", "شَوَّال", "ذُو ٱلْقَعْدَة", "ذُو ٱلْحِجَّة"}; //String[] moonNames = {"al-Muḥarram", "Ṣafar", "Rabīʿ al-ʾAwwal", "Rabīʿ ath-Thānī", "Jumadā al-ʾŪlā", "Jumādā ath-Thāniyah", "Rajab", "Shaʿbān", "Ramaḍān", "Shawwāl", "Ḏū al-Qaʿdah", "Ḏū al-Ḥijjah"}; String colname[] = {"سب", "اح", "ثن", "ثل", "ار", "خم", "جم"}; // String colname[] = {"Sa", "sun", "Mon", "Tu", "We", "Th", "Fr"}; JLabel cell; JLabel monthNameLabel = new JLabel(); // get name from array Image img; private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd"; private static final int DIALOG_WIDTH = 270; private static final int DIALOG_HEIGHT = 210; private SimpleDateFormat dateFormat; private Hijrah.DatePanel datePanel = null; private JDialog dateDialog = null; private JButton choosdate; private JTextField datetext; private JPanel datePanels; //Build a block to give a value to the year and month and day when the class loads public void hdate() { HijrahDate hijrahDate = HijrahDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); String formatted = formatter.format(hijrahDate); // 07/03/1439 ye = Integer.parseInt(formatted.substring(0, 4)); mo = Integer.parseInt(formatted.substring(5, 7)); da = Integer.parseInt(formatted.substring(8, 10)); } public Hijrah(String dateFormatPattern, Date date) { this(); DEFAULT_DATE_FORMAT = dateFormatPattern; } public Hijrah() { hdate(); choosdate.setText("choos date"); add(datePanels); datePanels.setPreferredSize(new Dimension(640, 480)); datePanels.setLayout(new FlowLayout()); try { img = ImageIO.read(getClass().getResource("cleander.png")); choosdate.setIcon(new ImageIcon(img)); } catch (IOException ex) { } setDate(); // datetext.setEditable(false); setCursor(new Cursor(Cursor.HAND_CURSOR)); addListeners(); datePanels.add(choosdate); datePanels.add(datetext); } private void addListeners() { addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent paramMouseEvent) { if (datePanel == null) { datePanel = new DatePanel(); } Point point = getLocationOnScreen(); point.y = point.y + 30; showDateDialog(datePanel, point); } }); } private void showDateDialog(DatePanel dateChooser, Point position) { Frame owner = (Frame) SwingUtilities .getWindowAncestor(Hijrah.this); if (dateDialog == null || dateDialog.getOwner() != owner) { dateDialog = createDateDialog(owner, dateChooser); } dateDialog.setLocation(getAppropriateLocation(owner, position)); dateDialog.setVisible(true); } private JDialog createDateDialog(Frame owner, JPanel contentPanel) { JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal // JDialog dialog = new JDialog(owner, "Date Selected", true); dialog.setUndecorated(true); dialog.getContentPane().add(contentPanel, BorderLayout.CENTER); dialog.pack(); dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT); dialog.addWindowFocusListener(new WindowFocusListener() { public void windowLostFocus(WindowEvent e) { dialog.setVisible(false); } public void windowGainedFocus(WindowEvent e) { } }); return dialog; } private Point getAppropriateLocation(Frame owner, Point position) { Point result = new Point(position); Point p = owner.getLocation(); int offsetX = (position.x + DIALOG_WIDTH) - (p.x + owner.getWidth()); int offsetY = (position.y + DIALOG_HEIGHT) - (p.y + owner.getHeight()); if (offsetX > 0) { result.x -= offsetX; } if (offsetY > 0) { result.y -= offsetY; } return result; } private SimpleDateFormat getDefaultDateFormat() { if (dateFormat == null) { dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT); } return dateFormat; } public void setText() { setDate(); } public void setDate() { datetext.setText(testMethod()); //<<<<< Changed } public Date getDate() { try { return getDefaultDateFormat().parse(datetext.getText()); } catch (ParseException e) { return new Date(); } } private class DatePanel extends JPanel implements ChangeListener { int startYear = 1; int lastYear = 99999; Color backGroundColor = Color.gray; Color palletTableColor = Color.white; Color todayBackColor = Color.orange; Color weekFontColor = Color.blue; Color dateFontColor = Color.black; Color weekendFontColor = Color.red; Color controlLineColor = Color.GREEN; Color controlTextColor = new java.awt.Color(153, 0, 153); Color controlMoonnameTextColor = Color.BLUE; JSpinner yearSpin; JSpinner monthSpin; JButton[][] daysButton = new JButton[8][7]; // Fill the days of the month in the buttons on the desired day DatePanel() { setLayout(new BorderLayout()); setBorder(new LineBorder(backGroundColor, 2)); setBackground(backGroundColor); JPanel topYearAndMonth = createYearAndMonthPanal(); add(topYearAndMonth, BorderLayout.NORTH); JPanel centerWeekAndDay = createWeekAndDayPanal(); add(centerWeekAndDay, BorderLayout.CENTER); reflushWeekAndDay(); } //Build a panel for daylight months and sppiners and labels private JPanel createYearAndMonthPanal() { Font font = new Font("Tahoma", Font.BOLD, 10); Calendar cal = getCalendar(); int currentYear = cal.get(Calendar.YEAR); int currentMonth = cal.get(Calendar.MONTH) + 1; JPanel panel = new JPanel(); panel.setLayout(new FlowLayout()); panel.setBackground(controlLineColor); JLabel yearLabel = new JLabel("السنة"); yearLabel.setForeground(controlTextColor); yearLabel.setHorizontalAlignment(SwingConstants.CENTER); yearLabel.setVerticalAlignment(SwingConstants.CENTER); yearLabel.setFont(font); panel.add(yearLabel); yearSpin = new JSpinner(new SpinnerNumberModel(currentYear, startYear, lastYear, 1)); yearSpin.setPreferredSize(new Dimension(60, 30)); yearSpin.setName("Year"); yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####")); yearSpin.addChangeListener(this); panel.add(yearSpin); monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1, 12, 1)); monthSpin.setPreferredSize(new Dimension(45, 30)); monthSpin.setName("Month"); monthSpin.addChangeListener(this); monthNameLabel.setPreferredSize(new Dimension(65, 20)); monthNameLabel.setForeground(controlTextColor); monthNameLabel.setHorizontalAlignment(SwingConstants.CENTER); monthNameLabel.setVerticalAlignment(SwingConstants.CENTER); monthNameLabel.setFont(font); monthNameLabel.setText(moonNames[(Integer) monthSpin.getValue() - 1]); // get name from array panel.add(monthNameLabel); panel.add(monthSpin); JLabel monthLabel = new JLabel("الشهر"); monthLabel.setForeground(controlTextColor); monthLabel.setHorizontalAlignment(SwingConstants.CENTER); monthLabel.setVerticalAlignment(SwingConstants.CENTER); monthLabel.setFont(font); panel.add(monthLabel); return panel; } //Fill the days of the month in the buttons on the desired day private JPanel createWeekAndDayPanal() { JPanel panel = new JPanel(); panel.setFont(new Font("Tahoma", Font.PLAIN, 10)); panel.setLayout(new GridLayout(7, 7)); panel.setBackground(Color.white); for (int i = 0; i < 7; i++) { cell = new JLabel(colname[i]); cell.setHorizontalAlignment(JLabel.CENTER); if (i == 6) { cell.setForeground(weekendFontColor); } else { cell.setForeground(weekFontColor); } panel.add(cell); } int actionCommandId = 0; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { JButton numBtn = new JButton(); numBtn.setBorder(null); numBtn.setHorizontalAlignment(SwingConstants.CENTER); numBtn.setActionCommand(String.valueOf(actionCommandId)); numBtn.setBackground(palletTableColor); numBtn.setForeground(dateFontColor); numBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JButton source = (JButton) event.getSource(); if (source.getText().length() == 0) { numBtn.setForeground(dateFontColor); return; } numBtn.setBackground(palletTableColor); numBtn.setForeground(dateFontColor); source.setForeground(todayBackColor); int newDay = Integer.parseInt(source.getText()); da = newDay; setDate(); dayColorUpdate(true); dateDialog.setVisible(false); } }); daysButton[i][j] = numBtn; panel.add(numBtn); actionCommandId++; if (j == 6) { numBtn.setForeground(weekendFontColor); } else { numBtn.setForeground(dateFontColor); } } } return panel; } private Calendar getCalendar() { Calendar calendar = Calendar.getInstance(); calendar.setTime(getDate()); return calendar; } private int getSelectedYear() { return ((Integer) yearSpin.getValue()).intValue(); } private int getSelectedMonth() { return ((Integer) monthSpin.getValue()).intValue(); } // Paint the days of the week and today private void dayColorUpdate(boolean isOldDay) { String s = String.valueOf(da); for (int a = 0; a < 6; a++) { for (int b = 0; b < 7; b++) { daysButton[a][b].setForeground(dateFontColor); daysButton[a][6].setForeground(weekendFontColor); if (daysButton[a][b].getText().equals(s)) { daysButton[a][b].setForeground(todayBackColor); } } } } // Specify the number of days of a cat and start on what day and end on what day private void reflushWeekAndDay() { ye = (Integer) yearSpin.getValue(); mo = (Integer) monthSpin.getValue(); Locale ar = new Locale("ar"); DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar); HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(ye, mo, da); String WeekdayNum = hijrahDate.format(dayOfweekNum); int WeekdayNumb = Integer.parseInt(WeekdayNum); int moondaysnumber = hijrahDate.lengthOfMonth(); int maxDayNo = moondaysnumber; int dayNo = -2 - WeekdayNumb; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { String s = ""; if (dayNo >= 1 && dayNo <= maxDayNo) { s = String.valueOf(dayNo); } daysButton[i][j].setText(s); dayNo++; if (daysButton[i][j].getText().equals("")) { daysButton[i][j].setVisible(false); } else { daysButton[i][j].setVisible(true); } } } dayColorUpdate(false); } // set year when click on jdialog button's and spinner and textfild public void stateChanged(ChangeEvent e) { dayColorUpdate(true); JSpinner source = (JSpinner) e.getSource(); Calendar cal = getCalendar(); if (source.getName().equals("Year")) { cal.set(Calendar.YEAR, getSelectedYear()); } else { cal.set(Calendar.MONTH, getSelectedMonth() - 1); } reflushWeekAndDay(); setDate(); mothnam(); } } // set month when click on jdialog button's and spinner and textfild public void mothnam() { monthNameLabel.setText(moonNames[mo - 1]); } //set date for jtext and jdialog.... String testMethod() { // ADDED DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); HijrahDate today = HijrahChronology.INSTANCE.date(ye, mo, da); String formatted = formatter.format(today); // 07/03/1439 return formatted.toString(); //<<<<<< ADDED } }
Last edited by cnmeysam; May 1st, 2021 at 01:13 PM.
I get this message:
Line 99:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Hijrah7.<init>(Hijrah7.java:99)
at Hijrah7$3.run(Hijrah7.java:481)
choosdate.setText("choos date");
that is the error I told you about. The choosdate variable has a null value. Where, in the code that you have, is choosdate assigned a value?
If you don't understand my answer, don't ignore it, ask a question.
i don't know how you get that eror choosdate is private JButton choosdate; and just called choosdate.setText("choos date"); for set label for it and added to datePanels datePanels.add(choosdate); And you have no other use for this baton. it is made to be displayed in front of the JText
--- Update ---
my code only 471 line i can't unrestand how your's is 481 line
Where is the variable choosdate assigned a value? There needs to be a statement something like this:choosdate = new JButton(); // assign a value to choosdate
I add in a main() method so I can execute it.how your's is 481 line
If you don't understand my answer, don't ignore it, ask a question.
i create a class name as public class Hijrah extends JPanel and cleaned Bild for create component
after that use component for create hijri date this is my component
https://s18.picofile.com/file/843239...onent.jar.html after click open a website in the site click on red button for download
after that i add it in palette in netbeans IDE after that i create a new frame and add hijri date to my frame and it is work's Properly and without any problems
--- Update ---
send me you'r code so i can tested
--- Update ---
you can changetoprivate JButton choosdate ;private JButton choosdate = new JButton();
Last edited by cnmeysam; May 1st, 2021 at 04:25 PM.
Ok, if you do not post code that I can compile and execute without errors, I can not help you.work's Properly and without any problems
I am using the code that you posted with the addition of a main method and the NewJFrame class that you posted earlier.send me you'r code so i can tested
If you don't understand my answer, don't ignore it, ask a question.
i use this class
and cleaned Bild for create component/* * Many thanks to Norm from the www.javaprogrammingforums.com that knew more than me.put more time and energy to create this component * Maysam Soleymani Qorabaee www.programsfuture.com * */ /** * * @author Admin */ import java.awt.BorderLayout; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Frame; import java.awt.GridLayout; import java.awt.Image; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowEvent; import java.awt.event.WindowFocusListener; import java.io.IOException; import java.text.Format; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.chrono.HijrahChronology; import java.time.chrono.HijrahDate; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.JTextField; import javax.swing.SpinnerNumberModel; import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import javax.swing.border.LineBorder; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import javax.swing.JPanel; public class Hijrah extends JPanel { int ye; int mo; int da; String[] moonNames = {"ٱلْمُحَرَّم", "صَفَر", "رَبِيع ٱلْأَوَّل", "رَبِيع ٱلثَّانِي", "جُمَادَىٰ ٱلْأُولَىٰ", "جُمَادَىٰ ٱلثَّانِيَة", "رَجَب", "شَعْبَان", "رَمَضَان", "شَوَّال", "ذُو ٱلْقَعْدَة", "ذُو ٱلْحِجَّة"}; //String[] moonNames = {"al-Muḥarram", "Ṣafar", "Rabīʿ al-ʾAwwal", "Rabīʿ ath-Thānī", "Jumadā al-ʾŪlā", "Jumādā ath-Thāniyah", "Rajab", "Shaʿbān", "Ramaḍān", "Shawwāl", "Ḏū al-Qaʿdah", "Ḏū al-Ḥijjah"}; String colname[] = {"سب", "اح", "ثن", "ثل", "ار", "خم", "جم"}; // String colname[] = {"Sa", "sun", "Mon", "Tu", "We", "Th", "Fr"}; JLabel cell; JLabel monthNameLabel = new JLabel(); // get name from array Image img; private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd"; private static final int DIALOG_WIDTH = 270; private static final int DIALOG_HEIGHT = 210; private SimpleDateFormat dateFormat; private Hijrah.DatePanel datePanel = null; private JDialog dateDialog = null; private JButton choosdate = new JButton(); private JTextField datetext; private JPanel datePanels; //Build a block to give a value to the year and month and day when the class loads public void hdate() { HijrahDate hijrahDate = HijrahDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); String formatted = formatter.format(hijrahDate); // 07/03/1439 ye = Integer.parseInt(formatted.substring(0, 4)); mo = Integer.parseInt(formatted.substring(5, 7)); da = Integer.parseInt(formatted.substring(8, 10)); } public Hijrah(String dateFormatPattern, Date date) { this(); DEFAULT_DATE_FORMAT = dateFormatPattern; } public Hijrah() { hdate(); choosdate.setText("choos date"); add(datePanels); datePanels.setPreferredSize(new Dimension(640, 480)); datePanels.setLayout(new FlowLayout()); try { img = ImageIO.read(getClass().getResource("cleander.png")); choosdate.setIcon(new ImageIcon(img)); } catch (IOException ex) { } setDate(); // datetext.setEditable(false); setCursor(new Cursor(Cursor.HAND_CURSOR)); addListeners(); datePanels.add(choosdate); choosdate.setVisible(true); datePanels.add(datetext ); } private void addListeners() { addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent paramMouseEvent) { if (datePanel == null) { datePanel = new DatePanel(); } Point point = getLocationOnScreen(); point.y = point.y + 30; showDateDialog(datePanel, point); } }); } private void showDateDialog(DatePanel dateChooser, Point position) { Frame owner = (Frame) SwingUtilities .getWindowAncestor(Hijrah.this); if (dateDialog == null || dateDialog.getOwner() != owner) { dateDialog = createDateDialog(owner, dateChooser); } dateDialog.setLocation(getAppropriateLocation(owner, position)); dateDialog.setVisible(true); } private JDialog createDateDialog(Frame owner, JPanel contentPanel) { JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal // JDialog dialog = new JDialog(owner, "Date Selected", true); dialog.setUndecorated(true); dialog.getContentPane().add(contentPanel, BorderLayout.CENTER); dialog.pack(); dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT); dialog.addWindowFocusListener(new WindowFocusListener() { public void windowLostFocus(WindowEvent e) { dialog.setVisible(false); } public void windowGainedFocus(WindowEvent e) { } }); return dialog; } private Point getAppropriateLocation(Frame owner, Point position) { Point result = new Point(position); Point p = owner.getLocation(); int offsetX = (position.x + DIALOG_WIDTH) - (p.x + owner.getWidth()); int offsetY = (position.y + DIALOG_HEIGHT) - (p.y + owner.getHeight()); if (offsetX > 0) { result.x -= offsetX; } if (offsetY > 0) { result.y -= offsetY; } return result; } private SimpleDateFormat getDefaultDateFormat() { if (dateFormat == null) { dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT); } return dateFormat; } public void setText() { setDate(); } public void setDate() { datetext.setText(testMethod()); //<<<<< Changed } public Date getDate() { try { return getDefaultDateFormat().parse(datetext.getText()); } catch (ParseException e) { return new Date(); } } private class DatePanel extends JPanel implements ChangeListener { int startYear = 1; int lastYear = 99999; Color backGroundColor = Color.gray; Color palletTableColor = Color.white; Color todayBackColor = Color.orange; Color weekFontColor = Color.blue; Color dateFontColor = Color.black; Color weekendFontColor = Color.red; Color controlLineColor = Color.GREEN; Color controlTextColor = new java.awt.Color(153, 0, 153); Color controlMoonnameTextColor = Color.BLUE; JSpinner yearSpin; JSpinner monthSpin; JButton[][] daysButton = new JButton[8][7]; // Fill the days of the month in the buttons on the desired day DatePanel() { setLayout(new BorderLayout()); setBorder(new LineBorder(backGroundColor, 2)); setBackground(backGroundColor); JPanel topYearAndMonth = createYearAndMonthPanal(); add(topYearAndMonth, BorderLayout.NORTH); JPanel centerWeekAndDay = createWeekAndDayPanal(); add(centerWeekAndDay, BorderLayout.CENTER); reflushWeekAndDay(); } //Build a panel for daylight months and sppiners and labels private JPanel createYearAndMonthPanal() { Font font = new Font("Tahoma", Font.BOLD, 10); Calendar cal = getCalendar(); int currentYear = cal.get(Calendar.YEAR); int currentMonth = cal.get(Calendar.MONTH) + 1; JPanel panel = new JPanel(); panel.setLayout(new FlowLayout()); panel.setBackground(controlLineColor); JLabel yearLabel = new JLabel("السنة"); yearLabel.setForeground(controlTextColor); yearLabel.setHorizontalAlignment(SwingConstants.CENTER); yearLabel.setVerticalAlignment(SwingConstants.CENTER); yearLabel.setFont(font); panel.add(yearLabel); yearSpin = new JSpinner(new SpinnerNumberModel(currentYear, startYear, lastYear, 1)); yearSpin.setPreferredSize(new Dimension(60, 30)); yearSpin.setName("Year"); yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####")); yearSpin.addChangeListener(this); panel.add(yearSpin); monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1, 12, 1)); monthSpin.setPreferredSize(new Dimension(45, 30)); monthSpin.setName("Month"); monthSpin.addChangeListener(this); monthNameLabel.setPreferredSize(new Dimension(65, 20)); monthNameLabel.setForeground(controlTextColor); monthNameLabel.setHorizontalAlignment(SwingConstants.CENTER); monthNameLabel.setVerticalAlignment(SwingConstants.CENTER); monthNameLabel.setFont(font); monthNameLabel.setText(moonNames[(Integer) monthSpin.getValue() - 1]); // get name from array panel.add(monthNameLabel); panel.add(monthSpin); JLabel monthLabel = new JLabel("الشهر"); monthLabel.setForeground(controlTextColor); monthLabel.setHorizontalAlignment(SwingConstants.CENTER); monthLabel.setVerticalAlignment(SwingConstants.CENTER); monthLabel.setFont(font); panel.add(monthLabel); return panel; } //Fill the days of the month in the buttons on the desired day private JPanel createWeekAndDayPanal() { JPanel panel = new JPanel(); panel.setFont(new Font("Tahoma", Font.PLAIN, 10)); panel.setLayout(new GridLayout(7, 7)); panel.setBackground(Color.white); for (int i = 0; i < 7; i++) { cell = new JLabel(colname[i]); cell.setHorizontalAlignment(JLabel.CENTER); if (i == 6) { cell.setForeground(weekendFontColor); } else { cell.setForeground(weekFontColor); } panel.add(cell); } int actionCommandId = 0; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { JButton numBtn = new JButton(); numBtn.setBorder(null); numBtn.setHorizontalAlignment(SwingConstants.CENTER); numBtn.setActionCommand(String.valueOf(actionCommandId)); numBtn.setBackground(palletTableColor); numBtn.setForeground(dateFontColor); numBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JButton source = (JButton) event.getSource(); if (source.getText().length() == 0) { numBtn.setForeground(dateFontColor); return; } numBtn.setBackground(palletTableColor); numBtn.setForeground(dateFontColor); source.setForeground(todayBackColor); int newDay = Integer.parseInt(source.getText()); da = newDay; setDate(); dayColorUpdate(true); dateDialog.setVisible(false); } }); daysButton[i][j] = numBtn; panel.add(numBtn); actionCommandId++; if (j == 6) { numBtn.setForeground(weekendFontColor); } else { numBtn.setForeground(dateFontColor); } } } return panel; } private Calendar getCalendar() { Calendar calendar = Calendar.getInstance(); calendar.setTime(getDate()); return calendar; } private int getSelectedYear() { return ((Integer) yearSpin.getValue()).intValue(); } private int getSelectedMonth() { return ((Integer) monthSpin.getValue()).intValue(); } // Paint the days of the week and today private void dayColorUpdate(boolean isOldDay) { String s = String.valueOf(da); for (int a = 0; a < 6; a++) { for (int b = 0; b < 7; b++) { daysButton[a][b].setForeground(dateFontColor); daysButton[a][6].setForeground(weekendFontColor); if (daysButton[a][b].getText().equals(s)) { daysButton[a][b].setForeground(todayBackColor); } } } } // Specify the number of days of a cat and start on what day and end on what day private void reflushWeekAndDay() { ye = (Integer) yearSpin.getValue(); mo = (Integer) monthSpin.getValue(); Locale ar = new Locale("ar"); DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar); HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(ye, mo, da); String WeekdayNum = hijrahDate.format(dayOfweekNum); int WeekdayNumb = Integer.parseInt(WeekdayNum); int moondaysnumber = hijrahDate.lengthOfMonth(); int maxDayNo = moondaysnumber; int dayNo = -2 - WeekdayNumb; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { String s = ""; if (dayNo >= 1 && dayNo <= maxDayNo) { s = String.valueOf(dayNo); } daysButton[i][j].setText(s); dayNo++; if (daysButton[i][j].getText().equals("")) { daysButton[i][j].setVisible(false); } else { daysButton[i][j].setVisible(true); } } } dayColorUpdate(false); } // set year when click on jdialog button's and spinner and textfild public void stateChanged(ChangeEvent e) { dayColorUpdate(true); JSpinner source = (JSpinner) e.getSource(); Calendar cal = getCalendar(); if (source.getName().equals("Year")) { cal.set(Calendar.YEAR, getSelectedYear()); } else { cal.set(Calendar.MONTH, getSelectedMonth() - 1); } reflushWeekAndDay(); setDate(); mothnam(); } } // set month when click on jdialog button's and spinner and textfild public void mothnam() { monthNameLabel.setText(moonNames[mo - 1]); } //set date for jtext and jdialog.... String testMethod() { // ADDED DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); HijrahDate today = HijrahChronology.INSTANCE.date(ye, mo, da); String formatted = formatter.format(today); // 07/03/1439 return formatted.toString(); //<<<<<< ADDED } }
after that use component for add date to my form
and this my all code in form
public class NewJFrame extends javax.swing.JFrame { public NewJFrame() { initComponents(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { hijrah1 = new newpackage.Hijrah(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(199, 199, 199) .addComponent(hijrah1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(139, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(20, 20, 20) .addComponent(hijrah1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(260, Short.MAX_VALUE)) ); setSize(new java.awt.Dimension(416, 339)); setLocationRelativeTo(null); }// </editor-fold> public static void main(String args[]) { //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); } }); } // Variables declaration - do not modify private newpackage.Hijrah hijrah1; // End of variables declaration }
--- Update ---
Here is a picture of my program output
https://s19.picofile.com/file/8432397718/Capture.JPG
java.lang.NullPointerException because datepanels is null on line 98
Where is datepanels assigned a value?
Where is datetext assigned a value?
If you don't understand my answer, don't ignore it, ask a question.
if you think soft problem change to this
int ye; int mo; int ;int ye=1442; int mo=9; int da=21;
--- Update ---
i think all problem started when i change
public class Hijrah extends JTextField {public class Hijrah extends JPanel {
this is before extend to jpanel
test this and see problem is soft or nost/* * Many thanks to Norm from the www.javaprogrammingforums.com that knew more than me.put more time and energy to create this component * Maysam Soleymani Qorabaee www.programsfuture.com * and open the template in the editor. */ import java.awt.BorderLayout; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Frame; import java.awt.GridLayout; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowEvent; import java.awt.event.WindowFocusListener; import java.text.Format; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.chrono.HijrahChronology; import java.time.chrono.HijrahDate; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; import java.util.Locale; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.JTextField; import javax.swing.SpinnerNumberModel; import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import javax.swing.border.LineBorder; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class Hijrah extends JTextField { int ye; int mo; int da; String days; String[] moonNames = {"ٱلْمُحَرَّم", "صَفَر", "رَبِيع ٱلْأَوَّل", "رَبِيع ٱلثَّانِي", "جُمَادَىٰ ٱلْأُولَىٰ", "جُمَادَىٰ ٱلثَّانِيَة", "رَجَب", "شَعْبَان", "رَمَضَان", "شَوَّال", "ذُو ٱلْقَعْدَة", "ذُو ٱلْحِجَّة"}; //String[] moonNames = {"al-Muḥarram", "Ṣafar", "Rabīʿ al-ʾAwwal", "Rabīʿ ath-Thānī", "Jumadā al-ʾŪlā", "Jumādā ath-Thāniyah", "Rajab", "Shaʿbān", "Ramaḍān", "Shawwāl", "Ḏū al-Qaʿdah", "Ḏū al-Ḥijjah"}; String colname[] = {"سب", "اح", "ثن", "ثل", "ار", "خم", "جم"}; // String colname[] = {"Sa", "sun", "Mon", "Tu", "We", "Th", "Fr"}; JLabel cell; JLabel monthNameLabel = new JLabel(); // get name from array private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd"; private static final int DIALOG_WIDTH = 270; private static final int DIALOG_HEIGHT = 210; private SimpleDateFormat dateFormat; private DatePanel datePanel = null; private JDialog dateDialog = null; public void hdate() { HijrahDate hijrahDate = HijrahDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); String formatted = formatter.format(hijrahDate); // 07/03/1439 ye = Integer.parseInt(formatted.substring(0, 4)); mo = Integer.parseInt(formatted.substring(5, 7)); da = Integer.parseInt(formatted.substring(8, 10)); } // public Hijrah() { // this(); // } public Hijrah(String dateFormatPattern, Date date) { this(); DEFAULT_DATE_FORMAT = dateFormatPattern; } public Hijrah() { hdate(); setDate(); // setEditable(false); setCursor(new Cursor(Cursor.HAND_CURSOR)); addListeners(); } private void addListeners() { addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent paramMouseEvent) { days = String.valueOf(da); if (datePanel == null) { datePanel = new DatePanel(); } Point point = getLocationOnScreen(); point.y = point.y + 30; showDateDialog(datePanel, point); } }); } private void showDateDialog(DatePanel dateChooser, Point position) { Frame owner = (Frame) SwingUtilities .getWindowAncestor(Hijrah.this); if (dateDialog == null || dateDialog.getOwner() != owner) { dateDialog = createDateDialog(owner, dateChooser); } dateDialog.setLocation(getAppropriateLocation(owner, position)); dateDialog.setVisible(true); } private JDialog createDateDialog(Frame owner, JPanel contentPanel) { JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal // JDialog dialog = new JDialog(owner, "Date Selected", true); dialog.setUndecorated(true); dialog.getContentPane().add(contentPanel, BorderLayout.CENTER); dialog.pack(); dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT); dialog.addWindowFocusListener(new WindowFocusListener() { public void windowLostFocus(WindowEvent e) { dialog.setVisible(false); } public void windowGainedFocus(WindowEvent e) { } }); return dialog; } private Point getAppropriateLocation(Frame owner, Point position) { Point result = new Point(position); Point p = owner.getLocation(); int offsetX = (position.x + DIALOG_WIDTH) - (p.x + owner.getWidth()); int offsetY = (position.y + DIALOG_HEIGHT) - (p.y + owner.getHeight()); if (offsetX > 0) { result.x -= offsetX; } if (offsetY > 0) { result.y -= offsetY; } return result; } private SimpleDateFormat getDefaultDateFormat() { if (dateFormat == null) { dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT); } return dateFormat; } public void setText() { setDate(); } public void setDate() { super.setText(testMethod()); //<<<<< Changed } public Date getDate() { try { return getDefaultDateFormat().parse(getText()); } catch (ParseException e) { return new Date(); } } void setDate(int i) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } private class DatePanel extends JPanel implements ChangeListener { int startYear = 1; int lastYear = 99999; Color backGroundColor = Color.gray; Color palletTableColor = Color.white; Color todayBackColor = Color.orange; Color weekFontColor = Color.blue; Color dateFontColor = Color.black; Color weekendFontColor = Color.red; Color controlLineColor = Color.GREEN; Color controlTextColor = new java.awt.Color(153, 0, 153); Color controlMoonnameTextColor = Color.BLUE; JSpinner yearSpin; JSpinner monthSpin; JButton[][] daysButton = new JButton[8][7]; DatePanel() { setLayout(new BorderLayout()); setBorder(new LineBorder(backGroundColor, 2)); setBackground(backGroundColor); JPanel topYearAndMonth = createYearAndMonthPanal(); add(topYearAndMonth, BorderLayout.NORTH); JPanel centerWeekAndDay = createWeekAndDayPanal(); add(centerWeekAndDay, BorderLayout.CENTER); reflushWeekAndDay(); } private JPanel createYearAndMonthPanal() { Font font = new Font("Tahoma", Font.BOLD, 10); Calendar cal = getCalendar(); int currentYear = cal.get(Calendar.YEAR); int currentMonth = cal.get(Calendar.MONTH) + 1; JPanel panel = new JPanel(); panel.setLayout(new FlowLayout()); panel.setBackground(controlLineColor); JLabel yearLabel = new JLabel("السنّة"); yearLabel.setForeground(controlTextColor); yearLabel.setHorizontalAlignment(SwingConstants.CENTER); yearLabel.setVerticalAlignment(SwingConstants.CENTER); yearLabel.setFont(font); panel.add(yearLabel); yearSpin = new JSpinner(new SpinnerNumberModel(currentYear, startYear, lastYear, 1)); yearSpin.setPreferredSize(new Dimension(60, 30)); yearSpin.setName("Year"); yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####")); yearSpin.addChangeListener(this); panel.add(yearSpin); monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1, 12, 1)); monthSpin.setPreferredSize(new Dimension(45, 30)); monthSpin.setName("Month"); monthSpin.addChangeListener(this); monthNameLabel.setPreferredSize(new Dimension(65, 20)); monthNameLabel.setForeground(controlTextColor); monthNameLabel.setHorizontalAlignment(SwingConstants.CENTER); monthNameLabel.setVerticalAlignment(SwingConstants.CENTER); monthNameLabel.setFont(font); monthNameLabel.setText(moonNames[(Integer) monthSpin.getValue() - 1]); // get name from array panel.add(monthNameLabel); panel.add(monthSpin); JLabel monthLabel = new JLabel("الشَّهْر"); monthLabel.setForeground(controlTextColor); monthLabel.setHorizontalAlignment(SwingConstants.CENTER); monthLabel.setVerticalAlignment(SwingConstants.CENTER); monthLabel.setFont(font); panel.add(monthLabel); return panel; } private JPanel createWeekAndDayPanal() { JPanel panel = new JPanel(); panel.setFont(new Font("Tahoma", Font.PLAIN, 10)); panel.setLayout(new GridLayout(7, 7)); panel.setBackground(Color.white); for (int i = 0; i < 7; i++) { cell = new JLabel(colname[i]); cell.setHorizontalAlignment(JLabel.CENTER); if (i == 6) { cell.setForeground(weekendFontColor); } else { cell.setForeground(weekFontColor); } panel.add(cell); } int actionCommandId = 0; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { JButton numBtn = new JButton(); numBtn.setBorder(null); numBtn.setHorizontalAlignment(SwingConstants.CENTER); numBtn.setActionCommand(String.valueOf(actionCommandId)); numBtn.setBackground(palletTableColor); numBtn.setForeground(dateFontColor); numBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JButton source = (JButton) event.getSource(); if (source.getText().length() == 0) { numBtn.setForeground(dateFontColor); return; } numBtn.setBackground(palletTableColor); numBtn.setForeground(dateFontColor); source.setForeground(todayBackColor); int newDay = Integer.parseInt(source.getText()); da = newDay; // reflushWeekAndDay(); setDate(); dayColorUpdate(true); dateDialog.setVisible(false); } }); daysButton[i][j] = numBtn; panel.add(numBtn); actionCommandId++; if (j == 6) { numBtn.setForeground(weekendFontColor); } else { numBtn.setForeground(dateFontColor); } } } return panel; } private Calendar getCalendar() { Calendar calendar = Calendar.getInstance(); calendar.setTime(getDate()); return calendar; } private int getSelectedYear() { return ((Integer) yearSpin.getValue()).intValue(); } private int getSelectedMonth() { return ((Integer) monthSpin.getValue()).intValue(); } private void dayColorUpdate(boolean isOldDay) { days = String.valueOf(da); for (int a = 0; a < 6; a++) { for (int b = 0; b < 7; b++) { daysButton[a][b].setForeground(dateFontColor); daysButton[a][6].setForeground(weekendFontColor); if (daysButton[a][b].getText().equals(days)) { daysButton[a][b].setForeground(todayBackColor); } } } } private void reflushWeekAndDay() { ye = (Integer) yearSpin.getValue(); mo = (Integer) monthSpin.getValue(); Locale ar = new Locale("ar"); DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar); HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(ye, mo, da); String WeekdayNum = hijrahDate.format(dayOfweekNum); int WeekdayNumb = Integer.parseInt(WeekdayNum); int moondaysnumber = hijrahDate.lengthOfMonth(); int maxDayNo = moondaysnumber; int dayNo = -2 - WeekdayNumb; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { String s = ""; if (dayNo >= 1 && dayNo <= maxDayNo) { s = String.valueOf(dayNo); } daysButton[i][j].setText(s); dayNo++; if (daysButton[i][j].getText().equals("")) { daysButton[i][j].setVisible(false); } else { daysButton[i][j].setVisible(true); } } } dayColorUpdate(false); } public void stateChanged(ChangeEvent e) { dayColorUpdate(true); JSpinner source = (JSpinner) e.getSource(); Calendar cal = getCalendar(); if (source.getName().equals("Year")) { cal.set(Calendar.YEAR, getSelectedYear()); } else { cal.set(Calendar.MONTH, getSelectedMonth() - 1); } reflushWeekAndDay(); setDate(); mothnam(); } } public void mothnam() { monthNameLabel.setText(moonNames[mo - 1]); } String testMethod() { // ADDED DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); HijrahDate today = HijrahChronology.INSTANCE.date(ye, mo, da); String formatted = formatter.format(today); // 07/03/1439 return formatted.toString(); //<<<<<< ADDED } }
What happened to the need for a button?test this and see problem is soft or nost
The class needs to extend JPanel if it is going to display a JTextField and a JButton.
I did not have any problems with the values of ye, mo or da.
If you don't understand my answer, don't ignore it, ask a question.
ok now how we can change extend to jpanel That we do not face a problem
The problem was that there were several variables that were not given values. Add code to assign them values.
If you don't understand my answer, don't ignore it, ask a question.
cnmeysam (May 1st, 2021)
pleas edit extend text to panel and add button for me to i can see what happened and learn it
--- Update ---
Unfortunately, I learn the code better and faster to explain it, so I use project-based tutorials.
Sorry, the posted code is too large to work with for learning.
I would suggest that you create a small, simple program for learning how:
The test program would have a JFrame and main method.
Then create a JPanel that contains some components like a textfield and button.
Add that panel to the frame.
Then add a listener to the button that would add some text to the textfield.
When the program executes, it would show the textfield and the button
When the button is pressed, some text would be added to the textfield.
If you don't understand my answer, don't ignore it, ask a question.
i think now all it's fixed
/* * Many thanks to Norm from the www.javaprogrammingforums.com that knew more than me.put more time and energy to create this component * Maysam Soleymani Qorabaee www.programsfuture.com * */ /** * * @author Admin */ import java.awt.BorderLayout; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Frame; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Image; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowEvent; import java.awt.event.WindowFocusListener; import java.io.IOException; import java.text.Format; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.chrono.HijrahChronology; import java.time.chrono.HijrahDate; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.JTextField; import javax.swing.SpinnerNumberModel; import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import javax.swing.border.LineBorder; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import javax.swing.JPanel; public class Hijrah extends JPanel { int ye; int mo; int da; String[] moonNames = {"ٱلْمُحَرَّم", "صَفَر", "رَبِيع ٱلْأَوَّل", "رَبِيع ٱلثَّانِي", "جُمَادَىٰ ٱلْأُولَىٰ", "جُمَادَىٰ ٱلثَّانِيَة", "رَجَب", "شَعْبَان", "رَمَضَان", "شَوَّال", "ذُو ٱلْقَعْدَة", "ذُو ٱلْحِجَّة"}; //String[] moonNames = {"al-Muḥarram", "Ṣafar", "Rabīʿ al-ʾAwwal", "Rabīʿ ath-Thānī", "Jumadā al-ʾŪlā", "Jumādā ath-Thāniyah", "Rajab", "Shaʿbān", "Ramaḍān", "Shawwāl", "Ḏū al-Qaʿdah", "Ḏū al-Ḥijjah"}; String colname[] = {"سب", "اح", "ثن", "ثل", "ار", "خم", "جم"}; // String colname[] = {"Sa", "sun", "Mon", "Tu", "We", "Th", "Fr"}; JLabel cell; JLabel monthNameLabel = new JLabel(); // get name from array Image img; private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd"; private static final int DIALOG_WIDTH = 270; private static final int DIALOG_HEIGHT = 210; private SimpleDateFormat dateFormat; private Hijrah.DatePanel datePanel = null; private JDialog dateDialog = null; private JButton choosdate = new JButton(); private JTextField datetext = new JTextField(); ; private JPanel datePanels = new JPanel(); String formatted; //Build a block to give a value to the year and month and day when the class loads public void hdate() { HijrahDate hijrahDate = HijrahDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); formatted = formatter.format(hijrahDate); // 07/03/1439 ye = Integer.parseInt(formatted.substring(0, 4)); mo = Integer.parseInt(formatted.substring(5, 7)); da = Integer.parseInt(formatted.substring(8, 10)); } public Hijrah(String dateFormatPattern, Date date) { this(); DEFAULT_DATE_FORMAT = dateFormatPattern; } public Hijrah() { hdate(); // choosdate.setText("choos date"); add(datePanels); datePanels.setPreferredSize(new Dimension(130, 30)); datetext.setPreferredSize(new Dimension(90, 25)); choosdate.setPreferredSize(new Dimension(30, 28)); datePanels.setLayout(new GridBagLayout()); try { img = ImageIO.read(getClass().getResource("cleander.png")); choosdate.setIcon(new ImageIcon(img)); } catch (IOException ex) { } // setDate(); //// datetext.setEditable(false); choosdate.setCursor(new Cursor(Cursor.HAND_CURSOR)); // addListeners(); datePanels.add(datetext); datePanels.add(choosdate); choosdate.setVisible(true); choosdate.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (datePanel == null) { datePanel = new DatePanel(); } Point point = getLocationOnScreen(); point.y = point.y + 30; showDateDialog(datePanel, point); } }); } // private void addListeners() { // addMouseListener(new MouseAdapter() { // public void mouseClicked(MouseEvent paramMouseEvent) { // // } // }); // } private void showDateDialog(DatePanel dateChooser, Point position) { Frame owner = (Frame) SwingUtilities .getWindowAncestor(Hijrah.this); if (dateDialog == null || dateDialog.getOwner() != owner) { dateDialog = createDateDialog(owner, dateChooser); } dateDialog.setLocation(getAppropriateLocation(owner, position)); dateDialog.setVisible(true); } private JDialog createDateDialog(Frame owner, JPanel contentPanel) { JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal // JDialog dialog = new JDialog(owner, "Date Selected", true); dialog.setUndecorated(true); dialog.getContentPane().add(contentPanel, BorderLayout.CENTER); dialog.pack(); dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT); dialog.addWindowFocusListener(new WindowFocusListener() { public void windowLostFocus(WindowEvent e) { dialog.setVisible(false); } public void windowGainedFocus(WindowEvent e) { } }); return dialog; } private Point getAppropriateLocation(Frame owner, Point position) { Point result = new Point(position); Point p = owner.getLocation(); int offsetX = (position.x + DIALOG_WIDTH) - (p.x + owner.getWidth()); int offsetY = (position.y + DIALOG_HEIGHT) - (p.y + owner.getHeight()); if (offsetX > 0) { result.x -= offsetX; } if (offsetY > 0) { result.y -= offsetY; } return result; } private SimpleDateFormat getDefaultDateFormat() { if (dateFormat == null) { dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT); } return dateFormat; } public void setText() { setDate(); } public void setDate() { datetext.setText(testMethod()); //<<<<< Changed } public Date getDate() { try { return getDefaultDateFormat().parse(formatted); } catch (ParseException e) { return new Date(); } } private class DatePanel extends JPanel implements ChangeListener { int startYear = 1; int lastYear = 99999; Color backGroundColor = Color.gray; Color palletTableColor = Color.white; Color todayBackColor = Color.orange; Color weekFontColor = Color.blue; Color dateFontColor = Color.black; Color weekendFontColor = Color.red; Color controlLineColor = Color.GREEN; Color controlTextColor = new java.awt.Color(153, 0, 153); Color controlMoonnameTextColor = Color.BLUE; JSpinner yearSpin; JSpinner monthSpin; JButton[][] daysButton = new JButton[8][7]; // Fill the days of the month in the buttons on the desired day DatePanel() { setLayout(new BorderLayout()); setBorder(new LineBorder(backGroundColor, 2)); setBackground(backGroundColor); JPanel topYearAndMonth = createYearAndMonthPanal(); add(topYearAndMonth, BorderLayout.NORTH); JPanel centerWeekAndDay = createWeekAndDayPanal(); add(centerWeekAndDay, BorderLayout.CENTER); reflushWeekAndDay(); } //Build a panel for daylight months and sppiners and labels private JPanel createYearAndMonthPanal() { Font font = new Font("Tahoma", Font.BOLD, 10); Calendar cal = getCalendar(); int currentYear = cal.get(Calendar.YEAR); int currentMonth = cal.get(Calendar.MONTH) + 1; JPanel panel = new JPanel(); panel.setLayout(new FlowLayout()); panel.setBackground(controlLineColor); JLabel yearLabel = new JLabel("السنة"); yearLabel.setForeground(controlTextColor); yearLabel.setHorizontalAlignment(SwingConstants.CENTER); yearLabel.setVerticalAlignment(SwingConstants.CENTER); yearLabel.setFont(font); panel.add(yearLabel); yearSpin = new JSpinner(new SpinnerNumberModel(currentYear, startYear, lastYear, 1)); yearSpin.setPreferredSize(new Dimension(60, 30)); yearSpin.setName("Year"); yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####")); yearSpin.addChangeListener(this); panel.add(yearSpin); monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1, 12, 1)); monthSpin.setPreferredSize(new Dimension(45, 30)); monthSpin.setName("Month"); monthSpin.addChangeListener(this); monthNameLabel.setPreferredSize(new Dimension(65, 20)); monthNameLabel.setForeground(controlTextColor); monthNameLabel.setHorizontalAlignment(SwingConstants.CENTER); monthNameLabel.setVerticalAlignment(SwingConstants.CENTER); monthNameLabel.setFont(font); monthNameLabel.setText(moonNames[(Integer) monthSpin.getValue() - 1]); // get name from array panel.add(monthNameLabel); panel.add(monthSpin); JLabel monthLabel = new JLabel("الشهر"); monthLabel.setForeground(controlTextColor); monthLabel.setHorizontalAlignment(SwingConstants.CENTER); monthLabel.setVerticalAlignment(SwingConstants.CENTER); monthLabel.setFont(font); panel.add(monthLabel); return panel; } //Fill the days of the month in the buttons on the desired day private JPanel createWeekAndDayPanal() { JPanel panel = new JPanel(); panel.setFont(new Font("Tahoma", Font.PLAIN, 10)); panel.setLayout(new GridLayout(7, 7)); panel.setBackground(Color.white); for (int i = 0; i < 7; i++) { cell = new JLabel(colname[i]); cell.setHorizontalAlignment(JLabel.CENTER); if (i == 6) { cell.setForeground(weekendFontColor); } else { cell.setForeground(weekFontColor); } panel.add(cell); } int actionCommandId = 0; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { JButton numBtn = new JButton(); numBtn.setBorder(null); numBtn.setHorizontalAlignment(SwingConstants.CENTER); numBtn.setActionCommand(String.valueOf(actionCommandId)); numBtn.setBackground(palletTableColor); numBtn.setForeground(dateFontColor); numBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JButton source = (JButton) event.getSource(); if (source.getText().length() == 0) { numBtn.setForeground(dateFontColor); return; } numBtn.setBackground(palletTableColor); numBtn.setForeground(dateFontColor); source.setForeground(todayBackColor); int newDay = Integer.parseInt(source.getText()); da = newDay; setDate(); dayColorUpdate(true); dateDialog.setVisible(false); } }); daysButton[i][j] = numBtn; panel.add(numBtn); actionCommandId++; if (j == 6) { numBtn.setForeground(weekendFontColor); } else { numBtn.setForeground(dateFontColor); } } } return panel; } private Calendar getCalendar() { Calendar calendar = Calendar.getInstance(); calendar.setTime(getDate()); return calendar; } private int getSelectedYear() { return ((Integer) yearSpin.getValue()).intValue(); } private int getSelectedMonth() { return ((Integer) monthSpin.getValue()).intValue(); } // Paint the days of the week and today private void dayColorUpdate(boolean isOldDay) { String s = String.valueOf(da); for (int a = 0; a < 6; a++) { for (int b = 0; b < 7; b++) { daysButton[a][b].setForeground(dateFontColor); daysButton[a][6].setForeground(weekendFontColor); if (daysButton[a][b].getText().equals(s)) { daysButton[a][b].setForeground(todayBackColor); } } } } // Specify the number of days of a cat and start on what day and end on what day private void reflushWeekAndDay() { ye = (Integer) yearSpin.getValue(); mo = (Integer) monthSpin.getValue(); Locale ar = new Locale("ar"); DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar); HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(ye, mo, da); String WeekdayNum = hijrahDate.format(dayOfweekNum); int WeekdayNumb = Integer.parseInt(WeekdayNum); int moondaysnumber = hijrahDate.lengthOfMonth(); int maxDayNo = moondaysnumber; int dayNo = -0 - WeekdayNumb; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { String s = ""; if (dayNo >= 1 && dayNo <= maxDayNo) { s = String.valueOf(dayNo); } daysButton[i][j].setText(s); dayNo++; if (daysButton[i][j].getText().equals("")) { daysButton[i][j].setVisible(false); } else { daysButton[i][j].setVisible(true); } } } dayColorUpdate(false); } // set year when click on jdialog button's and spinner and textfild public void stateChanged(ChangeEvent e) { dayColorUpdate(true); JSpinner source = (JSpinner) e.getSource(); Calendar cal = getCalendar(); if (source.getName().equals("Year")) { cal.set(Calendar.YEAR, getSelectedYear()); } else { cal.set(Calendar.MONTH, getSelectedMonth() - 1); } reflushWeekAndDay(); setDate(); mothnam(); } } // set month when click on jdialog button's and spinner and textfild public void mothnam() { monthNameLabel.setText(moonNames[mo - 1]); } //set date for jtext and jdialog.... String testMethod() { // ADDED DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); HijrahDate today = HijrahChronology.INSTANCE.date(ye, mo, da); String formatted = formatter.format(today); // 07/03/1439 return formatted.toString(); //<<<<<< ADDED } }
I am happy you got it to work.
If you don't understand my answer, don't ignore it, ask a question.
last problem how i must set jtext and jbutton in panel if resized panel jtext size change?
--- Update ---
i use this
but i don't know how fix datePanels to extends JPaneljavax.swing.GroupLayout datePanelsLayout = new javax.swing.GroupLayout(datePanels); datePanels.setLayout(datePanelsLayout); datePanelsLayout.setHorizontalGroup( datePanelsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(datePanelsLayout.createSequentialGroup() .addComponent(datetext, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, 0) .addComponent(choosdate, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)) ); datePanelsLayout.setVerticalGroup( datePanelsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(datePanelsLayout.createSequentialGroup() .addGroup(datePanelsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(datetext, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(choosdate, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(0, 0, Short.MAX_VALUE)) );
--- Update ---
this is my all code now
/* * Many thanks to Norm from the www.javaprogrammingforums.com that knew more than me.put more time and energy to create this component * Maysam Soleymani Qorabaee www.programsfuture.com * */ /** * * @author Admin */ import java.awt.BorderLayout; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Frame; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Image; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowEvent; import java.awt.event.WindowFocusListener; import java.io.IOException; import java.text.Format; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.chrono.HijrahChronology; import java.time.chrono.HijrahDate; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFormattedTextField; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.JTextField; import javax.swing.SpinnerNumberModel; import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import javax.swing.border.LineBorder; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import javax.swing.JPanel; import javax.swing.SpringLayout; public class Hijrah extends JPanel { int ye; int mo; int da; String[] moonNames = {"ٱلْمُحَرَّم", "صَفَر", "رَبِيع ٱلْأَوَّل", "رَبِيع ٱلثَّانِي", "جُمَادَىٰ ٱلْأُولَىٰ", "جُمَادَىٰ ٱلثَّانِيَة", "رَجَب", "شَعْبَان", "رَمَضَان", "شَوَّال", "ذُو ٱلْقَعْدَة", "ذُو ٱلْحِجَّة"}; //String[] moonNames = {"al-Muḥarram", "Ṣafar", "Rabīʿ al-ʾAwwal", "Rabīʿ ath-Thānī", "Jumadā al-ʾŪlā", "Jumādā ath-Thāniyah", "Rajab", "Shaʿbān", "Ramaḍān", "Shawwāl", "Ḏū al-Qaʿdah", "Ḏū al-Ḥijjah"}; String colname[] = {"سب", "اح", "ثن", "ثل", "ار", "خم", "جم"}; // String colname[] = {"Sa", "sun", "Mon", "Tu", "We", "Th", "Fr"}; JLabel cell; JLabel monthNameLabel = new JLabel(); // get name from array Image img; private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd"; private static final int DIALOG_WIDTH = 270; private static final int DIALOG_HEIGHT = 210; private SimpleDateFormat dateFormat; private Hijrah.DatePanel datePanel = null; private JDialog dateDialog = null; private JButton choosdate = new JButton(); private JFormattedTextField datetext = new JFormattedTextField(); private JPanel datePanels = new JPanel(); String formatted; //Build a block to give a value to the year and month and day when the class loads public void hdate() { HijrahDate hijrahDate = HijrahDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); formatted = formatter.format(hijrahDate); // 07/03/1439 ye = Integer.parseInt(formatted.substring(0, 4)); mo = Integer.parseInt(formatted.substring(5, 7)); da = Integer.parseInt(formatted.substring(8, 10)); } public Hijrah(String dateFormatPattern, Date date) { this(); DEFAULT_DATE_FORMAT = dateFormatPattern; } public Hijrah() { hdate(); try { datetext.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("####/##/##"))); } catch (java.text.ParseException ex) { ex.printStackTrace(); } javax.swing.GroupLayout datePanelsLayout = new javax.swing.GroupLayout(datePanels); datePanels.setLayout(datePanelsLayout); datePanelsLayout.setHorizontalGroup( datePanelsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(datePanelsLayout.createSequentialGroup() .addComponent(datetext, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, 0) .addComponent(choosdate, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)) ); datePanelsLayout.setVerticalGroup( datePanelsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(datePanelsLayout.createSequentialGroup() .addGroup(datePanelsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(datetext, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(choosdate, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(0, 0, Short.MAX_VALUE)) ); add(datePanels); try { img = ImageIO.read(getClass().getResource("cleander.png")); choosdate.setIcon(new ImageIcon(img)); } catch (IOException ex) { } setDate(); //// datetext.setEditable(false); choosdate.setCursor(new Cursor(Cursor.HAND_CURSOR)); // addListeners(); datePanels.add(datetext); datePanels.add(choosdate); choosdate.setVisible(true); choosdate.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (datePanel == null) { datePanel = new DatePanel(); } Point point = getLocationOnScreen(); point.y = point.y + 30; showDateDialog(datePanel, point); } }); } // private void addListeners() { // addMouseListener(new MouseAdapter() { // public void mouseClicked(MouseEvent paramMouseEvent) { // // } // }); // } private void showDateDialog(DatePanel dateChooser, Point position) { Frame owner = (Frame) SwingUtilities .getWindowAncestor(Hijrah.this); if (dateDialog == null || dateDialog.getOwner() != owner) { dateDialog = createDateDialog(owner, dateChooser); } dateDialog.setLocation(getAppropriateLocation(owner, position)); dateDialog.setVisible(true); } private JDialog createDateDialog(Frame owner, JPanel contentPanel) { JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal // JDialog dialog = new JDialog(owner, "Date Selected", true); dialog.setUndecorated(true); dialog.getContentPane().add(contentPanel, BorderLayout.CENTER); dialog.pack(); dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT); dialog.addWindowFocusListener(new WindowFocusListener() { public void windowLostFocus(WindowEvent e) { dialog.setVisible(false); } public void windowGainedFocus(WindowEvent e) { } }); return dialog; } private Point getAppropriateLocation(Frame owner, Point position) { Point result = new Point(position); Point p = owner.getLocation(); int offsetX = (position.x + DIALOG_WIDTH) - (p.x + owner.getWidth()); int offsetY = (position.y + DIALOG_HEIGHT) - (p.y + owner.getHeight()); if (offsetX > 0) { result.x -= offsetX; } if (offsetY > 0) { result.y -= offsetY; } return result; } private SimpleDateFormat getDefaultDateFormat() { if (dateFormat == null) { dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT); } return dateFormat; } public void setText() { setDate(); } public void setDate() { datetext.setText(testMethod()); //<<<<< Changed } public Date getDate() { try { return getDefaultDateFormat().parse(formatted); } catch (ParseException e) { return new Date(); } } private class DatePanel extends JPanel implements ChangeListener { int startYear = 1; int lastYear = 99999; Color backGroundColor = Color.gray; Color palletTableColor = Color.white; Color todayBackColor = Color.orange; Color weekFontColor = Color.blue; Color dateFontColor = Color.black; Color weekendFontColor = Color.red; Color controlLineColor = Color.GREEN; Color controlTextColor = new java.awt.Color(153, 0, 153); Color controlMoonnameTextColor = Color.BLUE; JSpinner yearSpin; JSpinner monthSpin; JButton[][] daysButton = new JButton[8][7]; // Fill the days of the month in the buttons on the desired day DatePanel() { setLayout(new BorderLayout()); setBorder(new LineBorder(backGroundColor, 2)); setBackground(backGroundColor); JPanel topYearAndMonth = createYearAndMonthPanal(); add(topYearAndMonth, BorderLayout.NORTH); JPanel centerWeekAndDay = createWeekAndDayPanal(); add(centerWeekAndDay, BorderLayout.CENTER); reflushWeekAndDay(); } //Build a panel for daylight months and sppiners and labels private JPanel createYearAndMonthPanal() { Font font = new Font("Tahoma", Font.BOLD, 10); Calendar cal = getCalendar(); int currentYear = cal.get(Calendar.YEAR); int currentMonth = cal.get(Calendar.MONTH) + 1; JPanel panel = new JPanel(); panel.setLayout(new FlowLayout()); panel.setBackground(controlLineColor); JLabel yearLabel = new JLabel("السنة"); yearLabel.setForeground(controlTextColor); yearLabel.setHorizontalAlignment(SwingConstants.CENTER); yearLabel.setVerticalAlignment(SwingConstants.CENTER); yearLabel.setFont(font); panel.add(yearLabel); yearSpin = new JSpinner(new SpinnerNumberModel(currentYear, startYear, lastYear, 1)); yearSpin.setPreferredSize(new Dimension(60, 30)); yearSpin.setName("Year"); yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####")); yearSpin.addChangeListener(this); panel.add(yearSpin); monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1, 12, 1)); monthSpin.setPreferredSize(new Dimension(45, 30)); monthSpin.setName("Month"); monthSpin.addChangeListener(this); monthNameLabel.setPreferredSize(new Dimension(65, 20)); monthNameLabel.setForeground(controlTextColor); monthNameLabel.setHorizontalAlignment(SwingConstants.CENTER); monthNameLabel.setVerticalAlignment(SwingConstants.CENTER); monthNameLabel.setFont(font); monthNameLabel.setText(moonNames[(Integer) monthSpin.getValue() - 1]); // get name from array panel.add(monthNameLabel); panel.add(monthSpin); JLabel monthLabel = new JLabel("الشهر"); monthLabel.setForeground(controlTextColor); monthLabel.setHorizontalAlignment(SwingConstants.CENTER); monthLabel.setVerticalAlignment(SwingConstants.CENTER); monthLabel.setFont(font); panel.add(monthLabel); return panel; } //Fill the days of the month in the buttons on the desired day private JPanel createWeekAndDayPanal() { JPanel panel = new JPanel(); panel.setFont(new Font("Tahoma", Font.PLAIN, 10)); panel.setLayout(new GridLayout(7, 7)); panel.setBackground(Color.white); for (int i = 0; i < 7; i++) { cell = new JLabel(colname[i]); cell.setHorizontalAlignment(JLabel.CENTER); if (i == 6) { cell.setForeground(weekendFontColor); } else { cell.setForeground(weekFontColor); } panel.add(cell); } int actionCommandId = 0; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { JButton numBtn = new JButton(); numBtn.setBorder(null); numBtn.setHorizontalAlignment(SwingConstants.CENTER); numBtn.setActionCommand(String.valueOf(actionCommandId)); numBtn.setBackground(palletTableColor); numBtn.setForeground(dateFontColor); numBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JButton source = (JButton) event.getSource(); if (source.getText().length() == 0) { numBtn.setForeground(dateFontColor); return; } numBtn.setBackground(palletTableColor); numBtn.setForeground(dateFontColor); source.setForeground(todayBackColor); int newDay = Integer.parseInt(source.getText()); da = newDay; setDate(); dayColorUpdate(true); dateDialog.setVisible(false); } }); daysButton[i][j] = numBtn; panel.add(numBtn); actionCommandId++; if (j == 6) { numBtn.setForeground(weekendFontColor); } else { numBtn.setForeground(dateFontColor); } } } return panel; } private Calendar getCalendar() { Calendar calendar = Calendar.getInstance(); calendar.setTime(getDate()); return calendar; } private int getSelectedYear() { return ((Integer) yearSpin.getValue()).intValue(); } private int getSelectedMonth() { return ((Integer) monthSpin.getValue()).intValue(); } // Paint the days of the week and today private void dayColorUpdate(boolean isOldDay) { String s = String.valueOf(da); for (int a = 0; a < 6; a++) { for (int b = 0; b < 7; b++) { daysButton[a][b].setForeground(dateFontColor); daysButton[a][6].setForeground(weekendFontColor); if (daysButton[a][b].getText().equals(s)) { daysButton[a][b].setForeground(todayBackColor); } } } } // Specify the number of days of a cat and start on what day and end on what day private void reflushWeekAndDay() { ye = (Integer) yearSpin.getValue(); mo = (Integer) monthSpin.getValue(); Locale ar = new Locale("ar"); DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar); HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(ye, mo, da); String WeekdayNum = hijrahDate.format(dayOfweekNum); int WeekdayNumb = Integer.parseInt(WeekdayNum); int moondaysnumber = hijrahDate.lengthOfMonth(); int maxDayNo = moondaysnumber; int dayNo = -0 - WeekdayNumb; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { String s = ""; if (dayNo >= 1 && dayNo <= maxDayNo) { s = String.valueOf(dayNo); } daysButton[i][j].setText(s); dayNo++; if (daysButton[i][j].getText().equals("")) { daysButton[i][j].setVisible(false); } else { daysButton[i][j].setVisible(true); } } } dayColorUpdate(false); } // set year when click on jdialog button's and spinner and textfild public void stateChanged(ChangeEvent e) { dayColorUpdate(true); JSpinner source = (JSpinner) e.getSource(); Calendar cal = getCalendar(); if (source.getName().equals("Year")) { cal.set(Calendar.YEAR, getSelectedYear()); } else { cal.set(Calendar.MONTH, getSelectedMonth() - 1); } reflushWeekAndDay(); setDate(); mothnam(); } } // set month when click on jdialog button's and spinner and textfild public void mothnam() { monthNameLabel.setText(moonNames[mo - 1]); } //set date for jtext and jdialog.... String testMethod() { // ADDED DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); HijrahDate today = HijrahChronology.INSTANCE.date(ye, mo, da); String formatted = formatter.format(today); // 07/03/1439 return formatted.toString(); //<<<<<< ADDED } }