toedter.com Board
The JCalendar and HTML sitemap community forum!
Please log in or register.
The date and time is now September 6, 2010, 01:26:17 PM
Home  Search  Help  Log in  Register  Members

New Post Post Reply
toedter.com Board :: JCalendar :: Bugs :: JDateChooser prefered size to small  ::
paradox
Newbie
Image

JCalendar is great!
Posts: 2
JDateChooser prefered size to small (January 31, 2007, 03:22:32 AM) quote  
Hi,

Again, the subject pretty much says it all, the preferred size of the JDateChooser is to small and part of the date in the text field gets cut off. The only work around is to manually set the preferred size of the JDateChooser. The following small program demonstrates this:



public class CalendarTest
{
public static void main(String [] args)
{
JFrame frame = new JFrame();
Container cp = frame.getContentPane();
cp.setLayout(new FlowLayout());

cp.add(new JDateChooser(new Date()));

frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}



I took a bit of a dig through the code and it looks like the problem comes down to JTextFieldDateEditor and is getPreferredSize method.


public Dimension getPreferredSize() {
if (datePattern != null) {
return new JTextField(datePattern).getPreferredSize();
}
return super.getPreferredSize();
}


Perhaps when being used inside a JDateChooser the JTextFieldDateEditors datePattern is not being set correctly, causing it to use the preferred size of its super class?

IP logged Status: logged off Profile 
Order of replies: first reply last :: first reply first
maestroalubia
Newbie
Image


Posts: 3
RE: JDateChooser prefered size to small (July 27, 2007, 05:42:57 PM) quote  
The problem occurs when Swing uses a proportional font (see http://en.wikipedia.org/wiki/Typeface#Proportion) and the width (in pixels) of the date-pattern is shorter than some real date-values.

for example:
dd.MM.yyyy results in a quite small width because the y-char needs less space than the 0-digit in most proportional fonts.

IP logged Status: logged off Profile Website 
maestroalubia
Newbie
Image


Posts: 3
RE: JDateChooser prefered size to small (July 27, 2007, 05:55:31 PM) quote  
I just saw that the font-proportion should be regarded in JTextField whose method getPreferredSize() is called in JTextFieldDateEditor.

I will proceed analyzing this problem..

IP logged Status: logged off Profile Website 
BTracy
Newbie
Image


Posts: 4
RE: JDateChooser prefered size to small (August 7, 2007, 05:51:51 PM) quote  
Just at a glance, could this be because it's not accounting for the size of the calendar button itself in addition to the text field, losing you a good 15 pixels or so?

Apologies if this is way off, I might be misunderstanding.

IP logged Status: logged off Profile 
maestroalubia
Newbie
Image


Posts: 3
RE: JDateChooser prefered size to small (August 7, 2007, 06:04:27 PM) quote  
This was also my first thought, but after analysing the sourcecode I can say that this is not the key to the problem.

The method getPreferredSize() returns the size of the whole panel, which contains both, the button and the textfield.

IP logged Status: logged off Profile Website 
adamfixflyer
Newbie
Image

JCalendar is great!
Posts: 2
RE: JDateChooser prefered size to small (November 17, 2009, 02:30:08 AM) quote  
maestroalubia is correct, the solution is to get the preferredSize from a formatted date, not the date pattern.

The following getPreferredSize() method is working for me. I use this class as my IDateEditor to benefit from the fix without changing the JCalendar source code:

quote:

public class BugFixJTextFieldDateEditor extends JTextFieldDateEditor {

private static final long serialVersionUID = 1L;

@Override
public Dimension getPreferredSize() {
//if we have a date and a formatter, use that to get the text field size
if(this.dateFormatter != null) {
//if no date, use today
Date d = this.date != null ? this.date : new Date();
String s = this.dateFormatter.format(d);
return new JTextField(s).getPreferredSize();
}
//else, use the date pattern string itself
if (datePattern != null) {
return new JTextField(datePattern).getPreferredSize();
}
//else, return default
return super.getPreferredSize();
}

}


Its not perfect, but it does work for me.

IP logged Status: logged off Profile 
rfl
Newbie
Image


Posts: 4
RE: JDateChooser prefered size to small (January 5, 2010, 07:13:48 PM) quote  
hi
bellow is a running example of the bug and a try to use your call, ut with no results...

what am i doing wrong?

import java.awt.*;
import java.util.Date;
import javax.swing.*;
import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JTextFieldDateEditor;

public class Bug extends JPanel {
private JDateChooser calendarioRadar;
private JTextFieldDateEditor textFieldDateEditorRadar;
private JButton botaoCalendarioRadar;
private JPanel panel;

public Bug() {
super();
criaPanel();
criaEMostraGui();
}

private void criaPanel() {
calendarioRadar = new JDateChooser(new Date(), "dd/MM/yyyy");

BugFixJTextFieldDateEditor fix = new BugFixJTextFieldDateEditor();

// Dimension d = fix.getPreferredSize();
// System.out.println("d " + d);//65, 20

textFieldDateEditorRadar = (JTextFieldDateEditor)(calendarioRadar.getComponent(1));
textFieldDateEditorRadar.setHorizontalAlignment(JTextField.RIGHT);
Dimension d = fix.getPreferredSize();
textFieldDateEditorRadar.setPreferredSize(d);
textFieldDateEditorRadar.setPreferredSize(new Dimension(100, 25));
add(textFieldDateEditorRadar);

botaoCalendarioRadar = calendarioRadar.getCalendarButton();
Dimension tamanhoBotao = new Dimension(25, 25);
botaoCalendarioRadar.setPreferredSize(tamanhoBotao);

add(botaoCalendarioRadar);
}

private void criaEMostraGui() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(this);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main (String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Bug();
}
});
}
}
// fix found in the forums
class BugFixJTextFieldDateEditor extends JTextFieldDateEditor {

@Override
public Dimension getPreferredSize() {
//if we have a date and a formatter, use that to get the text field size
if(this.dateFormatter != null) {
//if no date, use today
Date d = this.date != null ? this.date : new Date();
String s = this.dateFormatter.format(d);
return new JTextField(s).getPreferredSize();
}
//else, use the date pattern string itself
if (datePattern != null) {
return new JTextField(datePattern).getPreferredSize();
}
//else, return default
// return super.getPreferredSize();
return new Dimension(100, 25);
}
}

