Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4342190
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:35:06+00:00 2026-05-21T11:35:06+00:00

I want to open a date picker by clicking on button. But when I

  • 0

I want to open a date picker by clicking on button. But when I click on button, it will show a calendar in top left corner of the screen. I want to see it just below the button. How can I do it?

Also How can I change the size of calendar to make it little bit small?

Below is the example.


package practiceproblems; 

import java.awt.*;  
import java.awt.event.*;  
import javax.swing.*;

class DatePicker {  

int month = java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);
int year = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);
JLabel l = new JLabel("", JLabel.CENTER);  
String day = "";
JDialog d;
JButton[] button = new JButton[49];

public DatePicker(JFrame parent) {  
d = new JDialog();
d.setModal(true);
String[] header = { "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat" };
JPanel p1 = new JPanel(new GridLayout(7, 7));
p1.setPreferredSize(new Dimension(430, 120));
for (int x = 0; x < button.length; x++) {
    final int selection = x;
    button[x] = new JButton();
    button[x].setFocusPainted(false);
    button[x].setBackground(Color.white);
    if (x > 6)
        button[x].addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                day = button[selection].getActionCommand();
                d.dispose();
            }
        });
        if (x < 7) {
            button[x].setText(header[x]);
            button[x].setForeground(Color.red);
        }
        p1.add(button[x]);
}
JPanel p2 = new JPanel(new GridLayout(1, 3));
JButton previous = new JButton("<< Previous");
previous.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
        month--;
        displayDate();
    }
});
p2.add(previous);
p2.add(l);
JButton next = new JButton("Next >>");
next.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
        month++;
        displayDate();
    }
});
p2.add(next);
d.add(p1, BorderLayout.CENTER);
d.add(p2, BorderLayout.SOUTH);
d.pack();
d.setLocationRelativeTo(parent);
displayDate();
d.setVisible(true);

}

public void displayDate() {
for (int x = 7; x < button.length; x++)
    button[x].setText("");
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("MMMM yyyy");
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.set(year, month, 1);
int dayOfWeek = cal.get(java.util.Calendar.DAY_OF_WEEK);
int daysInMonth = cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH);
for (int x = 6 + dayOfWeek, day = 1; day <= daysInMonth; x++, day++)
    button[x].setText("" + day);
l.setText(sdf.format(cal.getTime()));
d.setTitle("Date Picker");

}

public String setPickedDate() {
if (day.equals(""))
    return day;
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd-MM-yyyy");
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.set(year, month, Integer.parseInt(day));
return sdf.format(cal.getTime());
}

}

 class Picker {   

   public static void main(String[] args) {  
    JLabel label = new JLabel("Selected Date:");
    final JTextField text = new JTextField(20);
    JButton b = new JButton("popup");
    JPanel p = new JPanel();
    p.add(label);
    p.add(text);
    p.add(b);
    final JFrame f = new JFrame();
    f.getContentPane().add(p);
    f.pack();
    f.setVisible(true);
    b.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            text.setText(new DatePicker(f).setPickedDate());
        }
    });
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T11:35:07+00:00Added an answer on May 21, 2026 at 11:35 am

    So the code required for you to make the calendar show under the button is below. But before you can use it, there are few changes to your code which you must (1), should (2) and could (3) make.

    1. In the DatePicker constructor get rid off d.setVisible(true); since we are going to do some settings in the action listener before actually showing the dialog.
    2. When you are creating the frame it is always useful to add this line f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); so your application will always close, otherwise you end up with tone of applications consuming your computer resources.
    3. I would recommend for the Date Picker to extend JDialog instead of having the dialog as a variable. Because effectively that what the date picker looks like to be in your code.
    
    
            b.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    DatePicker dp = new DatePicker(f);
                    Point bP = b.getLocationOnScreen();
                    dp.d.setLocation(bP.x, bP.y + b.getHeight()); 
                    dp.d.setVisible(true);
                    text.setText(dp.setPickedDate());
                }
            });
    
    

    When it goes to the second part of your question considering size change Andrew Thompson answered it great, since you are already calling pack, any size modification are undesired.

    Enjoy, Boro.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I added a button that is supposed to open a calendar 'date-picker'. The button
I have a button that I want to open a calendar and then disappear
I want open a path to vim from Screen's copy-mode by Ctrl-A f similarly
I have a php page which has a chart, a date picker(calendar) and a
I want to open a file for reading, the C++ way. I need to
I want to open a folder window, in the appropriate file manager, from within
I want to open a TCP client socket in Python. Do I have to
I want to open and manipulate Excel files with ActiveX. I've had success with
I want to open a save file dialog, have the user enter a filename,
I want to open a file dialog box in user control. I used using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.