File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Problem adding JComboBox to JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem adding JComboBox to JFrame" Watch "Problem adding JComboBox to JFrame" New topic
Author

Problem adding JComboBox to JFrame

Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 118
Hi! I want to add a JComboBox to JFrame. My code:



But i don't get the combobox anywhere on the jframe. Can anyone help? Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

You only add the combo box if you have at least one result. My guess is you have 0 results.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 118
Rob Prime wrote:You only add the combo box if you have at least one result. My guess is you have 0 results.


Hi! Thanks for your reply. I have single stepped through the code. It went inside the if statement and the do while loop.

I even created a dummy model.addElement(new Item(1, "2010")) and shifted the creation of the JCombobox, getContentPane, add and setContentPane outside the 'if' statement but I sill do not see it in the JFrame.

Am I missing something here? Please help, thanks.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2092
    
    7
The general code for adding components to a visible GUI should be:

panel.add(...);
panel.revalidate();
panel.repaint(); // sometimes needed

If that doesn't help then post your SSCCE showing the problem.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

To get better help, post a SSCCE.

The code you posted has a whole lot of executable statements outside of any constructor or method. If you don't PostRealCode how do you expect members here to see the problem?


luck, db
There are no new questions, but there may be new answers.
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 118
Hi! Thanks for your replies. I cannot access the SSCCE website.

I saw an example using FlowLayout and I have tried this:



The combobox appears but the layout of all the other textboxes, buttons, labels in the JFrame are all in one line. I think it is the display problem.

How should I make the combobox appear without using FlowLayout? Please help, thanks.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Alan Blass wrote:Hi! Thanks for your replies. I cannot access the SSCCE website.


Yep, it's down. This one might work though: Alternative SSCCE link
It is quite a useful construct since if you create a decent SSCCE, it would let us easily see your problem first hand, test your code, alter your code, and find a solution for your problem. My experience has been that posting a decent SSCCE is often your best way of getting a decent fast solution here.


I saw an example using FlowLayout and I have tried this:

....

How should I make the combobox appear without using FlowLayout? Please help, thanks.


Use the other layouts.

There are several to choose from, and often we use multiple different layouts by nesting JPanels, each using its own layout.

Much luck!
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2092
    
    7
Hi! Thanks for your replies. I cannot access the SSCCE website.


Whats wrong with the link I gave you?

Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 118
Hi Rob,

Web browser says "Operation timed out".

Pete,

Can I just add the JComboBox to the JFrame without changing any layouts?

Thanks
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Alan Blass wrote:Hi Rob,

Web browser says "Operation timed out".

You've been given three different links. It just isn't possible that all of them are down at the same time.

I just checked and two out of the three are working.
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 118
Ok. I have read SSCCE. Here is my code:



I still cannot get the combobox in the jframe. Please help. Thanks.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2092
    
    7
Ok. I have read SSCCE.


Well, you didn't real it very closely. That is NOT a SSCCE.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

Indeed. I don't have a working database connection.
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 118
Sorry, here's the revised code:



Please help, thanks.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2092
    
    7
Please explain to me how that code compiles:

a) when there are no import statements
b) when the initComponents() method is missing.

I don't have the time to debug your code. Twice we've asked for a proper SSCCE. Three strikes and you are out!
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 118
ok here's the code:


There are 2 buttons in the Jframe. Press the first button.
Sorry about the previous posts. Please help. Thanks
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2092
    
    7
The reason you can't solve this is because you still don't know how to simplify the problem and understand the basics of how Swing works.

Your question as about adding a combo box to a frame. Thats it.

I do not use and IDE so I do not have acess to your org.desktop.application package.

I do not have access to your ResourceMap.

Please explain how those classes/packages are relevant to your original question?

As a hint to your solution. Don't use the IDE to build the GUI. By default the IDE uses a GroupLayout to add components to the GUI. Therefore it is extremely difficult to add another component later, because you need to specify all the constraints when you attempt to add the component.

Instead design the GUI yourself using the appropriate combination of layout managers. Then you can generally just add the component to the panel and revalidate() the panel. I can't give any more specific advice than that.
 
Don't get me started about those stupid light bulbs.
 
subject: Problem adding JComboBox to JFrame
 
Similar Threads
JComboBox
Display value and Actual value in JComboBox
Change & sort the contents of JComboBox in Swing
any idea about JComboBox Modal? with JDBC
How to set ID and Value for a JCombobox