Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-09-2009, 08:04 AM
nehaa's Avatar
Member
 
Join Date: Jan 2009
Posts: 35
Rep Power: 0
nehaa is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 05-09-2009, 08:13 AM
Darryl.Burke's Avatar
Senior Member
 
Join Date: Sep 2008
Location: Madgaon, Goa, India
Posts: 662
Rep Power: 2
Darryl.Burke is on a distinguished road
Default
Ever heard of SimpleDateFormat?

db
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-09-2009, 08:23 AM
nehaa's Avatar
Member
 
Join Date: Jan 2009
Posts: 35
Rep Power: 0
nehaa is on a distinguished road
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

Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-09-2009, 09:05 AM
nehaa's Avatar
Member
 
Join Date: Jan 2009
Posts: 35
Rep Power: 0
nehaa is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-09-2009, 12:20 PM
Darryl.Burke's Avatar
Senior Member
 
Join Date: Sep 2008
Location: Madgaon, Goa, India
Posts: 662
Rep Power: 2
Darryl.Burke is on a distinguished road
Default
First tell me what exactly you mean by "i'm getting the format"

db
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-10-2009, 06:52 AM
nehaa's Avatar
Member
 
Join Date: Jan 2009
Posts: 35
Rep Power: 0
nehaa is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-10-2009, 01:00 PM
Darryl.Burke's Avatar
Senior Member
 
Join Date: Sep 2008
Location: Madgaon, Goa, India
Posts: 662
Rep Power: 2
Darryl.Burke is on a distinguished road
Default
No you're not. Repeating the same sentence in BLOCK CAPITALS is not a clarification, it's just rude.

Clarify "getting"

db
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 05-10-2009, 01:19 PM
nehaa's Avatar
Member
 
Join Date: Jan 2009
Posts: 35
Rep Power: 0
nehaa is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 05-10-2009, 02:51 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
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:

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();
      }
    });
  }

}
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 05-11-2009, 09:58 AM
nehaa's Avatar
Member
 
Join Date: Jan 2009
Posts: 35
Rep Power: 0
nehaa is on a distinguished road
Smile Thanks for your consideration



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

Thanks again.

Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 05-11-2009, 02:34 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
Default
Cool. Glad it helped.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Jtable Column Selection Problem REVANSIDDHA Advanced Java 0 03-31-2009 08:53 AM
JCheckBox in JTable column hind New To Java 8 01-04-2009 08:40 PM
tooltips for JTable column headers fossildoc AWT / Swing 2 12-18-2008 12:42 PM
How to sort column in JTable johnt AWT / Swing 3 06-14-2008 06:48 AM
insert row and column and delete row and column daredavil82 New To Java 7 11-30-2007 11:32 AM


All times are GMT +2. The time now is 07:04 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org