Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Loading a JComboBox

Reply
Thread Tools

Loading a JComboBox

 
 
bruce
Guest
Posts: n/a
 
      11-21-2010
I am using NetBeans for my development.

I want to load a combobox with two pieces of information, namely the
data from a SELECT of a primary key and a datafield. How can I do
this?

I thought about creating an Object containing the Key and the
datafield, but couldn't figure out how to display the "datafield" in
the dropdown list.

Appreciate any help.

Thanks...

Bruce
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      11-21-2010
On 11/21/2010 3:14 PM, bruce wrote:
> I am using NetBeans for my development.
>
> I want to load a combobox with two pieces of information, namely the
> data from a SELECT of a primary key and a datafield. How can I do
> this?
>
> I thought about creating an Object containing the Key and the
> datafield, but couldn't figure out how to display the "datafield" in
> the dropdown list.


It's pretty straightforward. There are several ways to do
it; here's one:

Object[] items = new Object[] { theKey, theDataField };
JComboBox cbox = new JComboBox(items);

Then add the combo box to your JPanel or whatever, and eventually to
a JFrame, pack() and setVisible(true).

Or if the combo box already exists in the GUI and you want to
change the list of items it displays and selects from:

Object[] items = ...as above...;
JComboBox cbox = ...the existing combo box...;
cbox.setModel(new DefaultComboBoxModel(items));

There are additional variations, too, but these should get you
started. If they don't, please explain your difficulty in more detail
than "couldn't figure out" and show us what you've tried.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
 
 
 
bruce
Guest
Posts: n/a
 
      11-21-2010
On Nov 21, 3:37*pm, Eric Sosman <(E-Mail Removed)> wrote:
> On 11/21/2010 3:14 PM, bruce wrote:
>
> > I am using NetBeans for my development.

>
> > I want to load a combobox with two pieces of information, namely the
> > data from a SELECT of a primary key and a datafield. *How can I do
> > this?

>
> > I thought about creating an Object containing the Key and the
> > datafield, but couldn't figure out how to display the "datafield" in
> > the dropdown list.

>
> * * *It's pretty straightforward. *There are several ways to do
> it; here's one:
>
> * * * * Object[] items = new Object[] { theKey, theDataField };
> * * * * JComboBox cbox = new JComboBox(items);
>
> Then add the combo box to your JPanel or whatever, and eventually to
> a JFrame, pack() and setVisible(true).
>
> * * *Or if the combo box already exists in the GUI and you want to
> change the list of items it displays and selects from:
>
> * * * * Object[] items = ...as above...;
> * * * * JComboBox cbox = ...the existing combo box...;
> * * * * cbox.setModel(new DefaultComboBoxModel(items));
>
> * * *There are additional variations, too, but these should get you
> started. *If they don't, please explain your difficulty in more detail
> than "couldn't figure out" and show us what you've tried.
>
> --
> Eric Sosman
> (E-Mail Removed)


I think I don't know how to do line 2
JComboBox cbox = ...the existing combo box...;

Here is what I have

Object[][] items = new Object[][]{
{1, "BridgeMill Family Medical Associates, PC"},
{4, "Diagnostic and Imaging Services",},
{3, "Northside Hospital - Cherokee"}};

JComboBox cbox = cboTestLoad;
cbox.setModel(new DefaultComboBoxModel(items));

