This discussion is archived
7 Replies Latest reply: Nov 15, 2012 6:32 AM by 901855 RSS

setPrototypeDisplayValue with generics

901855 Newbie
Currently Being Moderated
I am just converting an old swing app to use features of Java 7. I changed all uses of JComboBox to specify the type of objects being listed. But I find that the setPrototypeDisplayValue() method wants an object of the same type to set the width of the combo box. Why is that? My combo boxes are dynamically built, so I may not have an object available at creation time. And in some cases the objects of the desired class are not just Strings, but have a toString() method to construct one. In order to set a prototype I think I have to construct a prototype that returns an appropriately long string.

Shouldn't we be allowed to just specify a String of the length we want? Or is there some other (better) way?

Thanks.
  • 1. Re: setPrototypeDisplayValue with generics
    Jörg Explorer
    Currently Being Moderated
    My combo boxes are dynamically built, so I may not have an object available at creation time.
    Then use the <Object> type, which covers all classes.
  • 2. Re: setPrototypeDisplayValue with generics
    Kayaman Guru
    Currently Being Moderated
    cwjrda wrote:
    My combo boxes are dynamically built, so I may not have an object available at creation time.
    Can't you set the prototype display value when you do have an object available?

    Shouldn't we be allowed to just specify a String of the length we want?
    You'll have to take that up with the designer of the class. But it's hardly a problem, since there's an obvious easy workaround.
  • 3. Re: setPrototypeDisplayValue with generics
    901855 Newbie
    Currently Being Moderated
    Jörg wrote:
    My combo boxes are dynamically built, so I may not have an object available at creation time.
    Then use the <Object> type, which covers all classes.
    If you mean combo.setPrototypeDisplayValue(new Object()); that doesn't compile and besides it wouldn't have a value or size.

    If you mean use JComboBox<Object> that just avoids the whole purpose and benefit of generics.

    Is there something else I am missing with your reply?
  • 4. Re: setPrototypeDisplayValue with generics
    901855 Newbie
    Currently Being Moderated
    Kayaman wrote:
    Shouldn't we be allowed to just specify a String of the length we want?
    You'll have to take that up with the designer of the class. But it's hardly a problem, since there's an obvious easy workaround.
    I am the class designer. And I did create a workaround by making a constructor that would cause the toString method to return a suitably long string value. What I am asking is: can't there just be a simple way of setting the preferred size, in characters, of a combo box? Something like JFormattedTextField's setColumns(). Perhaps I should take that up with Swing designers.

    Edited by: cwjrda on Nov 14, 2012 12:22 PM

    Edited by: cwjrda on Nov 14, 2012 12:23 PM
  • 5. Re: setPrototypeDisplayValue with generics
    Jörg Explorer
    Currently Being Moderated
    And in some cases the objects of the desired class are not just Strings.
    To me that means you want to put various types of objects into your combo. In that case I see no other way than to use JComboBox<Object>.

    What I am asking is: can't there just be a simple way of setting the preferred size, in characters, of a combo box?
    Not in characters, but you could calculate the width of your longest string with FontMetrics.stringWidth(...) and then set the combo's preferredSize accordingly.
  • 6. Re: setPrototypeDisplayValue with generics
    Kayaman Guru
    Currently Being Moderated
    cwjrda wrote:
    Kayaman wrote:
    Shouldn't we be allowed to just specify a String of the length we want?
    You'll have to take that up with the designer of the class. But it's hardly a problem, since there's an obvious easy workaround.
    I am the class designer.
    No, you did not create JComboBox.
    Perhaps I should take that up with Swing designers.
    That's what I meant. Although I don't recommend you actually waste your time doing that.
    And I did create a workaround by making a constructor that would cause the toString method to return a suitably long string value.
    Good. Problem solved.
    What I am asking is: can't there just be a simple way of setting the preferred size, in characters, of a combo box? Something like JFormattedTextField's setColumns().
    Of course there could be. I don't think the designers made it that way just to be evil.

    Also, the method is optional. The javadoc mentions possible performance issues that might result from not using it, but it's not mandatory.
  • 7. Re: setPrototypeDisplayValue with generics
    901855 Newbie
    Currently Being Moderated
    Thanks to those who helped. Here's what I have learned from this experience.

    setPrototypeDisplayValue is not looking for a value, but an item of the class specified for the JComboBox. Its renderer will use the item and the return from its toString method to determine the preferred size of the combo. Since a combo can handle Components that contain an icon instead of just a string, a string value is just not enough. OK. But a string will work if that's all you want in the combo. So one way I tried that worked was to add a constructor to the class that would create just enough of the object for it to return a string of the desired size. Another way, without changing the subject class, is as follows:

    combo.setPrototypeDisplayValue(new myClass() {
    public String toString() { return "XXXXXXXXXXX"; }
    });

    That subclasses the class and overrides the toString to give me a string value like I want.

    Another way would be to leave off the <E> in the declaration of the JComboBox and just let it handle any Object, the way things were back before generics. That still works (and its the way you would learn it if you follow the tutorials even today -- http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html).

    The reason it has been important for me to set the combo box size is that with dynamic filling of the combo's model, the size of the box would change. That does strange things to a GridBagLayout, causing the columns to change width. Then we encounter the strange "feature" of GridBagLayout of collapsing text fields in other parts of the grid when the column widths change. That could be the subject of another thread but I see that its been around for a long time.

    I am calling this question answered due to finding the above workarounds.

    Happy coding...

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points