Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem adding JComboBox to JFrame

 
Alan Blass
Ranch Hand
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You only add the combo box if you have at least one result. My guess is you have 0 results.
 
Alan Blass
Ranch Hand
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2475
8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Alan Blass
Ranch Hand
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2475
8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Thanks for your replies. I cannot access the SSCCE website.


Whats wrong with the link I gave you?

 
Alan Blass
Ranch Hand
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2475
8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I have read SSCCE.


Well, you didn't real it very closely. That is NOT a SSCCE.
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed. I don't have a working database connection.
 
Alan Blass
Ranch Hand
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, here's the revised code:



Please help, thanks.
 
Rob Camick
Ranch Hand
Posts: 2475
8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2475
8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic