JComboBox problem - only one element

G

Genevieve

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
 
R

Raymond DeCampo

Genevieve said:
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
 
G

Genevieve

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
 
D

Daniel Dyer

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.
 
G

Genevieve

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...
 
D

Daniel Dyer

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.
 
R

Raymond DeCampo

Please do not top-post when the response has already been bottom-posted.
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
 
G

Genevieve

How weird...I have the same mistake...only one element and no arrow to
this the other ones...
 
R

Raymond DeCampo

Genevieve said:
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
 
T

Thomas Fritsch

Genevieve said:
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."
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top