Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2008

Tip: Looking for answers? Try searching our database.

JTable display

Thread view: 
Abbas - 25 Mar 2008 11:46 GMT
In the below code I'm trying to display two fields (String and Date),
what happens is I'm not able to get the desired display of date in the
JTable. Can any one help me in getting the date displayed like "H:m |
dd-MM-yyyy"?

Thanks,
Abbas

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class tableTest {
    public JFrame mainFrame = new JFrame("Java Task Manager");
    public JPanel mainPanel = new JPanel();
    public JTable mainTable = new JTable();
    public DefaultTableModel defaultTableModel = new DefaultTableModel(){
        public Class getColumnClass(int c){
            return  getValueAt(0,c).getClass();
        }
    };

    public class CustomDate extends Date {

        public CustomDate(){

        }
        public CustomDate(long arg0){
            super(arg0);
        }
        public String toString(){
            SimpleDateFormat sl = new SimpleDateFormat("H:m | dd-MMM-yyyy");
            return sl.format(this);
        }
    }

    public void showUI(){
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.getContentPane().add(mainPanel);
        mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.X_AXIS));
        mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
        mainPanel.add(new JScrollPane(mainTable));
        mainTable.setModel(defaultTableModel);
        String[] cid = {"Name","Date"};
        defaultTableModel.setColumnIdentifiers(cid);
        String[] nms = {"a","ss","de","des","fes"};
        Calendar cl = Calendar.getInstance();
        Date[] dts = new Date[5];
        //1
        cl.set(2008, Calendar.JANUARY, 1, 12, 44);
        dts[0] = new CustomDate(cl.getTimeInMillis());
        //2
        cl.set(2008, Calendar.FEBRUARY, 22, 10, 12);
        dts[1] = new CustomDate(cl.getTimeInMillis());
        //3
        cl.set(2008, Calendar.MARCH, 1, 1, 2);
        dts[2] = new CustomDate(cl.getTimeInMillis());
        //4
        cl.set(2008, Calendar.APRIL, 1, 1, 32);
        dts[3] = new CustomDate(cl.getTimeInMillis());
        //5
        cl.set(2008, Calendar.MAY, 1, 13, 14);
        dts[4] = new CustomDate(cl.getTimeInMillis());
        for(int i=0;i<5;i++){
            Object[] ob= {nms[i],dts[i]};
            defaultTableModel.addRow(ob);
        }
        mainTable.setAutoCreateRowSorter(true);
        mainFrame.pack();
        mainFrame.setResizable(false);
        mainFrame.setVisible(true);
    }
    /**
    * @param args
    */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        tableTest ts = new tableTest();
        ts.showUI();
    }
}
Roedy Green - 25 Mar 2008 13:53 GMT
On Tue, 25 Mar 2008 03:46:58 -0700 (PDT), Abbas
<a.mohamed.abbas.ali@gmail.com> wrote, quoted or indirectly quoted
someone who said :

> date displayed like "H:m |
>dd-MM-yyyy"?

see http://mindprod.com/jgloss/jtable.html

There is code for a DateRenderer.  You would modify it to use a
SimpleDateFormat to get what you want.

See http://mindprod.com/jgloss/calendar.html
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Abbas - 25 Mar 2008 15:45 GMT
By adding this     "public class drend extends DefaultTableCellRenderer {
        public Component getTableCellRendererComponent(JTable table, Object
value, boolean isSelected, boolean hasFocus, int row, int column) {
            setText(value.toString());
            return this;
        }
    }"

I got what I wanted!

Thanks,
Abbas

import java.awt.Component;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class tableTest {
    public JFrame mainFrame = new JFrame("Java Task Manager");
    public JPanel mainPanel = new JPanel();
    public JTable mainTable = new JTable();
    public DefaultTableModel defaultTableModel = new DefaultTableModel(){
        public Class getColumnClass(int c){
            return  getValueAt(0,c).getClass();
        }
    };

    public class CustomDate extends Date {

        public CustomDate(){

        }
        public CustomDate(long arg0){
            super(arg0);
        }
        public String toString(){
            SimpleDateFormat sl = new SimpleDateFormat("H:m | dd-MMM-yyyy");
            return sl.format(this);
        }
    }

    public class drend extends DefaultTableCellRenderer {
        public Component getTableCellRendererComponent(JTable table, Object
value, boolean isSelected, boolean hasFocus, int row, int column) {
            setText(value.toString());
            return this;
        }
    }

    public void showUI(){
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.getContentPane().add(mainPanel);
        mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.X_AXIS));
        mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
        mainPanel.add(new JScrollPane(mainTable));
        mainTable.setModel(defaultTableModel);
        String[] cid = {"Name","Date"};
        defaultTableModel.setColumnIdentifiers(cid);
        String[] nms = {"a","ss","de","des","fes"};
        Calendar cl = Calendar.getInstance();
        Date[] dts = new Date[5];
        //1
        cl.set(2008, Calendar.JANUARY, 1, 12, 44);
        dts[0] = new CustomDate(cl.getTimeInMillis());
        //2
        cl.set(2008, Calendar.FEBRUARY, 22, 10, 12);
        dts[1] = new CustomDate(cl.getTimeInMillis());
        //3
        cl.set(2008, Calendar.MARCH, 1, 1, 2);
        dts[2] = new CustomDate(cl.getTimeInMillis());
        //4
        cl.set(2008, Calendar.APRIL, 1, 1, 32);
        dts[3] = new CustomDate(cl.getTimeInMillis());
        //5
        cl.set(2008, Calendar.MAY, 1, 13, 14);
        dts[4] = new CustomDate(cl.getTimeInMillis());
        for(int i=0;i<5;i++){
            Object[] ob= {nms[i],dts[i]};
            defaultTableModel.addRow(ob);
        }

        mainTable.setDefaultRenderer(Date.class, new drend());
        //TablemainTable.getColorModel() getColumn(1).setHeaderRenderer(new
