Results 1 to 19 of 19
  1. #1
    kiki2009 is offline Member
    Join Date
    Jun 2009
    Posts
    43
    Rep Power
    0

    Question display a clocklabel

    how to display a simple clocklabel in JFrame. :confused:

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    Java Code:
    label.setText(...);
    frame.add( label );

  3. #3
    kiki2009 is offline Member
    Join Date
    Jun 2009
    Posts
    43
    Rep Power
    0

    Default

    thanks again for your response, ok, how to display only the clock,not the day,month ,year and IST. Also i want the clock format to be in 12 hours

  4. #4
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    do you know how to print the current time (in the format you want),
    as a System.out.println(...)

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    Check out the SimpleDateFormat class.

  6. #6
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    I suggest you use a Format class.
    It's in the java.text.*;

    Try it.

  7. #7
    kiki2009 is offline Member
    Join Date
    Jun 2009
    Posts
    43
    Rep Power
    0

    Default do you mean this part

    Quote Originally Posted by camickr View Post
    Check out the SimpleDateFormat class.
    SimpleDateFormat sdf = new SimpleDateFormat()
    sdf.applyPattern(" HH:mm:ss 'A.M'");
    setText(sdf.format(new Date()));
    do you mean this part, what about automatics change the text A.M to P.M when it have reach that time to switch back and forth depending on the timing.

  8. #8
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    > do you mean this part, what about automatics change the text A.M to P.M
    > when it have reach that time to switch back and forth depending on the timing.

    you shouldn't have stopped reading when you did - read the rest of it - answers are all there

  9. #9
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    Yeah, Michael Dunn is right.
    In the matter of fact, I have already answered your thread.
    Well it matters if you would implement it...

    Oh, seeming from your reply from the closed thread, are you a Filipino, kiki2009?

  10. #10
    kiki2009 is offline Member
    Join Date
    Jun 2009
    Posts
    43
    Rep Power
    0

    Question

    Quote Originally Posted by Michael Dunn View Post
    > do you mean this part, what about automatics change the text A.M to P.M
    > when it have reach that time to switch back and forth depending on the timing.

    you shouldn't have stopped reading when you did - read the rest of it - answers are all there
    SimpleDateFormat sdf = new SimpleDateFormat()
    sdf.applyPattern(" h:mm:ss a");
    setText(sdf.format(new Date()));
    is this the right way , please check it . also i have another problem about the format of the date to be display in the jtextbox without time, when it use this code below

    SimpleDateFormat sdf = new SimpleDateFormat();
    sdf.applyPattern("dd/MMMM/yyyy " );

    Date aDate =jdc.showDateDialog();
    txtdate.setText(" "+sdf.format(new Date()));
    it display the date without time but it that the current date only i can choose from the calendar.

    please check the whole code below :

    /**
    * @(#)DateChooser.java
    *
    *
    * @author
    * @version 1.00 2010/4/8
    */

    package datechooser;

    import jdatechooser.JDateChooser;

    import java.awt.*;
    import java.awt.Toolkit;
    import java.awt.event.*;
    import java.util.Date;
    import java.text.SimpleDateFormat;
    import javax.swing.*;

    public class DateChooser extends JFrame {


    final JTextField txtdate;
    JLabel lbldate;
    JButton btndate;

    Container cpane;

    public DateChooser() {

    Container cpane = getContentPane();

    JPanel jp = new JPanel();

    cpane.add(jp);

    lbldate = new JLabel("Date");
    txtdate = new JTextField(15);
    btndate = new JButton("choose");

    jp.add(lbldate);
    jp.add(txtdate);
    jp.add(btndate);

    btndate.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){

    JDateChooser jdc = new JDateChooser(DateChooser.this);

    SimpleDateFormat sdf = new SimpleDateFormat();
    sdf.applyPattern("dd/MMMM/yyyy " );

    Date aDate =jdc.showDateDialog();

    txtdate.setText(" "+sdf.format(new Date()));
    //Date aDate = jdc.showDateDialog();
    //txtdate.setText(""+aDate);
    }
    });
    }

    public static void main(String args[])
    {

    DateChooser dc = new DateChooser();
    dc.setTitle("Date Chooser");
    dc.setSize(300,200);
    dc.setVisible(true);

    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    dc.setLocation((screen.width - 300)/2,((screen.height-200)/2));

    }
    }
    /**
    * @(#)CalendarModel.java
    *
    *
    * @author :
    * @version 1.00 2010/4/6
    */
    package calendarmodel;

    import javax.swing.table.*;
    import javax.swing.event.*;
    import java.util.*;


    public class CalendarModel extends AbstractTableModel {

    private GregorianCalendar c, scroller;
    private String[] dayNames,monthNames;
    private String[][] dayMatrix = new String[6][7];

    public CalendarModel(Date initialDate,String[] months, String[] days){
    dayNames = days;
    monthNames = months;
    c = new GregorianCalendar();
    c.setTime(initialDate);
    scroller = new GregorianCalendar();
    scroller.setTime(initialDate);
    int thisMonth = scroller.get(Calendar.MONTH);
    scroller.set(Calendar.DAY_OF_MONTH,1);
    while(scroller.get(Calendar.MONTH)==thisMonth){
    dayMatrix[scroller.get(Calendar.WEEK_OF_MONTH)-1][scroller.get(Calendar.DAY_OF_WEEK)-1]=""+ scroller.get(Calendar.DAY_OF_MONTH);
    scroller.add(Calendar.DAY_OF_MONTH,1);

    }

    }

    public String[] getLocalizedMonthNames(){
    return monthNames;
    }

    public void setMonth(int month){
    scroller.set(Calendar.MONTH,month);
    c.set(Calendar.MONTH,month);
    updateMatrix();
    fireTableChanged(new TableModelEvent(this));
    }

    public void setYear(int year){
    scroller.set(Calendar.YEAR,year);
    scroller.set(Calendar.MONTH,c.get(Calendar.MONTH)) ;
    c.set(Calendar.YEAR,year);
    updateMatrix();
    fireTableChanged(new TableModelEvent(this));
    }

    public void setDayMatrix(int row, int column){
    if(row<0){return;}
    if(column<0){return;}
    if(dayMatrix[row][column]!=null){
    int newDate = Integer.parseInt(dayMatrix[row][column]);
    scroller.set(Calendar.DAY_OF_MONTH,newDate);
    c.set(Calendar.DAY_OF_MONTH,newDate);
    }
    }

    public String getLocalizedMonthName(){
    return monthNames[scroller.get(Calendar.MONTH)-1];
    }

    public String getYear(){
    return scroller.get(Calendar.YEAR)+"";
    }

    public Date getSelectedDate(){
    return c.getTime();
    }

    public boolean isCellEditable(int row,int column){
    setDayMatrix(row,column);
    return false;
    }

    public int getRowCount(){
    return 6;
    }

    public int getColumnCount(){
    return 7;
    }

    public String getColumnName(int columnIndex){
    return dayNames[columnIndex];
    }

    public Object getValueAt(int row,int column){
    return dayMatrix[row][column];
    }

    private void updateMatrix(){
    dayMatrix = new String[6][7];
    int thisMonth = scroller.get(Calendar.MONTH);
    scroller.set(Calendar.DAY_OF_MONTH,1);
    while(scroller.get(Calendar.MONTH)==thisMonth){
    dayMatrix[scroller.get(Calendar.WEEK_OF_MONTH)-1][scroller.get(Calendar.DAY_OF_WEEK)-1]=""+ scroller.get(Calendar.DAY_OF_MONTH);
    scroller.add(Calendar.DAY_OF_MONTH,1);
    }
    }

    }
    /**
    * @(#)JDateChooser.java
    *
    *
    * @author :
    * @version 1.00 2010/4/6
    */

    package jdatechooser;

    import calendarmodel.CalendarModel;

    import java.text.SimpleDateFormat;
    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;


    public class JDateChooser{

    protected String s,defaultYears[];
    protected String[] defaultMonths = {"January","February","March","April","May","June" ,"July","August","September","October","November", "December"};
    protected String[] defaultDays = {"Sunday","Monday","Tuesday","Wednesday","Thursday ","Friday","Saturday"};
    protected Frame f;
    private static JDialog dialog;
    protected CalendarModel cm;
    protected JComboBox monthList,yearList;
    protected JTable table;
    protected Date d;

    public JDateChooser(Frame owner){
    this(owner,"Select a Date");
    }

    public JDateChooser(Frame owner, String title){
    f = owner;
    s = title;
    defaultYears = new String[150];
    int startYear = 1970;
    for(int i=0;i<defaultYears.length;i++){
    defaultYears[i]=(startYear+i)+"";
    }
    }

    public Date showDateDialog(){
    return showDateDialog(new Date(),defaultMonths,defaultDays,defaultYears);
    }

    public Date showDateDialog(String[] monthNames, String[] dayNames){
    return showDateDialog(new Date(),monthNames,dayNames,defaultYears);
    }

    public Date showDateDialog(Date initialDate){
    return showDateDialog(initialDate,defaultMonths,defaultDa ys,defaultYears);
    }

    public Date showDateDialog(Date initialDate, String[] monthNames, String[] dayNames){
    return showDateDialog(initialDate,monthNames,dayNames,def aultYears);
    }

    public Date showDateDialog(Date initialDate, String[] monthNames, String[] dayNames, String[] yearNames){
    d = initialDate;
    dialog = new JDialog(f,s,true);
    cm = new CalendarModel(d,monthNames,dayNames);
    table = new JTable(cm);
    table.setShowGrid(false);
    table.setRowSelectionAllowed(false);
    table.setSelectionBackground(Color.blue);
    table.setSelectionForeground(Color.white);
    JButton okButton = new JButton("Ok");
    JButton cancelButton = new JButton("Cancel");
    monthList = new JComboBox(cm.getLocalizedMonthNames());
    monthList.setSelectedItem(cm.getLocalizedMonthName ());
    yearList = new JComboBox(yearNames);
    yearList.setSelectedItem(cm.getYear());
    JPanel topPanel = new JPanel(new FlowLayout());
    topPanel.add(monthList);
    topPanel.add(yearList);
    JPanel buttonPanel = new JPanel(new FlowLayout());
    buttonPanel.add(okButton);
    buttonPanel.add(cancelButton);
    JScrollPane pane = new JScrollPane(table);
    pane.getViewport().setBackground(Color.white);
    okButton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    JDateChooser.dialog.setVisible(false);
    d = cm.getSelectedDate();
    }
    });
    cancelButton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    JDateChooser.dialog.setVisible(false);
    d = null;
    }
    });
    monthList.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    cm.setMonth(monthList.getSelectedIndex());
    }
    });
    yearList.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    String aYear = yearList.getSelectedItem().toString();
    cm.setYear(Integer.parseInt(aYear));
    }

    });
    dialog.setSize(200,220);
    dialog.setLocation(450, 317);
    dialog.getContentPane().setLayout(new BorderLayout());
    dialog.getContentPane().add(topPanel,BorderLayout. NORTH);
    dialog.getContentPane().add(buttonPanel,BorderLayo ut.SOUTH);
    dialog.getContentPane().add(pane,BorderLayout.CENT ER);
    dialog.show();
    return d;
    }

    }

  11. #11
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    > please check the whole code below :

    checked it, poorly formatted, so reading it is not on.

    you stated this in an earlier post
    "how to display only the clock,not the day,month ,year and IST"

    yet the class is called 'DateChooser', so, do you have any idea what you really want?

  12. #12
    kiki2009 is offline Member
    Join Date
    Jun 2009
    Posts
    43
    Rep Power
    0

    Default

    sorry i make a mistake in quoting that three package in the same quote. This another one which i'm facing difficult to get the date in the jtextbox, which it take only the current date when i choose from the calendar also.

  13. #13
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    Well, I think you should apply JFormattedTextField, JFTF was created to apply this formmatted schemes (i should say) for easy development.

  14. #14
    kiki2009 is offline Member
    Join Date
    Jun 2009
    Posts
    43
    Rep Power
    0

    Default

    thank again for your help

  15. #15
    kiki2009 is offline Member
    Join Date
    Jun 2009
    Posts
    43
    Rep Power
    0

    Default please check whether the adding part is correct or not

    confused:please check this is correct or not i have added some part of the code to view the date in simple format.
    this part below i have added in that DateChooser package
    String st = sdf.format(aDate);
    txtdate.setText(""+st);
    package datechooser;

    import jdatechooser.JDateChooser;

    import java.awt.*;
    import java.awt.Toolkit;
    import java.awt.event.*;
    import java.util.Date;
    import java.text.SimpleDateFormat;
    import javax.swing.*;

    public class DateChooser extends JFrame {


    final JTextField txtdate;
    JLabel lbldate;
    JButton btndate;

    Container cpane;

    public DateChooser() {

    Container cpane = getContentPane();

    JPanel jp = new JPanel();

    cpane.add(jp);

    lbldate = new JLabel("Date");
    txtdate = new JTextField(15);
    btndate = new JButton("choose");

    jp.add(lbldate);
    jp.add(txtdate);
    jp.add(btndate);

    btndate.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){

    JDateChooser jdc = new JDateChooser(DateChooser.this);

    Date aDate =jdc.showDateDialog();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
    String st = sdf.format(aDate);
    txtdate.setText(""+st);
    }
    });
    }

    public static void main(String args[])
    {

    DateChooser dc = new DateChooser();
    dc.setTitle("Date Chooser");
    dc.setSize(300,200);
    dc.setVisible(true);

    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    dc.setLocation((screen.width - 300)/2,((screen.height-200)/2));

    }
    }
    package calendarmodel;

    import javax.swing.table.*;
    import javax.swing.event.*;
    import java.util.*;


    public class CalendarModel extends AbstractTableModel {

    private GregorianCalendar c, scroller;
    private String[] dayNames,monthNames;
    private String[][] dayMatrix = new String[6][7];

    public CalendarModel(Date initialDate,String[] months, String[] days){
    dayNames = days;
    monthNames = months;
    c = new GregorianCalendar();
    c.setTime(initialDate);
    scroller = new GregorianCalendar();
    scroller.setTime(initialDate);
    int thisMonth = scroller.get(Calendar.MONTH);
    scroller.set(Calendar.DAY_OF_MONTH,1);
    while(scroller.get(Calendar.MONTH)==thisMonth){
    dayMatrix[scroller.get(Calendar.WEEK_OF_MONTH)-1][scroller.get(Calendar.DAY_OF_WEEK)-1]=""+ scroller.get(Calendar.DAY_OF_MONTH);
    scroller.add(Calendar.DAY_OF_MONTH,1);

    }

    }

    public String[] getLocalizedMonthNames(){
    return monthNames;
    }

    public void setMonth(int month){
    scroller.set(Calendar.MONTH,month);
    c.set(Calendar.MONTH,month);
    updateMatrix();
    fireTableChanged(new TableModelEvent(this));
    }

    public void setYear(int year){
    scroller.set(Calendar.YEAR,year);
    scroller.set(Calendar.MONTH,c.get(Calendar.MONTH)) ;
    c.set(Calendar.YEAR,year);
    updateMatrix();
    fireTableChanged(new TableModelEvent(this));
    }

    public void setDayMatrix(int row, int column){
    if(row<0){return;}
    if(column<0){return;}
    if(dayMatrix[row][column]!=null){
    int newDate = Integer.parseInt(dayMatrix[row][column]);
    scroller.set(Calendar.DAY_OF_MONTH,newDate);
    c.set(Calendar.DAY_OF_MONTH,newDate);
    }
    }

    public String getLocalizedMonthName(){
    return monthNames[scroller.get(Calendar.MONTH)-1];
    }

    public String getYear(){
    return scroller.get(Calendar.YEAR)+"";
    }

    public Date getSelectedDate(){
    return c.getTime();
    }

    public boolean isCellEditable(int row,int column){
    setDayMatrix(row,column);
    return false;
    }

    public int getRowCount(){
    return 6;
    }

    public int getColumnCount(){
    return 7;
    }

    public String getColumnName(int columnIndex){
    return dayNames[columnIndex];
    }

    public Object getValueAt(int row,int column){
    return dayMatrix[row][column];
    }

    private void updateMatrix(){
    dayMatrix = new String[6][7];
    int thisMonth = scroller.get(Calendar.MONTH);
    scroller.set(Calendar.DAY_OF_MONTH,1);
    while(scroller.get(Calendar.MONTH)==thisMonth){
    dayMatrix[scroller.get(Calendar.WEEK_OF_MONTH)-1][scroller.get(Calendar.DAY_OF_WEEK)-1]=""+ scroller.get(Calendar.DAY_OF_MONTH);
    scroller.add(Calendar.DAY_OF_MONTH,1);
    }
    }

    }
    package jdatechooser;

    import calendarmodel.CalendarModel;

    import java.text.SimpleDateFormat;
    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;


    public class JDateChooser{

    protected String s,defaultYears[];
    protected String[] defaultMonths = {"January","February","March","April","May","Ju ne" ,"July","August","September","October","Novembe r", "December"};
    protected String[] defaultDays = {"Sunday","Monday","Tuesday","Wednesday","Thurs day ","Friday","Saturday"};
    protected Frame f;
    private static JDialog dialog;
    protected CalendarModel cm;
    protected JComboBox monthList,yearList;
    protected JTable table;
    protected Date d;

    public JDateChooser(Frame owner){
    this(owner,"Select a Date");
    }

    public JDateChooser(Frame owner, String title){
    f = owner;
    s = title;
    defaultYears = new String[150];
    int startYear = 1970;
    for(int i=0;i<defaultYears.length;i++){
    defaultYears[i]=(startYear+i)+"";
    }
    }

    public Date showDateDialog(){
    return showDateDialog(new Date(),defaultMonths,defaultDays,defaultYears);
    }

    public Date showDateDialog(String[] monthNames, String[] dayNames){
    return showDateDialog(new Date(),monthNames,dayNames,defaultYears);
    }

    public Date showDateDialog(Date initialDate){
    return showDateDialog(initialDate,defaultMonths,defaultDa ys,defaultYears);
    }

    public Date showDateDialog(Date initialDate, String[] monthNames, String[] dayNames){
    return showDateDialog(initialDate,monthNames,dayNames,def aultYears);
    }

    public Date showDateDialog(Date initialDate, String[] monthNames, String[] dayNames, String[] yearNames){
    d = initialDate;
    dialog = new JDialog(f,s,true);
    cm = new CalendarModel(d,monthNames,dayNames);
    table = new JTable(cm);
    table.setShowGrid(false);
    table.setRowSelectionAllowed(false);
    table.setSelectionBackground(Color.blue);
    table.setSelectionForeground(Color.white);
    JButton okButton = new JButton("Ok");
    JButton cancelButton = new JButton("Cancel");
    monthList = new JComboBox(cm.getLocalizedMonthNames());
    monthList.setSelectedItem(cm.getLocalizedMonthName ());
    yearList = new JComboBox(yearNames);
    yearList.setSelectedItem(cm.getYear());
    JPanel topPanel = new JPanel(new FlowLayout());
    topPanel.add(monthList);
    topPanel.add(yearList);
    JPanel buttonPanel = new JPanel(new FlowLayout());
    buttonPanel.add(okButton);
    buttonPanel.add(cancelButton);
    JScrollPane pane = new JScrollPane(table);
    pane.getViewport().setBackground(Color.white);
    okButton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    JDateChooser.dialog.setVisible(false);
    d = cm.getSelectedDate();
    }
    });
    cancelButton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    JDateChooser.dialog.setVisible(false);
    d = null;
    }
    });
    monthList.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    cm.setMonth(monthList.getSelectedIndex());
    }
    });
    yearList.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    String aYear = yearList.getSelectedItem().toString();
    cm.setYear(Integer.parseInt(aYear));
    }

    });
    dialog.setSize(200,220);
    dialog.setLocation(450, 317);
    dialog.getContentPane().setLayout(new BorderLayout());
    dialog.getContentPane().add(topPanel,BorderLayout. NORTH);
    dialog.getContentPane().add(buttonPanel,BorderLayo ut.SOUTH);
    dialog.getContentPane().add(pane,BorderLayout.CENT ER);
    dialog.show();
    return d;
    }

    }

  16. #16
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    seriously, do you write code like that, in the format you present here?

    if so, apply to Macdonalds
    if not, why should we read garbage?

  17. #17
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    i'm confused on your code kiki2009.
    i agree with Michael Dunn.

    but you must level-out your codes a little bit (like adding indents)
    anyways, what was the main goal of the code posted above?

  18. #18
    kiki2009 is offline Member
    Join Date
    Jun 2009
    Posts
    43
    Rep Power
    0

    Default i want u to check where is that date format mistake

    i'm sorry abt that arrangement, i'll try to make i better and please do help me how to write in the correct way, also i'm mess up here and there. Thank for your advise.

  19. #19
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    Is rulaifozu's post legal in this forums?
    Anyways. kiki2009, I think you should maximize the java.util.Calendar package.
    It might shorten your code.

    I am pertaining to its interface. See the java.util.Calendar package on JavaDocs 6.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

Similar Threads

  1. Display tag help
    By kirtichopra2003 in forum Advanced Java
    Replies: 0
    Last Post: 11-12-2009, 09:10 AM
  2. how to display a jsp by struts.xml
    By kirtichopra2003 in forum Advanced Java
    Replies: 1
    Last Post: 09-14-2009, 10:29 AM
  3. Replies: 8
    Last Post: 12-29-2008, 07:12 AM
  4. Please help. Trying to display a sphere.
    By aknbad23 in forum New To Java
    Replies: 6
    Last Post: 10-07-2008, 05:37 PM
  5. Replies: 0
    Last Post: 06-28-2008, 09:26 PM

Posting Permissions

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