IP logged Status: logged off Profile 
adamfixflyer
Newbie
Image

JCalendar is great!
Posts: 2
RE: JDateChooser prefered size to small (January 5, 2010, 08:10:11 PM) quote  
You're not giving the JDateChooser a reference to the fixed code. This works for me:

quote:

private void criaPanel() {
BugFixJTextFieldDateEditor fix = new BugFixJTextFieldDateEditor();
calendarioRadar = new JDateChooser(new Date(), "dd/MM/yyyy", fix);
...
}


IP logged Status: logged off Profile 
rfl
Newbie
Image


Posts: 4
RE: JDateChooser prefered size to small (January 5, 2010, 08:52:31 PM) quote  
that worked fine, thanks a lot!

IP logged Status: logged off Profile 
xmedeko
Newbie
Image

JCalendar is great!
Posts: 2
RE: JDateChooser prefered size to small (January 12, 2010, 09:25:25 AM) quote  
Hi guys,
I have the same problem with getPreferredSize method. I think it is a wrong approach to override getPreferredSize() in JTextFieldDateEditor. Because a potential user of the JTextFieldDateEditor cannot change the preferred size without overriding the class.

Instead, I would call setColumns() in setDateFormatString(), e.g. setColumns(datePattern.length()). I think that also the setColumns() is better for JTextField, then setPreferredSize().


IP logged Status: logged off Profile 
New Post Post Reply

Software PBLang 4.67.16.a © 2002-2006 by Martin Senftleben & the PBLang-Team
Image