I'm getting [Ljava.lang.Object;@f9f9d8, [Ljava.lang.Object;@87816d and
[Ljava.lang.Object;@422ede
loaded into the combobox..

So it's obvious I'm doing something wrong. Suggestions on what it is!!

Thanks for the help....

Bruce
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      11-21-2010
On 11/21/2010 1:41 PM, bruce wrote:

> Object[][] items = new Object[][]{
> {1, "BridgeMill Family Medical Associates, PC"},
> {4, "Diagnostic and Imaging Services",},
> {3, "Northside Hospital - Cherokee"}};
>
> JComboBox cbox = cboTestLoad;
> cbox.setModel(new DefaultComboBoxModel(items));



The DefaultComboBoxModel takes a single dimensioned array as it's
parameter, not a two dimensional array. That's why you are seeing
arrays as the items in your box model.

Remove the 2nd array for this to work. What do 1,4,3, represent? If
it's some sort of internal coding you'll have to make a new type to
cache it.

E.g.:

class ComboHolder {
final private String display;
final private int num;

public ComboHolder( String display, int num )
{
this.display = display;
this.num = num;
}

@Override
public String toString()
{
return display;
}

public int getNum()
{
return num;
}

}

Here's an example:


package test;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JCoboboxTest {

public static void main( String[] args )
{
SwingUtilities.invokeLater( new Runnable()
{

public void run()
{
initGui();
}
} );
}

private static void initGui() {
JFrame jf = new JFrame("ComboTest");
JPanel jp = new JPanel();
ComboHolder[] model = {
new ComboHolder( "String 1", 1),
new ComboHolder( "String 4", 4),
new ComboHolder( "String 3", 3),
};
JComboBox combo = new JComboBox( model );
jp.add( combo );
jf.add( jp );
combo.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
System.out.println( ((ComboHolder)((JComboBox)e.getSource())
.getSelectedItem()).getNum() );
}
} );

jf.pack();
jf.setLocationRelativeTo( null );
jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
jf.setSize( 300, 300 );
jf.setVisible( true );
}
}

 
Reply With Quote
 
bruce
Guest
Posts: n/a
 
      11-22-2010
On Nov 21, 6:28*pm, markspace <(E-Mail Removed)> wrote:
> On 11/21/2010 1:41 PM, bruce wrote:
>
> > Object[][] items = new Object[][]{
> > * * * * * * *{1, "BridgeMill Family Medical Associates, PC"},
> > * * * * * * *{4, "Diagnostic and Imaging Services",},
> > * * * * * * *{3, "Northside Hospital - Cherokee"}};

>
> > * * * * *JComboBox cbox = cboTestLoad;
> > * * * * *cbox.setModel(new DefaultComboBoxModel(items));

>
> The DefaultComboBoxModel takes a single dimensioned array as it's
> parameter, not a two dimensional array. *That's why you are seeing
> arrays as the items in your box model.
>
> Remove the 2nd array for this to work. *What do 1,4,3, represent? *If
> it's some sort of internal coding you'll have to make a new type to
> cache it.
>
> E.g.:
>
> class ComboHolder {
> * * final private String display;
> * * final private int num;
>
> * * public ComboHolder( String display, int num )
> * * {
> * * * *this.display = display;
> * * * *this.num = num;
> * * }
>
> * * @Override
> * * public String toString()
> * * {
> * * * *return display;
> * * }
>
> * * public int getNum()
> * * {
> * * * *return num;
> * * }
>
> }
>
> Here's an example:
>
> package test;
>
> import java.awt.event.ActionEvent;
> import java.awt.event.ActionListener;
> import javax.swing.JComboBox;
> import javax.swing.JFrame;
> import javax.swing.JPanel;
> import javax.swing.SwingUtilities;
>
> public class JCoboboxTest {
>
> * * public static void main( String[] args )
> * * {
> * * * *SwingUtilities.invokeLater( new Runnable()
> * * * *{
>
> * * * * * public void run()
> * * * * * {
> * * * * * * *initGui();
> * * * * * }
> * * * *} );
> * * }
>
> * * private static void initGui() {
> * * * *JFrame jf = new JFrame("ComboTest");
> * * * *JPanel jp = new JPanel();
> * * * *ComboHolder[] model = {
> * * * * * new ComboHolder( "String 1", 1),
> * * * * * new ComboHolder( "String 4", 4),
> * * * * * new ComboHolder( "String 3", 3),
> * * * *};
> * * * *JComboBox combo = new JComboBox( model );
> * * * *jp.add( combo );
> * * * *jf.add( jp );
> * * * *combo.addActionListener( new ActionListener() {
> * * * * * public void actionPerformed( ActionEvent e ) {
> * * * * * * *System.out.println( ((ComboHolder)((JComboBox)e.getSource())
> * * * * * * * * * * *.getSelectedItem()).getNum() );
> * * * * * }
> * * * *} );
>
> * * * *jf.pack();
> * * * *jf.setLocationRelativeTo( null );
> * * * *jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
> * * * *jf.setSize( 300, 300 );
> * * * *jf.setVisible( true );
> * * }
>
> }
>
>


