Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By camickr

Thread: Objects and jCombobox

  1. #1
    norbertK is offline Member
    Join Date
    Jul 2011
    Posts
    1
    Rep Power
    0

    Default Objects and jCombobox

    I am trying to create a class that populates a combobox with an object for instance country with fields like (name, city, area, population etc). I want the user to use a data entry form to enter the details but ones they have been saved, he can load details of different countries using the combo box navigation. My problem is that I am not able to listen to the combobox changes so that I can repaint the screeen.


    How should my action listener be written so that I can access the attributes of the object country by selecting the name. The graphic class is different from the logical one.

    Please help!!! I am new to java

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,463
    Rep Power
    25

    Default

    access the attributes of the object country by selecting the name
    Not sure about the rest of your problem, but this sounds like the place to use a Map. key = name, value = country object

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,256
    Rep Power
    19

    Default

    You don't need a Map.

    1. Populate the combo's data model with Country objects.
    2. Either override toString in the Country class or set a custom ListCellRenderer, probably extending DefaultListCellRenderer, in which you cast the value parameter to Country and extract the information for setText.

    As far as 'listening' is concerned, read the API for JComboBox and follow the link to the tutorial on How to Use Combo Boxes where you will find the information you seek.

    db

  4. #4
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    try using actionListener and JPanel for painting some image and every time combobox is updated repaint the image on JPanel to validate that use "validate()" or "SwingUtilities.updateComponentTreeUI(this)"

  5. #5
    camickr is online now Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    or "SwingUtilities.updateComponentTreeUI(this)"
    No, you would not use that method. You did NOT change the LAF.
    Fubarable likes this.

  6. #6
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    No, you would not use that method. You did NOT change the LAF.
    base in my experience SwingUtilities.updatecomponentTreeUI(this); also works because i use it in my project like removing and adding my JPanel. (that include image)

  7. #7
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by Jhovarie View Post
    base in my experience SwingUtilities.updatecomponentTreeUI(this); also works because i use it in my project like removing and adding my JPanel. (that include image)
    It may work, but it is definitely not the recommended way to validate a container. It is intended for LAF manipulations, not simple validation. To validate a container, call the validate() method. That is what it is there for.

  8. #8
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by DarrylBurke View Post
    2. Either override toString in the Country class or set a custom ListCellRenderer, probably extending DefaultListCellRenderer, in which you cast the value parameter to Country and extract the information for setText.
    Usually, overriding toString() is the simplest and most polymorphic way - if an object is going to be displayed in a combo box by, for example its name field, it's quite likely to be similarly displayed elsewhere in the GUI - e.g. perhaps in a Command Undo list (say, "Undo Add: France"). Knowing you can just call toString() on it wherever needed makes life a lot simpler and more polymorphic.

Similar Threads

  1. JComboBox
    By Java_25 in forum New To Java
    Replies: 10
    Last Post: 04-20-2011, 06:39 PM
  2. Replies: 2
    Last Post: 12-10-2009, 01:58 PM
  3. JComboBox
    By pinks_70986 in forum New To Java
    Replies: 2
    Last Post: 02-19-2009, 05:26 PM
  4. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  5. jcombobox
    By Freddie in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2007, 12:48 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
  •