Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Using a JComboBox to have a user select an object

  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Using a JComboBox to have a user select an object

    I am making a booking system for a theatre (Coursework, not real life).

    I have got pretty much the whole program functional now (i.e. booking the seat, interface) and am now adding functionality to have the seats unbooked.

    I want the user to be able to select the seat they want unbooked from a list of all seats and then click a "unbook" button to unbook the seat.


    Pseudo code for the button:
    Java Code:
    onButtonClick{
    Seat selectedSeat = getComboBoxValue();
    selectedSeat.unbook();
    displaySuccessMessage();
    }

    Obviously, I cannot just use a JComboBox to display my objects (Called Seat) as they are not text. However they do have a String variable in them called seatName.

    Is it somehow possible to have the JComboBox list the values of seatName but have it actually a selection of the object itself so that I can use the unbook() method within the object?



    I haven't done any work with JComboBoxes yet so am not really familiar with them.
    Sorry for the awful explanation.


    My only other idea is to have the JComboBox just have an array of all the seat names and then use a hashmap to link each of these names to the seat objects. Although this, although I would know how to do it, would involve duplicating data and I don't think this would be the best practice to get into the habit of.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Using a JComboBox to have a user select an object

    Quote Originally Posted by kkid View Post
    Obviously, I cannot just use a JComboBox to display my objects (Called Seat) as they are not text.
    Not true, not true at all. You can fill the JComboBox's model with Seat objects and give the Seat object a decent toString() method that is displayed by default if shown in a JComboBox, or even better and more robust is to create a custom renderer for your JComboBox that will show the Seat data in a clean way. I suggest you not create new unnecessary variables for this.
    DarrylBurke likes this.

  3. #3
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Using a JComboBox to have a user select an object

    The toString() method would be as simple as this, right?

    Inside the Seat class:
    Java Code:
    public String toString(){
    return seatName;
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Using a JComboBox to have a user select an object

    Quote Originally Posted by kkid View Post
    The toString() method would be as simple as this, right?

    Inside the Seat class:
    Java Code:
    public String toString(){
    return seatName;
    }

    What happens when you try this.

  5. #5
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Using a JComboBox to have a user select an object

    What happens when I use that toString() method in general?

    it returns the seatName, as expected.


    (Well Netbeans told be to add @override).


    How to I put this into the combobox then?
    And why is it better to use a toString() method rather than to just have SEATOBJECT.seatName?

  6. #6
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Using a JComboBox to have a user select an object

    To be honest, I don;t have a clue what i am doing with these JComboBoxes but by using this line of code and adding it to my interface, I get a result. I have no idea why it works though.

    Java Code:
    JComboBox seatCombobox = new JComboBox(A);
    Where A is a Seat[]

    This is what I get http://pasteboard.co/1b8tCP9f.png
    This is what I want but I do not understand where it is getting the names from, I haven't specified these names anywhere that is related to the JComboBox. it even corrects the A[0] actually being A1. How is it doing this?


    Also, how to I add my other rows (B, C, D, E...)?
    I tried .addItem(B) as suggested somewhere I read but that doesn't work.

  7. #7
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Using a JComboBox to have a user select an object

    Why can't I do this to add all of my Seats?

    Each A, B, C... is a Seat[]

    Java Code:
    JComboBox seatCombobox = new JComboBox(A);
    seatCombobox.addItem(B);
    seatCombobox.addItem(C);
    seatCombobox.addItem(D);
    ...

    I get this after the A list if I try that (Can;t post a screenshot of the actual list as for some reason it won't take if the list is active.
    Pasteboard — Uploaded Image

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Using a JComboBox to have a user select an object

    Have you gone through the combo box tutorial? That's where I'd start. If you need to update the contents of the combo box during program run, then you'll need to get its model and call methods on the model, usually a DefaultComboBoxModel, if memory serves me right.

    Your image with the cut-off of text suggests that you may not be using layout managers correctly.

  9. #9
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Using a JComboBox to have a user select an object

    Quote Originally Posted by Fubarable View Post
    Have you gone through the combo box tutorial? That's where I'd start. If you need to update the contents of the combo box during program run, then you'll need to get its model and call methods on the model, usually a DefaultComboBoxModel, if memory serves me right.

    Your image with the cut-off of text suggests that you may not be using layout managers correctly.
    I haven't finished going through it yet, I'll finish it later. I'll have a look into the model too.

    The layout is fine, it is only temporary. I know what to do to fix it, it is only like that for convenience at the moment.

  10. #10
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Using a JComboBox to have a user select an object

    I've read through it but still do not have a clue how I am meant to add all of my arrays into the combo box. Can I have a little more help please?

  11. #11
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Using a JComboBox to have a user select an object

    I've even tried "Bad" solutions such as using Vecotrs which I know are out-dated.

    Java Code:
    Vector comboBoxItems=new Vector();
                        comboBoxItems.add(A);
                        comboBoxItems.add(B);
                        comboBoxItems.add(C);
                        comboBoxItems.add(D);
                        comboBoxItems.add(E);
                        final DefaultComboBoxModel model = new DefaultComboBoxModel(comboBoxItems);
                        JComboBox seatCombobox = new JComboBox(model);

    Nothing seems to be working

  12. #12
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Using a JComboBox to have a user select an object

    Another attempt I have tried:

    Java Code:
    DefaultComboBoxModel model = new DefaultComboBoxModel();
                    model.addElement(A);
                    model.addElement(B);
                    model.addElement(C);
                    model.addElement(D);
                    model.addElement(E);
                    model.addElement(F);
                    model.addElement(G);
                    model.addElement(H);
                    model.addElement(J);
                    model.addElement(K);
                    model.addElement(L);
                    JComboBox seatCombobox = new JComboBox();
                    seatCombobox.setModel(model);
                    unbookSeatWindow.buttonsPanel.add(seatCombobox);

  13. #13
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Using a JComboBox to have a user select an object

    I asked my problem over at StackOverflow too after using the DefaultComboBoxModel method that you informed me of. In the comments section of the main response, you can see that it still doesn't appear to be working even though everything seems to be right. Any ideas?

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Using a JComboBox to have a user select an object

    Quote Originally Posted by kkid View Post
    I asked my problem over at StackOverflow too after using the DefaultComboBoxModel method that you informed me of. In the comments section of the main response, you can see ...
    No, we can't, because you forgot to post the link.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  15. #15
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Using a JComboBox to have a user select an object

    Quote Originally Posted by DarrylBurke View Post
    No, we can't, because you forgot to post the link.

    db
    Oh dear, I always do that.
    Sorry, here it is: java - How can I add multiple arrays to a JComboBox? - Stack Overflow

Similar Threads

  1. JTable: row select on the basis of an object
    By Heatryn in forum AWT / Swing
    Replies: 1
    Last Post: 07-11-2012, 11:57 PM
  2. Jcombobox get text when user type something
    By sh4rif in forum New To Java
    Replies: 2
    Last Post: 06-07-2012, 02:39 AM
  3. Using GUI to select a shape object from and array
    By Davededo in forum New To Java
    Replies: 3
    Last Post: 10-17-2010, 08:03 AM
  4. Replies: 2
    Last Post: 12-10-2009, 02:58 PM
  5. how to select Shape object after rotation.
    By dorairaj in forum AWT / Swing
    Replies: 5
    Last Post: 08-14-2009, 04:28 PM

Posting Permissions

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