Results 1 to 14 of 14
  1. #1
    Cybex is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Question Set editable JComboBox's max text length

    Hi!

    I need to limit editable JComboBox's maximum allowed text length to 50 characters. The solutions I have found are those:
    1.
    Java Code:
    // JComboBox can be cast to a JTextField like this
    ((JTextField) this.getEditor().getEditorComponent()).setDocument(new
    JComboBoxLimitDoc(1));
    2. IBM variant: publib.boulder.ibm.com/iseries/v5r2/ic2928/info/rzahh/javadoc/com/ibm/as400/ui/framework/java/TextDocument.html

    As you can see, those are rather sloppy and they just don't seem like the best variants to me.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Using a custom Document is an older approach. The new approach would be to use a custom DocumentFilter. An example can be found in the Swing tutorial in the section on "Text Component Features" (I believe).

  3. #3
    Cybex is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Thumbs up

    Quote Originally Posted by camickr View Post
    Using a custom Document is an older approach. The new approach would be to use a custom DocumentFilter. An example can be found in the Swing tutorial in the section on "Text Component Features" (I believe).
    Thanks camickr!

    To my shame, I have not heard about DocumentFilter yet. Too bad Sun did not make limiting text length for the combos as easy as it is for the text fields :).

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Too bad Sun did not make limiting text length for the combos as easy as it is for the text fields
    You don't understand the purpose of each component.

    A JTextField is used to display text.

    A JComboBox is used to display a group of related Objects. Therefore a combo box users renderers and editors to achieve its goal. The most common usage of a combo box is to display text so the default editor happens to be a text field to allow you to edit the text. However, the combo box could be used to display "images", so you would need a custom renderer and a custom editor. In this case the editor might be a JButton which would display a JFileChooser when clicked. So the concept of limiting text makes no sense which is why the functionality is not part of the combo box, but part of the editor.
    Last edited by camickr; 11-14-2009 at 07:37 PM.

  5. #5
    Cybex is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    Yes don't understand the purpose of each component.

    A JTextField is used to display text.

    A JComboBox is used to display a group of related Objects. Therefore a combo box users renderers and editors to achieve its goal. The most common usage of a combo box is to display text so the default editor happens to be a text field to allow you to edit the text. However, the combo box could be used to display "images", so you would need a custom renderer and a custom editor. In this case the editor might be a JButton which would display a JFileChooser when clicked. So the concept of limiting text makes no sense which is why the functionality is not part of the combo box, but part of the editor.
    I put smile there for a reason :). Anyway, they could make text-only hybrid of text field and combo box. Something like a text filed with a drop-down string list.

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Anyway, they could make text-only hybrid of text field and combo box. Something like a text filed with a drop-down string list.
    Why? So now we have a text only JTable and a text only JList?

    The point of having customizable components is so that you can be creative and build apps that are only limited by your imagination, not by something that should only display text.

    If you think its that important, then you can create your hybrid to do that. Personally I think the 2 -3 lines of code to add the DocumentFilter to the editor is simple enough.

  7. #7
    Cybex is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    Why? So now we have a text only JTable and a text only JList?

    The point of having customizable components is so that you can be creative and build apps that are only limited by your imagination, not by something that should only display text.

    If you think its that important, then you can create your hybrid to do that. Personally I think the 2 -3 lines of code to add the DocumentFilter to the editor is simple enough.
    I meant it to be an addition not a replacement.
    Have you tried other languages and IDEs (like Delphi)?
    Trust me, they got much better and easier GUI creation tools.
    This is why Delphi is called RAD (Rapid Application Development) and why Java
    is not so good for desktop application development (not counting shitty default layouts and rather bad performance comparing to concurrents).

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    I meant it to be an addition not a replacement.
    The JDK is already too big. People don't even bother to read the API to find out out to do stuff which is why so many questions are posted on the forum. This question is a perfect example of that.

    The JDK should provide the basics. It can't handle every single piece of one off functionality that you might require.

    As I already stated the DocumentFilter design is incredibly flexible. You can use it anywhere a JTextField is used. You can use it in a JComboBox, JTable, JTree, whereever a text field is used as an editor. And its only a single line of code. You use the API to get the editor and then add the filter to its. That is far more powerful then any IDE with a few custom components that you can't customize.

  9. #9
    Cybex is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Question

    Quote Originally Posted by camickr View Post
    The JDK is already too big. People don't even bother to read the API to find out out to do stuff which is why so many questions are posted on the forum. This question is a perfect example of that.

    The JDK should provide the basics. It can't handle every single piece of one off functionality that you might require.

    As I already stated the DocumentFilter design is incredibly flexible. You can use it anywhere a JTextField is used. You can use it in a JComboBox, JTable, JTree, whereever a text field is used as an editor. And its only a single line of code. You use the API to get the editor and then add the filter to its. That is far more powerful then any IDE with a few custom components that you can't customize.
    Now I am using the following function:

    Java Code:
    	private void setMaxTextLength(JTextComponent comp, int length)
    		{
    		AbstractDocument doc;
    
    		Document styledDoc = comp.getDocument();
    		if (styledDoc instanceof AbstractDocument)
    			{
    			doc = (AbstractDocument) styledDoc;
    			doc.setDocumentFilter(new DocumentSizeFilter(length));
    			}
    		else
    			{
    			// ...
    			}
    		}
    But it only works with the text fields, text areas. I am having problem getting document from JComboBox (or from comb box editor in it).
    Can you point out how it can be done?

  10. #10
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    I am having problem getting document from JComboBox (or from comb box editor in it).
    Why? You know how to get the text field when you tried to use a custom Document. So if you pass in a JTextField to your method it should work. Since you haven't posted your SSCCE there is nothing else I can do.

  11. #11
    Cybex is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    Why? You know how to get the text field when you tried to use a custom Document. So if you pass in a JTextField to your method it should work. Since you haven't posted your SSCCE there is nothing else I can do.
    That's OK, I solved it now with casting.

  12. #12
    mqma is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default

    Sorry but how did you solve it with casting? Would you post the code?

  13. #13
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    See the line of code in the original question. Thats how you get the default editor and cast it to a text field.

  14. #14
    mqma is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Red face

    What does the JComboBoxLimitDoc class look line? How do you use the setMaxTextLength()?

Similar Threads

  1. Using Editable Pdf form fields in Application
    By jiten.mistry in forum Advanced Java
    Replies: 5
    Last Post: 09-21-2011, 08:37 PM
  2. Non-Editable Table Column
    By ld_pvl in forum AWT / Swing
    Replies: 6
    Last Post: 08-03-2009, 06:35 PM
  3. Partly editable SourceViewer
    By shravan28 in forum SWT / JFace
    Replies: 7
    Last Post: 05-28-2009, 11:29 AM
  4. Creating a non-editable combo box
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:45 PM
  5. Jtree - making parts editable
    By kmarie in forum AWT / Swing
    Replies: 1
    Last Post: 07-27-2007, 02:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •