Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Problem adding JComboBox to JFrame

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22586
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3253
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3253
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3253
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22586
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed. I don't have a working database connection.
 
Alan Blass
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, here's the revised code:



Please help, thanks.
 
Rob Camick
Rancher
Posts: 3253
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3253
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic