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 6970441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:42:39+00:00 2026-05-27T16:42:39+00:00

I had initially built a DatePicker. A Calendar Dialog will open next to a

  • 0

I had initially built a DatePicker. A Calendar Dialog will open next to a JTextField as soon as a person clicks on the JtextField. If a person wants to type the date instead of selecting the date, then he would have first close the open dialog and then type the date.

Here’s how it looks:

public class DateTextField extends JTextField implements Observer {

public void update(Observable o, Object arg) {
    Calendar calendar = (Calendar) arg;
    DatePicker dp = (DatePicker) o;
    setText(dp.formatDate(calendar));
}

public DateTextField() {
    final Observer obs = this;
    final JTextField tf = this;
    this.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent e) {
            if(tf.isEditable()){
                DatePicker dp = new DatePicker(obs);
                Date selectedDate = dp.parseDate(tf.getText());
                dp.setSelectedDate(selectedDate);
                dp.start(tf);
            }
        }
    });
}

I havent shown the DatePicker class because that’s not the issue here. The issue is, it was very irritating for a person to close the Calendar dialog everytime to type the date. Instead what I wanted to do was keep a button on the side of the JtextField and only if the person clicks on the button would the dialog pop up.

But to do that I would need the DatePicker to extend a JPanel class and I could layout the JTextfield and the JButton side by side. But unfortunately, the DatePicker class extends JtextField and all over my application I am expecting DatePicker to be a JtextField. There are casts from JComponent to JTextfield and calls to get and setText() on this DatePicker.

If I have to change DatePicker to extend form JPanel then it would break my application everywhere. I was wondering if I can somehow dress up the JTextfield to show the button on the side maybe by painting it or something? Is this is possible?

  • 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-27T16:42:39+00:00Added an answer on May 27, 2026 at 4:42 pm

    For anyone wanting to see how later on.. the code is listed below. You override paint and draw the image on the left hand corner of the text field. Then implement the mouseClicked to only response if the image has been clicked. Also implement a mouse motion listener to change the cursor to a hand pointer if its on the image and a text cursor if it is not.

    public class DateTextField extends JTextField implements Observer {
    
    public void update(Observable o, Object arg) {
        Calendar calendar = (Calendar) arg;
        DatePicker dp = (DatePicker) o;
        setText(dp.formatDate(calendar));
    }
    
    public DateTextField() {
        final Observer obs = this;
        final JTextField tf = this;
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(tf.isEditable()){
                    Point p = e.getPoint();
                    if(p.x>=getWidth()-22 && p.x <getWidth()
                            && p.y<=getHeight()) {
                        DatePicker dp = new DatePicker(obs);
                        Date selectedDate = dp.parseDate(tf.getText());
                        dp.setSelectedDate(selectedDate);
                        dp.start(tf);
                    }
                }
            }
        });
    
        this.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                Point p = e.getPoint();
                if(p.x>=getWidth()-22 && p.x <getWidth()
                        && p.y<=getHeight()) {
                    DateTextField.this.setCursor(new Cursor(Cursor.HAND_CURSOR));
                }else {
                    DateTextField.this.setCursor(new Cursor(Cursor.TEXT_CURSOR));
                }
            }
        });
    }
    
    public DateTextField(String text) {
        this();
        this.setText(text);
    }
    
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        try {
            BufferedImage img = ImageIO.read(new File(DateTextField.class.getResource("/images/calendar.png").getFile()));
            int x=getWidth()-22;
            g.drawImage(img,x,1,20,getHeight()-2,null);
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently wrote a program that used a simple producer/consumer pattern. It initially had
Initially I had a method in our DL that would take in the object
For this assignment I had to create my own string class. I initially wrote
I have multiple apps running which access a sql server database. I had initially
In my initial days of using linux I usually had to search google to
Had an interesting discussion with some colleagues about the best scheduling strategies for realtime
Had a coworker ask me this, and in my brain befuddled state I didn't
Had a page that was working fine. Only change I made was to add
Had a good search here but can't see anything that gets my mind in
I had used Server Explorer and related tools for graphical database development with Microsoft

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.