Skip to main content

JXDatePicker resize by itself

9 replies [Last post]
cmadsen_dk
Offline
Joined: 2005-09-29
Points: 0

Using swingx 1.6.5.1 my JXDatePicker's editor field(s) have started to auto resize when panel loses focus it sounds a lot like this issue http://java.net/jira/browse/SWINGX-1292 ?

Tried:

getEditor().setColumns(7);

and

getEditor().setPreferredSize(getEditor().getMinimumSize());

with no luck.

Any workarounds possible?

BR
Carsten

AttachmentSize
img.png13.73 KB
img1.png12.48 KB

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kleopatra
Offline
Joined: 2003-06-11
Points: 0

hmm .. not aware of any recent change - what's the last version that behaved as you expected?

Thanks
Jeanette

cmadsen_dk
Offline
Joined: 2005-09-29
Points: 0

I'm porting an application from Spring Rich Client (swingx 0.9.3) to Valkyrie RCP which uses swingx 1.6.x so it may not necessarily be a recent change :-)

I have tried with swingx 1.6/1.6.1 both have the same behaviour. If I use swingx 0.9.3 there is no problem with the date pickers but it breaks a lot of other swingx stuff.

BR
Carsten

kleopatra
Offline
Joined: 2003-06-11
Points: 0

0.9.3 - that's not even the final for jdk5. There have been tons of changes, including a complete overhaul of the calendar-related components ... happy updating ;-)

As I mentioned in the issue you linked: the behaviour is the same as with a plain JFormattedTextField, so not seeing any force to do anything. While you are at changing stuff, I would recommend changing to a suitable LayoutManager as well (f.i. MigLayout, my personal favourite currently) - that'll allow you fine-grained control about sizes, locations, alignments ..

Cheers
Jeanette

cmadsen_dk
Offline
Joined: 2005-09-29
Points: 0

Ok, using MiG layout and JXDatePicker with getEditor().setColumns I can stop the resizing but my application is using UIManager.setLookAndFeel & updateComponentTreeUI and this apparently makes the JXDatePicker loose it's columns setting. Maybe it's got something to do with these issues?

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4144505
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4188128

I have tried listening for LAF changes and then doing a setColumns but it does not work. After the first LAF change the date pickers will auto resize when text is entered and the containing panel is repainted.

Here is some code that reproduces the problem. Enter some text in the date picker and then do a resize to reproduce. If the call to updateComponentTreeUI is removed things work as expected.

public class JXDatePickerResizeTest {

static class LafChangeListener implements PropertyChangeListener,
SkinChangeListener {

private JXDatePicker datePicker;

LafChangeListener(JXDatePicker datePicker) {
this.datePicker = datePicker;
UIManager.addPropertyChangeListener(this);
SubstanceLookAndFeel.registerSkinChangeListener(this);
}

@Override
public void propertyChange(PropertyChangeEvent event) {
if ("lookAndFeel".equals(event.getPropertyName())) {
skinChanged();
}
}

@Override
public void skinChanged() {
System.err.println("SkinChanged " + datePicker);
datePicker.getEditor().setColumns(10);
}

}

static class DockPanel extends JPanel {

public DockPanel() {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd/MM/yyyy HH:mm:ss");
DefaultFormatterFactory factory = new DefaultFormatterFactory(
new DatePickerFormatter(new DateFormat[] { dateFormat }));
setLayout(new MigLayout("fill, width 300"));
JXDatePicker picker = new JXDatePicker();
new LafChangeListener(picker);
picker.getEditor().setFormatterFactory(factory);
add(picker, "wrap");
picker.getEditor().setColumns(10);
JFormattedTextField textField = new JFormattedTextField();
textField.setFormatterFactory(factory);
textField.setText("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
textField.setColumns(10);
add(textField, "wrap");
setPreferredSize(new Dimension(200, 400));
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Test JXDatePicker resize");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DockPanel panel = new DockPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
try {
//UIManager
// .setLookAndFeel(new SubstanceCremeCoffeeLookAndFeel());
SwingUtilities.updateComponentTreeUI(frame);
//frame.pack();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}

}
});
}
}

Thanks
Carsten

kleopatra
Offline
Joined: 2003-06-11
Points: 0

OT, but: do you also see the first bug you referenced, that is

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4144505

truncated? It ends with the header "Solution" plus two lines

" Here's an example of a utility class that addresses this
problem. The BusyPanel is simple container for one "

and that's it. Possibly important info missing (as are the comments I **bet** had been there...)

Cheers
Jeanette

kleopatra
Offline
Joined: 2003-06-11
Points: 0

hmm ... you are right: the picker's columns property is lost while a bare formattedField's columns are kept. Would you please file an issue in the swingx issue tracker?

Actually, I don't see any growing in your example (not using substance, though, only dynamically toggling LAF in a swingx viaual test case), not even when using flowLayout - any ideas?

As a quick work-around you might try to wrap the setColumns into an invokeLater (in your LAF listener).

Thanks
Jeanette

cmadsen_dk
Offline
Joined: 2005-09-29
Points: 0

I have create this issue http://java.net/jira/browse/SWINGX-1551 in Jira.
BR
Carsten

kleopatra
Offline
Joined: 2003-06-11
Points: 0

thanks :-)

The technical reason is that the editor is under the control of the ui, that is recreated and set in its installUI (if the current editor is null or UIResource). It doesn't configure the textField except for the format, so at the end of the day, the column setting is lost.

So another workaround might to set a custom editor which is not of type UIResource - the ui installed one does some tricksery to return a reasonable pref if empty and columns not set ...

A real solution might (just musing, input welcome!):

Make the columns a property of the datePicker: that way it would we stored in the picker and propagate to the editor as needed. Slightly worried that doing so might open a flood gate: once started we might end up with tons of propagated properties. Hmm, need to check others, like font etc: they are passed through to the editor without being kept in the picker, might be lost on updating the ui.

Alternative, make the ui keep track of the columns if set in the editor (could store as a client property in the picker on uninstall). Feels hacky..

There's an old ui-property with key JXDatePicker.numCols which isn't (never was?) actually used - using it, would allow to some per-application setting

Cheers
Jeanette

cmadsen_dk
Offline
Joined: 2005-09-29
Points: 0

Doing the setColumns in invokerLater works, thanks!

In the test code I have to enter a long string in the date picker and then manually resize the window to provoke the resizing.

The problem is releated to updateComponentTreeUI. I do not have to switch LAF, just call updateComponentTreeUI and then ask the date picker for its column size will do it.