Thanks for the feedback. I'll work through it and map it my
application.

As for what I'm trying to do, this is from my initial posting:

I want to load a combobox with two pieces of information, namely
the
data from a SELECT of a primary key and a datafield. How can I
do
this?

The client selects a "datafield" from the dropdown. I then use the
"Primary Key" to extract the complete records from the database...

Thanks again...

Bruce
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      11-22-2010
On 11/21/2010 4:02 PM, bruce wrote:

> As for what I'm trying to do, this is from my initial posting:
>
> I want to load a combobox with two pieces of information, namely
> the
> data from a SELECT of a primary key and a datafield. How can I
> do
> this?



Well, that's what I did for you. I made a JComboBox hold two pieces of
information instead of one. I had it display a string, and when the
user selected one of those strings, I printed out the number that
corresponded to that string on the console (called "num" in my example).

 
Reply With Quote
 
bruce
Guest
Posts: n/a
 
      11-22-2010
On Nov 21, 7:19*pm, markspace <(E-Mail Removed)> wrote:
> On 11/21/2010 4:02 PM, bruce wrote:
>
> > As for what I'm trying to do, this is from my initial posting:

>
> > * * * I want to load a combobox with two pieces of information, namely
> > the
> > * * * data from a SELECT of a primary key and a datafield. *How can I
> > do
> > * * * this?

>
> Well, that's what I did for you. *I made a JComboBox hold two pieces of
> information instead of one. *I had it display a string, and when the
> user selected one of those strings, I printed out the number that
> corresponded to that string on the console (called "num" in my example).


Yes... I got your example to work. Thanks

Now I need to map to my application...

Appreciate the help....

Bruce
 
Reply With Quote
 
bruce
Guest
Posts: n/a
 
      11-22-2010
On Nov 21, 6:28*pm, markspace <(E-Mail Removed)> wrote:
> On 11/21/2010 1:41 PM, bruce wrote:
>
> > Object[][] items = new Object[][]{
> > * * * * * * *{1, "BridgeMill Family Medical Associates, PC"},
> > * * * * * * *{4, "Diagnostic and Imaging Services",},
> > * * * * * * *{3, "Northside Hospital - Cherokee"}};

>
> > * * * * *JComboBox cbox = cboTestLoad;
> > * * * * *cbox.setModel(new DefaultComboBoxModel(items));

>
> The DefaultComboBoxModel takes a single dimensioned array as it's
> parameter, not a two dimensional array. *That's why you are seeing
> arrays as the items in your box model.
>
> Remove the 2nd array for this to work. *What do 1,4,3, represent? *If
> it's some sort of internal coding you'll have to make a new type to
> cache it.
>
> E.g.:
>
> class ComboHolder {
> * * final private String display;
> * * final private int num;
>
> * * public ComboHolder( String display, int num )
> * * {
> * * * *this.display = display;
> * * * *this.num = num;
> * * }
>
> * * @Override
> * * public String toString()
> * * {
> * * * *return display;
> * * }
>
> * * public int getNum()
> * * {
> * * * *return num;
> * * }
>
> }
>
> Here's an example:
>
> package test;
>
> import java.awt.event.ActionEvent;
> import java.awt.event.ActionListener;
> import javax.swing.JComboBox;
> import javax.swing.JFrame;
> import javax.swing.JPanel;
> import javax.swing.SwingUtilities;
>
> public class JCoboboxTest {
>
> * * public static void main( String[] args )
> * * {
> * * * *SwingUtilities.invokeLater( new Runnable()
> * * * *{
>
> * * * * * public void run()
> * * * * * {
> * * * * * * *initGui();
> * * * * * }
> * * * *} );
> * * }
>
> * * private static void initGui() {
> * * * *JFrame jf = new JFrame("ComboTest");
> * * * *JPanel jp = new JPanel();
> * * * *ComboHolder[] model = {
> * * * * * new ComboHolder( "String 1", 1),
> * * * * * new ComboHolder( "String 4", 4),
> * * * * * new ComboHolder( "String 3", 3),
> * * * *};
> * * * *JComboBox combo = new JComboBox( model );
> * * * *jp.add( combo );
> * * * *jf.add( jp );
> * * * *combo.addActionListener( new ActionListener() {
> * * * * * public void actionPerformed( ActionEvent e ) {
> * * * * * * *System.out.println( ((ComboHolder)((JComboBox)e.getSource())
> * * * * * * * * * * *.getSelectedItem()).getNum() );
> * * * * * }
> * * * *} );
>
> * * * *jf.pack();
> * * * *jf.setLocationRelativeTo( null );
> * * * *jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
> * * * *jf.setSize( 300, 300 );
> * * * *jf.setVisible( true );
> * * }
>
> }
>
>


