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 Netbeans GUI Code Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Netbeans GUI Code Doubt" Watch "Netbeans GUI Code Doubt" New topic
Author

Netbeans GUI Code Doubt

Ashutosh Deo
Greenhorn

Joined: Jul 12, 2006
Posts: 10
Hi!

I have developed a GUI using Swing in Netbeans 6.0.1

I have a JComboBox component which I need to populate with some specific currencies(Strings), retrieved by using a Third-Party Software API.

I have a method PopulateCurrencies() which contains the code to retrieve those strings. This method contains an array of Strings - curr[] which stores the retrieved currencies using APIs.

My Questions -

1) Where do I include this Method code in the Program? (If in the GUI Source, Where Exactly?)

2) How Do I associate the Method with the JComboBox ?



Note: I tried to use the JComboBox's model property in properties
I selected "Value from Existing Component" for setting the model and then selected "Method Call".

But I could not figure it out.


I'd be glad if anyone helps me out!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Originally posted by Ashutosh Deo:
I have developed a GUI using Swing in Netbeans 6.0.1

<soapbox> If you are a student of Swing, I would strongly advise you to avoid using Netbeans-generated Swing code until you have a better familiarity with Swing coding. I firmly believe that using this crutch at this stage will hamper your learning. Later when you know your Swing, then it ceases being a crutch and becomes a tool, but not now. </soapbox>

I have a method PopulateCurrencies() which contains the code to retrieve those strings. This method contains an array of Strings - curr[] which stores the retrieved currencies using APIs.
1) Where do I include this Method code in the Program? (If in the GUI Source, Where Exactly?)

I don't know as it would depend on how your program is structured.

2) How Do I associate the Method with the JComboBox ?
Note: I tried to use the JComboBox's model property in properties
I selected "Value from Existing Component" for setting the model and then selected "Method Call".
But I could not figure it out.


You should look into the DefaultComboBoxModel class as I think that this is where your answer lies. You could create an object of this class, passing your array in its constructor, and then pass this object into your JComboBox via the combobox's "setModel(...)" method. Something like so:



Good luck, and hope this helps,

Pete
Ashutosh Deo
Greenhorn

Joined: Jul 12, 2006
Posts: 10
I will try that out Pete, Thanks


My program structure is like this-

I created a New Project -> Java Application -> New JFrame Form in the Package.

Then I designed the GUI with all the components.

The GUI Source is something like this ->


public class CSGenerator extends javax.swing.JFrame
{

public CSGenerator()
{
initComponents();
}


//ActionPerformed Methods of some of the components.



private void initComponents()
{
//Automatically Generated Code

jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "12", "123" })); //HardCoded Values

//Automatically Generated Code

}


public static void main(String args[])
{ // Main Method with the run() function}


//Variables Declaration

}


As you can see, The setModel method is in the Automatically generated code and the only way to Customize that particular code is by going to the Customize Code property of the JComboBox component. But I can't figure out where do I write my PopulateCurrencies() Method and then pass the curr[] array to the DefaultComboBox Model object.
[ April 29, 2008: Message edited by: Ashutosh Deo ]
Ashutosh Deo
Greenhorn

Joined: Jul 12, 2006
Posts: 10
Hi,

I found out where to include that method. I included it in the Class and simply called the PopulateCurrencies() method from the Customize Code Property of the JCombobox.

I got the string array and then passed it to the setModel method.


I have a special requirement - The JComboBox should be hidden until I click on some Radio button. i.e. I want the JComboBox to be created and populated during runtime. (Populated according to the Radio button action listener and the PopulateCurrencies() method)

How do I hide the component and display it with the populated values whenever the radio button is clicked?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
I can think of several ways to do this:
1) show the combobox in a disabled form and then enable it on radiobutton press, or
2) place the combobox in a JPanel that is held as a "card" for a cardlayout-using jpanel. When a radiobutton is pressed, show the card that holds the combobox. This is the best solution if you have to show and hide several components at once
3) set combobox invisible. make visible on radiobutton press.
4) add and remove combobox as needed. Kind of awkward (it seems to me).

Here's a solution that demonstrates 1) 2) and 3):


Hope it helps.
[ April 30, 2008: Message edited by: pete stein ]
Ashutosh Deo
Greenhorn

Joined: Jul 12, 2006
Posts: 10
Originally posted by pete stein:

3) set combobox invisible. make visible on radiobutton press.



Thanks a lot Pete. This approach worked best for me.

I have stopped using Netbeans as it doesn't give me much flexibility in editing and re-arranging the code as I want it. I don't find the "Back-door" Entry to the code so user-friendly. Hence I switched over to Eclipse.


Thanks once again for your inputs.

Cheers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Netbeans GUI Code Doubt