Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Custom JObject (Cross between JTextField and JComboBox)

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Custom JObject (Cross between JTextField and JComboBox)

    This is my first custom swing object, but I'm not sure where to start. I want to make something similar to a browser's address bar where you can type something. After any character is entered/removed, a drop-down box will appear below with search results from a database.

    I'm thinking of starting by extending JTextField. Then I would figure out how to add controls to catch events whenever the string is changed. Then I would add the drop-down items (though that sounds very though).

    Another option is extending JComboBox. This would be nice because I wouldn't have to worry about drop-down items, but then I'd need to figure out how to re-implement the first element.

    A third option is to use a JTextField, and draw a JList immediately below it with dynamic sizing.

    Do you have suggestions on which box to extend and how to re-implement it?


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,629
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: Custom JObject (Cross between JTextField and JComboBox)

    The first answer on this topic might provide you with some inspiration: swing - Create a autocompleting textbox in Java with a dropdown list - Stack Overflow
    Extending JTextField is probably what you want to do. If you do what the person in the other thread did (making the custom class for the drop down which renders alongside the text field), you can probably achieve what you want.

    I've made a few custom swing objects, but nothing like that. My custom swing objects have been replacements for the JTable and JList, where I render objects instead of values (such as adding an object to a table and having the row populate itself with values automatically retrieved from the object).
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,267
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Custom JObject (Cross between JTextField and JComboBox)

    A JComboBox can be set to editable, allowing a user to interact just like a JTextField (in fact, the underlying editing component is a JTextField). From there, you need to get the editing component and listen for changes to the underlying document (by say, adding a DocumentListener). When a change occurs, modify the items within the JComboBox accordingly. Its not necessarily trivial (I've done it myself and it needed some hacks to get around default behaviors) and you'd need to dig deep into the API to implement this behavior.

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Custom JObject (Cross between JTextField and JComboBox)

    Thanks for the responses. It looks I have two completely opposite paths to explorer. I'll try both out and see what happens.

Similar Threads

  1. Cross-posting
    By catisch in forum Totally Off Topic
    Replies: 2
    Last Post: October 18th, 2013, 08:25 AM
  2. Cross-Platform game
    By ggcon2 in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 02:33 PM
  3. (jComboBox, JTextField, jFileChooser) as table editor overrides the refrences!
    By campusGraphics in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 15th, 2012, 11:20 AM