paradox Newbie

JCalendar is great! Posts: 2
|
JDateChooser prefered size to small (January 31, 2007, 03:22:32 AM) |
|
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?
|
Status: logged off |
|
| Order of replies: first reply last :: first reply first |
maestroalubia Newbie

Posts: 3
|
RE: JDateChooser prefered size to small (July 27, 2007, 05:42:57 PM) |
|
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.
|
Status: logged off |
|
maestroalubia Newbie

Posts: 3
|
RE: JDateChooser prefered size to small (July 27, 2007, 05:55:31 PM) |
|
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..
|
Status: logged off |
|
BTracy Newbie

Posts: 4
|
RE: JDateChooser prefered size to small (August 7, 2007, 05:51:51 PM) |
|
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.
|
Status: logged off |
|
maestroalubia Newbie

Posts: 3
|
RE: JDateChooser prefered size to small (August 7, 2007, 06:04:27 PM) |
|
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.
|
Status: logged off |
|
adamfixflyer Newbie

JCalendar is great! Posts: 2
|
RE: JDateChooser prefered size to small (November 17, 2009, 02:30:08 AM) |
|
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.
|
Status: logged off |
|
rfl Newbie

Posts: 4
|
RE: JDateChooser prefered size to small (January 5, 2010, 07:13:48 PM) |
|
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); } }
|
Status: logged off |
|
adamfixflyer Newbie

JCalendar is great! Posts: 2
|
RE: JDateChooser prefered size to small (January 5, 2010, 08:10:11 PM) |
|
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); ... }
|
|
Status: logged off |
|
rfl Newbie

Posts: 4
|
RE: JDateChooser prefered size to small (January 5, 2010, 08:52:31 PM) |
|
that worked fine, thanks a lot!
|
Status: logged off |
|
xmedeko Newbie

JCalendar is great! Posts: 2
|
RE: JDateChooser prefered size to small (January 12, 2010, 09:25:25 AM) |
|
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().
|
Status: logged off |
|