0

In a JDateChooser, we have added a addPropertyChangeListener that detects if a date is chosen. If it is chosen, we want to open a JCombobox. The (date) string is detected when we select, but we can't open the JComboBox.

Here is the code:

dateChooserCal.getDateEditor().addPropertyChangeListener(new PropertyChangeListener() {
    public void propertyChange(PropertyChangeEvent evt) 
    {
        date = dateChooserCal.getDate();

        if ("date".equals(evt.getPropertyName())) 
        {   
            dates = evt.getNewValue();
            dateString = String.format("%1$td-%1$tm-%1$tY", date);  
            if (dateString != null) 
            {
                System.out.print(dateString);
                chooseTimeBox = new JComboBox(controllerApp.getTime());
                chooseTimeBox.setBounds(215, 261, 282, 22);
                add(chooseTimeBox);
                chooseTimeBox.setVisible(true);
            }

        }

    }       

});
mKorbel
  • 109,525
  • 20
  • 134
  • 319
tinaw25
  • 183
  • 2
  • 4
  • 14
  • It looks like you're using a null layout. While to many new Swing programmers null layout seems like the easiest way to create good looking complex GUI's, once you become experienced with Swing you'll find that this is a fallacy, that null layouts box you in, that GUI's created with this are very inflexible, difficult to upgrade, debug and maintain, that while the GUI's might look good on a single platform using a single screen resolution, they look bad on all others, and that using layout managers wisely is a much better option. – Hovercraft Full Of Eels Dec 02 '14 at 12:11

1 Answers1

3

Well the fact that combo box isn't contained within any displayable element is probably the primary cause of your issue...

// You create a new instance
chooseTimeBox = new JComboBox(controllerApp.getTime());
// You  position and size, more on this later...
chooseTimeBox.setBounds(215, 261, 282, 22);
// You make it visible...but it's visible by default...
chooseTimeBox.setVisible(true);
// But you never add it to anything...

Because you seem to be using a instance field, I might guess that you have already created a previous instance and have already added it, in that case, you should be updating that instances model...

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

Have a look at Why is it frowned upon to use a null layout in SWING? and Laying Out Components Within a Container for more details...

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • We have only declared the JComboBox chooseTimeBox as a local variable. We have not use it before. If we say add(chooseTimeBox); It still not showing. – tinaw25 Dec 02 '14 at 10:27
  • 1
    Normally I would suggest invalidating the container and repainting it, but you seem to be doing away with the layout manager so, that won't work...yet another reason to avoid `null` layouts. I'm making a lot of guesses as you've not provided a runnable example to your problem – MadProgrammer Dec 02 '14 at 10:32
  • We solved the problem, it was working but the JCombox was invisible on a Mac. Thanks for your help – tinaw25 Dec 02 '14 at 10:50