Okay.. I get your example to run fine. When I tried to map it to my
application, my "Inexperience" became apparent. I need to load the
ComboHolder object from a resultSet. Here is what I tried.

while (resultSet.next()) {

int facilityid = resultSet.getInt("facilityid");
String facility = resultSet.getString("name");
ComboHolder[] model = {new ComboHolder(facility, facilityid)};
System.out.println(facilityid + ":" + facility);

}

1. I don't know that the ComboHolder is being loaded correctly with
the code above.

2. When I added "JComboBox cboFacility = new JComboBox( model );"
after the closing left bracket, I go that model was "out of scope." I
was unable to define anything (above the while statement) that would
work.

Sorry that was unable to map your sample code to my application..

Your "additional" help will be greatly appreciated...

Thank you again...

Bruce

My application compiled and executed with no problems.
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      11-22-2010
On 11/22/2010 1:37 PM, bruce wrote:
>
> 2. When I added "JComboBox cboFacility = new JComboBox( model );"
> after the closing left bracket, I go that model was "out of scope."



Yeah well this is a basic syntax question. I'm pretty much unwilling to
write your entire program for you. You'll have to learn what scope is
and how it works.

All I did was write a little example. I expected you to modify it a lot
more.

Try "java variable scope" on Google.

 
Reply With Quote
 
bruce
Guest
Posts: n/a
 
      11-23-2010
On Nov 22, 6:26*pm, markspace <(E-Mail Removed)> wrote:
> On 11/22/2010 1:37 PM, bruce wrote:
>
>
>
> > 2. When I added "JComboBox cboFacility = new JComboBox( model );"
> > after the closing left bracket, I go that model was "out of scope."

>
> Yeah well this is a basic syntax question. *I'm pretty much unwilling to
> write your entire program for you. *You'll have to learn what scope is
> and how it works.
>
> All I did was write a little example. *I expected you to modify it a lot
> more.
>
> Try "java variable scope" on Google.



Thanks.. I do appreciate your help..

I do know what scope is... That's not the problem.. I have not been
able to define "model" so that it would have the scope I need.

I tried several different instances of defining ComboHolder which
would have made "model" in the "JComboBox cboFacility = new
JComboBox( model );" in scope.
Each attempt gave me an error.

I also added a method to the ComboHolder class to accept the
assignment of individual (String, int) pairs. When I did this, I had
to remove the "final" from the class final variables.

So, I have been trying to do it myself.

I do appreciate the help you have given me....
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic loading of a JComboBox bruce Java 4 10-18-2010 10:08 PM
Speeding up the loading of a JComboBox HP News Feed Java 7 07-19-2005 05:11 PM
JComboBox limit Sudarshan Raghavan Java 2 09-29-2003 05:20 PM
Objects in a JComboBox Ike Java 3 09-13-2003 05:52 PM
JComboBox itemStateChanged in 1.3 and 1.4 Manoj Nair Java 0 09-12-2003 04:38 AM



Advertisments