Results 1 to 11 of 11
  1. #1
    nehaa's Avatar
    nehaa is offline Member
    Join Date
    Jan 2009
    Posts
    35
    Rep Power
    0

    Thumbs up [SOLVED] JSpinner in JTable column

    With the code given below i'm getting the format

    Sat May 09 10:12:00 IST 2009

    But I want only May 09 2009


    import java.awt.Component;
    import java.util.Calendar;
    import java.util.Date;

    import javax.swing.AbstractCellEditor;
    import javax.swing.JSpinner;
    import javax.swing.JTable;
    import javax.swing.SpinnerDateModel;
    import javax.swing.SpinnerModel;
    import javax.swing.table.TableCellEditor;

    public class DateCellEditor extends AbstractCellEditor implements TableCellEditor
    {
    int selectedRow;
    int selectedColumn;
    JTable currentTable;
    Date currentDate;
    JSpinner spinner;

    protected static final String EDIT = "edit";

    public DateCellEditor()
    {
    Calendar calendar = Calendar.getInstance();
    Date initDate = calendar.getTime();
    System.out.println("INIT DATE++"+initDate);
    calendar.add(Calendar.YEAR, -100);
    Date earliestDate = calendar.getTime();
    calendar.add(Calendar.YEAR, 200);
    System.out.println("EARLIEST DATE++"+earliestDate);
    Date latestDate = calendar.getTime();
    System.out.println("LATEST DATE++"+latestDate);
    SpinnerDateModel dateModel = new SpinnerDateModel(initDate,
    earliestDate,
    latestDate,
    Calendar.YEAR);//ignored for user input
    spinner = new JSpinner(dateModel);

    spinner.setEditor(new JSpinner.DateEditor(spinner, "dd/MM/yyyy"));
    }


    public Component getTableCellEditorComponent(JTable table,
    Object value, boolean isSelected, int row, int column) {
    spinner.setValue(value);
    currentTable = table;
    selectedRow = row;
    selectedColumn = column;
    return spinner;
    }

    public Object getCellEditorValue() {
    return spinner.getValue();
    }


    }



    Please help me out

    Thanks in advance

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Ever heard of SimpleDateFormat?

    db

  3. #3
    nehaa's Avatar
    nehaa is offline Member
    Join Date
    Jan 2009
    Posts
    35
    Rep Power
    0

    Smile Thanks for your consideration

    :)
    No I dont ever heard about SimpleDateFormat.

    I'll definitely try this and will post if it fits my requirement so that can be helpful for others who face same difficulty.


    THANKS FOR UR VALUABLE SUGGESTION AND TIME.


    nb

    :)

  4. #4
    nehaa's Avatar
    nehaa is offline Member
    Join Date
    Jan 2009
    Posts
    35
    Rep Power
    0

    Thumbs up

    I have read about SimpleDateFormat.

    If you please tell me how it can be applied with JSpinner in JTable column.

    Thanks in advance.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    First tell me what exactly you mean by "i'm getting the format"

    db

  6. #6
    nehaa's Avatar
    nehaa is offline Member
    Join Date
    Jan 2009
    Posts
    35
    Rep Power
    0

    Thumbs up Thanks for ur time

    :)

    FIRSTLY I WANT TO THANK YOU...THEN YOUR QUES FOR ME:
    I'M GETTING DATE FORMAT IN GIVEN MANNER
    "Sat May 09 10:12:00 IST 2009".
    BUT I WANT TO DISPLAY DATE IN THIS FORMAT
    "May 09 2009"..
    HOPE THIS TIME I M MORE CLEAR.



    NB
    :)

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    No you're not. Repeating the same sentence in BLOCK CAPITALS is not a clarification, it's just rude.

    Clarify "getting"

    db

  8. #8
    nehaa's Avatar
    nehaa is offline Member
    Join Date
    Jan 2009
    Posts
    35
    Rep Power
    0

    Smile SORRY sorry

    I really was not rude..Neither was my intentions..If you felt so due to these BLOCK CAPITALS;then i m really sorry.My intentions were never to hurt anyone.

    Though now I'll try this problem myself still to clarify:

    I want that in one column of my JTable ,any user can select date which i am trying to provide using JSpinner .So I used the code i have given in my very first post.But with JSpinner(without SimpleDateFormat being used) date is displayed in this manner
    " Sat May 09 10:12:00 IST 2009 "
    that is combination of date and time.
    but i want only date without time means

    " May 09 2009 "..
    In which i am facing some problem.


    Hope now i am successful.

    Thank you

    nb
    :)

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Is your problem with your renderer or your editor? In other words, is the problem with the display of dates when the spinner is active, or is the problem present when you are using the spinner? If the problem is with the renderer, then the tutorials can show you how to use a date renderer. For instance:

    Java Code:
    import java.awt.Component;
    import java.text.DateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    import javax.swing.AbstractCellEditor;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JSpinner;
    import javax.swing.JTable;
    import javax.swing.SpinnerDateModel;
    import javax.swing.SpinnerModel;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellEditor;
    
    public class DateCellEditor extends AbstractCellEditor implements
        TableCellEditor
    {
      int selectedRow;
      int selectedColumn;
      JTable currentTable;
      Date currentDate;
      JSpinner spinner;
    
      protected static final String EDIT = "edit";
    
      public DateCellEditor()
      {
        Calendar calendar = Calendar.getInstance();
        Date initDate = calendar.getTime();
        System.out.println("INIT DATE++" + initDate);
        calendar.add(Calendar.YEAR, -100);
        Date earliestDate = calendar.getTime();
        calendar.add(Calendar.YEAR, 200);
        System.out.println("EARLIEST DATE++" + earliestDate);
        Date latestDate = calendar.getTime();
        System.out.println("LATEST DATE++" + latestDate);
        SpinnerDateModel dateModel = new SpinnerDateModel(initDate, earliestDate,
            latestDate, Calendar.YEAR);// ignored for user input
        spinner = new JSpinner(dateModel);
    
        spinner.setEditor(new JSpinner.DateEditor(spinner, "dd/MM/yyyy"));
      }
    
      public Component getTableCellEditorComponent(JTable table, Object value,
          boolean isSelected, int row, int column)
      {
        spinner.setValue(value);
        currentTable = table;
        selectedRow = row;
        selectedColumn = column;
        return spinner;
      }
    
      public Object getCellEditorValue()
      {
        return spinner.getValue();
      }
    
      // from Sun tutorials: 
      //http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#editrender  
      static class DateRenderer extends DefaultTableCellRenderer
      {
        DateFormat formatter;
    
        public DateRenderer()
        {
          super();
        }
    
        public void setValue(Object value)
        {
          if (formatter == null)
          {
            formatter = DateFormat.getDateInstance();
          }
          setText((value == null) ? "" : formatter.format(value));
        }
      }
    
      
    
      private static void createAndShowUI()
      {
        Date[][] dates =
        {
            {new Date(Calendar.getInstance().getTimeInMillis())}
        };
        String[] titles =
        {
          "Date"
        };
        JTable table = new JTable(dates, titles);
        table.setDefaultRenderer(Object.class, new DateRenderer());
        table.setDefaultEditor(Object.class, new DateCellEditor());
        
        JFrame frame = new JFrame("DateCellEditor");
        frame.getContentPane().add(new JScrollPane(table));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    
    }

  10. #10
    nehaa's Avatar
    nehaa is offline Member
    Join Date
    Jan 2009
    Posts
    35
    Rep Power
    0

    Smile Thanks for your consideration




    Thanks for your valuable suggestion.It worked.My problem got solved.

    Thanks again.
    :)

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. insert row and column and delete row and column
    By daredavil82 in forum New To Java
    Replies: 13
    Last Post: 09-22-2011, 06:10 PM
  2. Jtable Column Selection Problem
    By REVANSIDDHA in forum Advanced Java
    Replies: 0
    Last Post: 03-31-2009, 07:53 AM
  3. JCheckBox in JTable column
    By hind in forum New To Java
    Replies: 8
    Last Post: 01-04-2009, 07:40 PM
  4. tooltips for JTable column headers
    By fossildoc in forum AWT / Swing
    Replies: 2
    Last Post: 12-18-2008, 11:42 AM
  5. How to sort column in JTable
    By johnt in forum AWT / Swing
    Replies: 3
    Last Post: 06-14-2008, 05:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •