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

  • Home
  • SEARCH
  • 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 7948209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:39:32+00:00 2026-06-04T01:39:32+00:00

I am trying to implement an JSpinner for time in to a JTable ,

  • 0

I am trying to implement an JSpinner for time in to a JTable , it worked preaty goot at the first look but after losing the focus of the cell the edited cell is being set to “Thu Jan 01 +time+ UTC 1970” the time is being set correctly. How can I remove the Date from the Time ?

here is my hole SpinnerEditor.class , have added some comments.

Code :

public SpinnerEditor(String timeFormat) {
    super(new JTextField());

    // Default Time I want to Display (1 Hour)
    Time date = new Time(3600000);
    SpinnerDateModel timeModel = new SpinnerDateModel(date, null, null,Calendar.MINUTE);
    spinner = new JSpinner(timeModel);
    editorDate = new JSpinner.DateEditor(spinner, timeFormat);
    spinner.setEditor(editorDate);


    editorDate = ((JSpinner.DateEditor)spinner.getEditor());
    // println result : "Thu Jan 01 01:00:00 UTC 1970"
    System.out.println(editorDate.getTextField().getValue());
    textField = editorDate.getTextField();
    textField.addFocusListener( new FocusListener() {
        public void focusGained( FocusEvent fe ) {
            System.err.println("Got focus");
            //textField.setSelectionStart(0);
            //textField.setSelectionEnd(1);
            SwingUtilities.invokeLater( new Runnable() {
                public void run() {
                    if ( valueSet ) {
                        textField.setCaretPosition(1);
                    }
                }
            });
        }
        public void focusLost( FocusEvent fe ) {
        }
    });
    textField.addActionListener( new ActionListener() {
        public void actionPerformed( ActionEvent ae ) {
            stopCellEditing();
        }
    });
}

// Prepares the spinner component and returns it.
public Component getTableCellEditorComponent(
    JTable table, Object value, boolean isSelected, int row, int column
) {
    if ( !valueSet ) {
        spinner.setValue(value);
    }
    SwingUtilities.invokeLater( new Runnable() {
        public void run() {
            textField.requestFocus();
        }
    });
    return spinner;
}

public boolean isCellEditable( EventObject eo ) {
    System.err.println("isCellEditable");
    if ( eo instanceof KeyEvent ) {
        KeyEvent ke = (KeyEvent)eo;
        System.err.println("key event: "+ke.getKeyChar());    
        textField.setText(String.valueOf(ke.getKeyChar()));
        valueSet = true;
    } else {
        valueSet = false;
    }
    return true;
}

// Returns the spinners current value.
public Object getCellEditorValue() {
    return spinner.getValue();
}

public boolean stopCellEditing() {
    System.err.println("Stopping edit");
    // after stopcellEditing is called the TextField is being set with the wrong values (Thu Jan 01 01:00:00 UTC 1970)
    super.stopCellEditing();
    try {
        if( editorNumeric!=null) 
        {
            editorNumeric.commitEdit();
            spinner.commitEdit();

        }
        if( editorDate!=null)
        {
            SimpleDateFormat lFormat = new SimpleDateFormat("HH:mm");
            textField.setText((spinner.getValue() != null) ? lFormat.format(spinner.getValue()) : "");
        }

    } catch ( java.text.ParseException e ) {
        JOptionPane.showMessageDialog(null,
            "Invalid value, discarding.");
    }

    return true;
}
  • 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-06-04T01:39:34+00:00Added an answer on June 4, 2026 at 1:39 am

    You are confusing Editors and Renderers. Editor is a widget displayed when a cell is being edited. When the cell is no longer edited, the cell renderer is used to “paint” the cell. This is all explained in this paragraph. I would really recommend you to read it, as it explains clearly how table rendering is performed.

    What you should do, is use a Custom CellRenderer for the involved column so that it uses your date formatter.

    Take a look at this tutorial for more information on cell editors and cell renderers.

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

Sidebar

Related Questions

Trying to implement NSCopying for the first time, and I have a question about
SetFocus I'm trying implement the above Se Focus code in a Class Library that
Trying to implement AVAudioplayer and get some metering data of the played music, but
Trying to implement the new FP 10.1 Global error handler into my projects but
Just trying to implement mobFox into my app, but having trouble to make it
I'm trying implement my project in Apache Struts 2 but I'm not very familiar
am trying to implement fluent nhibernate in MVC project...there were no build errors... but
I'm trying implement pull to refresh on a ListFragment but right now none of
Trying to implement Piwik using REST API over http but need a little help.
Trying to implement sticky footer but its not working as planned. It throws it

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.