drend());
        mainTable.setAutoCreateRowSorter(true);
        mainFrame.pack();
        mainFrame.setResizable(false);
        mainFrame.setVisible(true);
    }
    /**
    * @param args
    */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        tableTest ts = new tableTest();
        ts.showUI();
    }
}

> > date displayed like "H:m |
> >dd-MM-yyyy"?
[quoted text clipped - 9 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com
Lew - 26 Mar 2008 01:42 GMT
> By adding this     "public class drend extends DefaultTableCellRenderer {

Class names are supposed to start with an upper-case letter.

Do not embed TABs in Usenet listings.

Signature

Lew

John B. Matthews - 25 Mar 2008 14:39 GMT
In article
<102e154f-829f-46e5-a533-fd0c2c6de19f@h11g2000prf.googlegroups.com>,

> In the below code I'm trying to display two fields (String and Date),
> what happens is I'm not able to get the desired display of date in the
[quoted text clipped - 3 lines]
> Thanks,
> Abbas
[...]
>     public JTable mainTable = new JTable();
>     public DefaultTableModel defaultTableModel = new DefaultTableModel(){
>         public Class getColumnClass(int c){
>             return  getValueAt(0,c).getClass();
>         }
>     };
[...]

Your implementation of getColumnClass() is incorrect. The default
implementation provided by AbstractTableModel does what you want:

public DefaultTableModel defaultTableModel = new DefaultTableModel();

John
Signature

John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews

Abbas - 25 Mar 2008 15:49 GMT
John,

Since I want the table to sortable by date, I have to implement the
getColumnClass() that way (I dont know any other better way!).
Otherwise the column gets sorted as a string not as a date.

Thanks,
Abbas
> In article
> <102e154f-829f-46e5-a533-fd0c2c6de...@h11g2000prf.googlegroups.com>,
[quoted text clipped - 26 lines]
> trashgod at gmail dot com
> home dot woh dot rr dot com slash jbmatthews
John B. Matthews - 26 Mar 2008 01:09 GMT
In article
<8cd76d57-5d0d-4c5b-a929-8f6d308d4e4d@d21g2000prf.googlegroups.com>,

> John,
>
[quoted text clipped - 4 lines]
> Thanks,
> Abbas

Here's an implementation that might suffice:

 public DefaultTableModel defaultTableModel = new DefaultTableModel() {
     public Class getColumnClass(int c) {
         return Object.class;
     }
 };

John

> > In article
> > <102e154f-829f-46e5-a533-fd0c2c6de...@h11g2000prf.googlegroups.com>,
[quoted text clipped - 26 lines]
> > trashgod at gmail dot com
> > home dot woh dot rr dot com slash jbmatthews
Abbas - 26 Mar 2008 05:34 GMT
John,

It does work! But how Object.class returns column class? Can you
please explain? I'm very new to java, picking it up.

Thanks,
Abbas

> In article
> <8cd76d57-5d0d-4c5b-a929-8f6d308d4...@d21g2000prf.googlegroups.com>,
[quoted text clipped - 48 lines]
> > > trashgod at gmail dot com
> > > home dot woh dot rr dot com slash jbmatthews
John B. Matthews - 26 Mar 2008 13:24 GMT
In article
<34d3d849-2266-4dcb-a1a9-05a43762dd8a@m44g2000hsc.googlegroups.com>,

> John,
>
[quoted text clipped - 3 lines]
> Thanks,
> Abbas

The interface for getColumnClass() requires that it "Returns the most
specific superclass for all the cell values in the column." The
implementation of getColumnClass() provided by AbstractTableModel
"Returns Object.class regardless of columnIndex." If you override
getColumnClass(), you should do the same.

Also, consider <http://en.wikipedia.org/wiki/Posting_style>.

John

> > In article
> > <8cd76d57-5d0d-4c5b-a929-8f6d308d4...@d21g2000prf.googlegroups.com>,
[quoted text clipped - 50 lines]
> > > > trashgod at gmail dot com
> > > > home dot woh dot rr dot com slash jbmatthews


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.