JComboBox problem - only one element

Discussion in 'Java' started by Genevieve, Jul 5, 2005.

  1. Genevieve

    Genevieve Guest

    Hello !

    I'm having a little problem with my JComboBox.

    I'm supposed to have 10 elements (Size 1, Size 2, etc.), but only the
    first one shows up and I don't have an arrow to be able to see the
    other ones. I know the other ones are there because I printed the
    getItemCount and I got 10...

    Anyone knows what to do ?

    Thanks in advance :)
    Gen
    Genevieve, Jul 5, 2005
    #1
    1. Advertising

  2. Genevieve

    Genevieve Guest

    I should probably tell you that my JComboBox is in a JMenu...
    Genevieve, Jul 5, 2005
    #2
    1. Advertising

  3. Genevieve wrote:
    > Hello !
    >
    > I'm having a little problem with my JComboBox.
    >
    > I'm supposed to have 10 elements (Size 1, Size 2, etc.), but only the
    > first one shows up and I don't have an arrow to be able to see the
    > other ones. I know the other ones are there because I printed the
    > getItemCount and I got 10...
    >
    > Anyone knows what to do ?


    Yes, here is exactly what to do. Write the smallest, self-contained
    program you possibly can that still exhibits the problem. Then post the
    code here. Then sit back and watch people solve your problem.

    HTH,
    Ray

    --
    XML is the programmer's duct tape.
    Raymond DeCampo, Jul 5, 2005
    #3
  4. Genevieve

    Genevieve Guest

    Hey I've been working on this for two days now...I don't think it's
    lazy to ask for help.

    anyways...

    /**********************************************/
    JComboBox comboBox = new JComboBox();
    comboBox.setMinimumSize(new Dimension(200, 130));

    for( int i = 1; i < 11; ++i ) {
    comboBox.addItem( "Size " + i );
    }
    comboBox.addItemListener( this );
    add(comboBox); //adds it to the menu
    /**********************************************/

    I read somwhere that maybe I need to put it in a JFrame or something
    like that, is it a possibility?

    Thanks again


    Raymond DeCampo wrote:
    > Genevieve wrote:
    > > Hello !
    > >
    > > I'm having a little problem with my JComboBox.
    > >
    > > I'm supposed to have 10 elements (Size 1, Size 2, etc.), but only the
    > > first one shows up and I don't have an arrow to be able to see the
    > > other ones. I know the other ones are there because I printed the
    > > getItemCount and I got 10...
    > >
    > > Anyone knows what to do ?

    >
    > Yes, here is exactly what to do. Write the smallest, self-contained
    > program you possibly can that still exhibits the problem. Then post the
    > code here. Then sit back and watch people solve your problem.
    >
    > HTH,
    > Ray
    >
    > --
    > XML is the programmer's duct tape.
    Genevieve, Jul 5, 2005
    #4
  5. Genevieve

    Daniel Dyer Guest

    On Tue, 05 Jul 2005 20:02:08 +0100, Genevieve <>
    wrote:

    > Hey I've been working on this for two days now...I don't think it's
    > lazy to ask for help.
    >
    > anyways...
    >
    > /**********************************************/
    > JComboBox comboBox = new JComboBox();
    > comboBox.setMinimumSize(new Dimension(200, 130));
    >
    > for( int i = 1; i < 11; ++i ) {
    > comboBox.addItem( "Size " + i );
    > }
    > comboBox.addItemListener( this );
    > add(comboBox); //adds it to the menu
    > /**********************************************/
    >
    > I read somwhere that maybe I need to put it in a JFrame or something
    > like that, is it a possibility?
    >


    Ray wasn't calling you lazy (at least that's not how I interpretted it),
    he meant if you post something that people can cut, paste, compile and run
    then people will take a look at it and probably find the problem. Even
    with the code you just posted there are still lots of questions that would
    be answered by posting a compilable example. You mention JFrame as if you
    aren't using one, does this mean it's an applet (since menu bars can only
    be added to JFrame and JApplet), or is it a JPopupMenu?

    Are you sure that adding a combo-box to a menu is a good idea? I've never
    seen an app with a combo-box menu item. It would appear to violate the
    "law of least astonishment" (or whatever it's called).

    Dan.


    --
    Daniel Dyer
    http://www.dandyer.co.uk
    Daniel Dyer, Jul 5, 2005
    #5
  6. Genevieve

    Genevieve Guest

    Oops...sorry Ray !! :p

    We are working with a JMenu that appears when we right-click on an
    specific element that we have in our program. My boss wants to be able
    to change the element's size by adding a comboBox in that menu. Before,
    I had done a menuItem that looked like a list...

    Thanks again...
    Genevieve, Jul 5, 2005
    #6
  7. Genevieve

    Daniel Dyer Guest

    On Tue, 05 Jul 2005 20:25:51 +0100, Genevieve <>
    wrote:

    > Oops...sorry Ray !! :p
    >
    > We are working with a JMenu that appears when we right-click on an
    > specific element that we have in our program. My boss wants to be able
    > to change the element's size by adding a comboBox in that menu. Before,
    > I had done a menuItem that looked like a list...
    >
    > Thanks again...


    OK, I just wrote this, which is what I think you are trying to achieve.
    It "works" for me in as much as the combo box is displayed in the menu and
    I can click the arrow to select an item, but the behaviour is still a bit
    odd because the menu disappears when you click on the combo-box and the
    combo-box pop-up remains visible.


    import javax.swing.*;

    public class MenuExample
    {
    public static void main(String[] args)
    {
    JFrame frame = new JFrame("Combo Menu Item");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Menu");
    JComboBox combo = new JComboBox();
    for (int i = 0; i < 5; i++)
    {
    combo.addItem("Item " + i);
    }
    menu.add(combo);
    menuBar.add(menu);
    frame.setJMenuBar(menuBar);
    frame.setSize(200, 200);
    frame.validate();
    frame.setVisible(true);
    }
    }


    Dan.

    --
    Daniel Dyer
    http://www.dandyer.co.uk
    Daniel Dyer, Jul 5, 2005
    #7
  8. Please do not top-post when the response has already been bottom-posted.

    Genevieve wrote:
    > Hey I've been working on this for two days now...I don't think it's
    > lazy to ask for help.


    I did not intend to imply you were lazy. I was genuinely trying to help.

    >
    > anyways...
    >
    > /**********************************************/
    > JComboBox comboBox = new JComboBox();
    > comboBox.setMinimumSize(new Dimension(200, 130));
    >
    > for( int i = 1; i < 11; ++i ) {
    > comboBox.addItem( "Size " + i );
    > }
    > comboBox.addItemListener( this );
    > add(comboBox); //adds it to the menu
    > /**********************************************/
    >
    > I read somwhere that maybe I need to put it in a JFrame or something
    > like that, is it a possibility?
    >


    By complete, self-contained program, I mean one that compiles and runs
    and shows the problem. The above does not do that. You need to make it
    easier for people to help you. If you do as I say, people will
    cut-n-paste the code and try to run it. If they encounter too many
    problems trying to do that, they will give up.

    It can also be a good exercise that reveals the problem without
    additional help.

    HTH,
    Ray

    --
    XML is the programmer's duct tape.
    Raymond DeCampo, Jul 5, 2005
    #8
  9. Genevieve

    Genevieve Guest

    You're right, I'm just new to this :)
    Genevieve, Jul 6, 2005
    #9
  10. Genevieve

    Genevieve Guest

    How weird...I have the same mistake...only one element and no arrow to
    this the other ones...
    Genevieve, Jul 6, 2005
    #10
  11. Genevieve wrote:
    > I should probably tell you that my JComboBox is in a JMenu...
    >

    It occurs to me that the standard way of handling this type of thing is
    to use a set of radio or toggle buttons in a submenu. That would be
    something more familiar from a UI point of view.

    Ray

    --
    XML is the programmer's duct tape.
    Raymond DeCampo, Jul 6, 2005
    #11
  12. Genevieve

    Genevieve Guest

    Yeah I saw that, I'll see if my boss wants to try that...
    Genevieve, Jul 6, 2005
    #12
  13. Genevieve wrote:
    > Yeah I saw that, I'll see if my boss wants to try that...
    >

    Be more competent than your boss. ;-)
    Just show him the CheckBox-/RadioButton-MenuItems of some prominent
    examples (like in the View-menu of Internet Explorer and Mozilla) and
    say: "That's the way GUI's look today, and I'll do it that way, too."

    --
    "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
    Thomas Fritsch, Jul 6, 2005
    #13
  14. Genevieve

    Genevieve Guest

    hahaha not a bad idea ;)
    Genevieve, Jul 6, 2005
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Y.S.
    Replies:
    3
    Views:
    1,010
    strajan
    Sep 17, 2003
  2. HANM
    Replies:
    2
    Views:
    708
    Joseph Kesselman
    Jan 29, 2008
  3. Mike N.
    Replies:
    3
    Views:
    567
    Joseph Kesselman
    Mar 17, 2008
  4. Chuckk Hubbard
    Replies:
    1
    Views:
    311
    Peter Otten
    Jun 9, 2008
  5. OccasionalFlyer
    Replies:
    6
    Views:
    246
    Garrett Smith
    Jul 29, 2009
Loading...

Share This Page