Forums Register Login

Problem adding JComboBox to JFrame

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
You only add the combo box if you have at least one result. My guess is you have 0 results.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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!
+Pie Number of slices to send: Send
 

Hi! Thanks for your replies. I cannot access the SSCCE website.



Whats wrong with the link I gave you?

+Pie Number of slices to send: Send
Hi Rob,

Web browser says "Operation timed out".

Pete,

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

Thanks
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Ok. I have read SSCCE. Here is my code:



I still cannot get the combobox in the jframe. Please help. Thanks.
+Pie Number of slices to send: Send
 

Ok. I have read SSCCE.



Well, you didn't real it very closely. That is NOT a SSCCE.
+Pie Number of slices to send: Send
Indeed. I don't have a working database connection.
+Pie Number of slices to send: Send
Sorry, here's the revised code:



Please help, thanks.
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
ok here's the code:


There are 2 buttons in the Jframe. Press the first button.
Sorry about the previous posts. Please help. Thanks
+Pie Number of slices to send: Send
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.
Yeah, but is it art? What do you think tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 3065 times.
Similar Threads
any idea about JComboBox Modal? with JDBC
JComboBox
Change & sort the contents of JComboBox in Swing
How to set ID and Value for a JCombobox
Display value and Actual value in JComboBox
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:07:51.