JComboBox with IDentifiers

R

Roberto Sagoleo

Hi Guys,
here I am again with another amazing trouble! 0:cool:

I need to create a JComboBox containing a list of Items, each of which has its own IDentifier!

So: when I select an Item of the JComboBox, I need to get its IDentifier (ie: its ID in a DataBase Table).

I wrote the following four classes example, and I hope this could help someone else with my same needs!

Check it out at: http://www.jesax.net/?demo/java/jcombobox-identifier

Please, let me know if and how it can be useful!

Best Regards,
Roberto Sagoleo.
[2012/07/19] 0:cool:





///////////////////////////////////////////////////////////////////
//
// JESAX.NET [ http://www.jesax.net/ ]
//
// JAVA/script Environmental Subroutines for Accessible XML
//
// BoF :: "JSX_JAVA_Main.java"
//
// (C) CopyRight JESAX.net / SagoSoft.it [1984 - 2012]
//
// Last update: 2012/07/19 - 12:34
//
///////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////
//
// Class.
//
public class JSX_JAVA_Main
{

public static void main(String[] args)
{
System.out.println( "JSX:: JAVA 'JComboBox-IDentifier' starting..." );
JSX_JAVA_JFrame_IDentifier l_jsx_jframe_identifier;
l_jsx_jframe_identifier = new JSX_JAVA_JFrame_IDentifier( "JSX.JAVA >>>>> JComboBox-IDentifier <<<<<" );
l_jsx_jframe_identifier.setVisible(true);
}//main

}//class
///////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////
// EoF :: "JSX_JAVA_Main.java"
///////////////////////////////////////////////////////////////////





///////////////////////////////////////////////////////////////////
//
// JESAX.NET [ http://www.jesax.net/ ]
//
// JAVA/script Environmental Subroutines for Accessible XML
//
// BoF :: "JSX_JAVA_JFRame_IDentifier.java"
//
// (C) CopyRight JESAX.net / SagoSoft.it [1984 - 2012]
//
// Last update: 2012/07/19 - 12:34
//
///////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////
//
// Imports.
//
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;

///////////////////////////////////////////////////////////////////
//
// Class.
//
public class JSX_JAVA_JFrame_IDentifier extends JFrame
{
private static final long serialVersionUID = 1L;
private JSX_JAVA_JComboBox_IDentifier m_jcombobox_identifier ;
private JButton m_jbutton_continue ;
private JLabel m_jlabel_identifier ;
private JLabel m_jlabel_index ;
private JPanel m_jpanel_identifier ;
private JSeparator m_jseparator_v01 ;
private JSeparator m_jseparator_v02 ;
private JSeparator m_jseparator_v03 ;

public JSX_JAVA_JFrame_IDentifier( String p_title )
{

// Inits.
super( p_title );
this.setLocationRelativeTo(null);
this.setSize(new Dimension(500,300));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Custom data.
Object[] itemData_0 = new Object[] { 0, "Please select an Item..." , "zero" };
Object[] itemData_1 = new Object[] { 1, "The first Item has an IDentifier with Value 1" , "1st" };
Object[] itemData_2 = new Object[] { 2, "The second Item has an IDentifier like its Position" , "2nd" };
Object[] itemData_3 = new Object[] { 5, "The third Item switches its IDentifier with the 5th Item" , "3rd" };
Object[] itemData_4 = new Object[] { 4, "The fourth Item has has an IDentifier corresponding to its Position" , "4th" };
Object[] itemData_5 = new Object[] { 3, "The fifth Item switches its IDentifier with the 3rd Item" , "5th" };

// Graphics.
m_jpanel_identifier = new JPanel();

// Separators.
m_jseparator_v01 = new JSeparator( SwingConstants.VERTICAL );
m_jseparator_v02 = new JSeparator( SwingConstants.VERTICAL );
m_jseparator_v03 = new JSeparator( SwingConstants.VERTICAL );
m_jseparator_v01.setSize(500, 10);
m_jseparator_v02.setSize(500, 10);
m_jseparator_v03.setSize(500, 10);
m_jseparator_v01.setPreferredSize(new Dimension(500,10));
m_jseparator_v02.setPreferredSize(new Dimension(500,10));
m_jseparator_v03.setPreferredSize(new Dimension(500,10));

// Combos.
m_jcombobox_identifier = new JSX_JAVA_JComboBox_IDentifier();
m_jcombobox_identifier.setSize(400, 50);
m_jcombobox_identifier.setPreferredSize(new Dimension(400,50));

// Custom data.
m_jcombobox_identifier.addItem( itemData_0 );
m_jcombobox_identifier.addItem( itemData_1 );
m_jcombobox_identifier.addItem( itemData_2 );
m_jcombobox_identifier.addItem( itemData_3 );
m_jcombobox_identifier.addItem( itemData_4 );
m_jcombobox_identifier.addItem( itemData_5 );
m_jcombobox_identifier.setSize(400, 50);
m_jcombobox_identifier.setPreferredSize(new Dimension(400,50));

// Buttons.
m_jbutton_continue = new JButton( "Update..." );
m_jbutton_continue.setSize(400, 50);
m_jbutton_continue.setPreferredSize(new Dimension(400,50));
m_jbutton_continue.setSize( new Dimension(400,50));
m_jbutton_continue.setAlignmentX(Component.CENTER_ALIGNMENT);

// Labels.
m_jlabel_index = new JLabel( "Index: N/A" );
m_jlabel_identifier = new JLabel( "Identifier: N/A" );
m_jlabel_index.setHorizontalTextPosition(JLabel.LEFT);
m_jlabel_identifier.setHorizontalTextPosition(JLabel.LEFT);
m_jlabel_index.setVerticalTextPosition(JLabel.TOP);
m_jlabel_identifier.setVerticalTextPosition(JLabel.TOP);
m_jlabel_index.setAlignmentX(Component.LEFT_ALIGNMENT);
m_jlabel_identifier.setAlignmentX(Component.LEFT_ALIGNMENT);

// Rendering.
this.add( m_jpanel_identifier );
m_jpanel_identifier.add( m_jcombobox_identifier );
m_jpanel_identifier.add( m_jseparator_v01 );
m_jpanel_identifier.add( m_jbutton_continue );
m_jpanel_identifier.add( m_jseparator_v02 );
m_jpanel_identifier.add( m_jlabel_index );
m_jpanel_identifier.add( m_jseparator_v03 );
m_jpanel_identifier.add( m_jlabel_identifier );

// Listeners.
ActionListener actionlistener_continue = new ActionListener()
{
@Override public void actionPerformed(ActionEvent ev)
{
UpdateIndexAndIdentifier( );
}//actionPerformed
};//actionlistener_continue
m_jbutton_continue.addActionListener(actionlistener_continue);

}//constructor

private void UpdateIndexAndIdentifier( )
{
Integer l_selectedIndex ;
Integer l_selectedIDentifier ;
String l_index_str ;
String l_identifier_str ;

// Index.
l_selectedIndex = m_jcombobox_identifier.getSelectedIndex();

// IDentifier.
l_selectedIDentifier = m_jcombobox_identifier.getIdentifierAt( l_selectedIndex );

// Labels.
l_index_str = String.valueOf( l_selectedIndex );
l_identifier_str = String.valueOf( l_selectedIDentifier );
m_jlabel_index.setText( "Index= " + l_index_str );
m_jlabel_identifier.setText( "IDentifier= " + l_identifier_str );

}//UpdateIndexAndIdentifier

}//class
///////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////
// EoF :: "JSX_JAVA_JFRame_IDentifier.java"
///////////////////////////////////////////////////////////////////





///////////////////////////////////////////////////////////////////
//
// JESAX.NET [ http://www.jesax.net/ ]
//
// JAVA/script Environmental Subroutines for Accessible XML
//
// BoF :: "JSX_JAVA_JComboBox_IDentifier.java"
//
// (C) CopyRight JESAX.net / SagoSoft.it [1984 - 2012]
//
// Last update: 2012/07/19 - 12:34
//
///////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////
//
// Imports.
//
import java.util.ArrayList;
import javax.swing.JComboBox;
import java.awt.List;
import javax.swing.*;
import java.awt.*;
import java.awt.List;
import java.awt.event.*;
import java.lang.Object;
import java.util.*;

///////////////////////////////////////////////////////////////////
//
// Class.
//
public class JSX_JAVA_JComboBox_IDentifier extends JComboBox
{

ArrayList<JSX_JAVA_NamVal> m_namval_list;

public JSX_JAVA_JComboBox_IDentifier()
{
super( );

m_namval_list = new ArrayList<JSX_JAVA_NamVal>();
}//constructor

public void addItem( Object[] p_obj )
{
String l_jsx_nam;
int l_jsx_val;

// Data.
l_jsx_val = (Integer) p_obj[0];
l_jsx_nam = (String) p_obj[1];

// Super.
super.addItem( (Object)l_jsx_nam );

JSX_JAVA_NamVal l_namval_obj;
l_namval_obj = new JSX_JAVA_NamVal();
l_namval_obj._SetNam( l_jsx_nam );
l_namval_obj._SetVal( l_jsx_val );
m_namval_list.add( l_namval_obj );

}//addItem

public int getIdentifierAt( int p_iIndex )
{
JSX_JAVA_NamVal l_namval_obj;
int l_val;

// IDentifier.
l_namval_obj = this.m_namval_list.get( p_iIndex );
l_val = l_namval_obj._GetVal();

// Return value.
return( l_val );

}//getIdentifierAt

}//class
///////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////
// EoF :: "JSX_JAVA_JComboBox_IDentifier.java"
///////////////////////////////////////////////////////////////////





///////////////////////////////////////////////////////////////////
//
// JESAX.NET [ http://www.jesax.net/ ]
//
// JAVA/script Environmental Subroutines for Accessible XML
//
// BoF :: "JSX_JAVA_NamVal.java"
//
// (C) CopyRight JESAX.net / SagoSoft.it [1984 - 2012]
//
// Last update: 2012/07/19 - 12:34
//
///////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////
//
// Imports.
//
import java.lang.Object;
import java.util.*;

///////////////////////////////////////////////////////////////////
//
// Class.
//
public class JSX_JAVA_NamVal
{

int m_jsx_java_val;
String m_jsx_java_nam;

public JSX_JAVA_NamVal()
{
this.m_jsx_java_val = 0;
this.m_jsx_java_nam = "";
}//constructor

public void _SetVal( int p_val )
{
int l_val;
l_val = p_val;
this.m_jsx_java_val = l_val;
}//_SetVal
public int _GetVal( )
{
int l_val;
l_val = this.m_jsx_java_val;
return( l_val );
}//_GetVal

public void _SetNam( String p_nam )
{
String l_nam;
l_nam = p_nam;
this.m_jsx_java_nam = l_nam;
}//_SetNam
public String _GetNam( )
{
String l_nam;
l_nam = this.m_jsx_java_nam;
return( l_nam );
}//_GetNam

@Override public String toString()
{
String l_nam;
l_nam = super.toString();
l_nam = this._GetNam();
return( l_nam );
}//toString

}//class
///////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////
// EoF :: "JSX_JAVA_NamVal.java"
///////////////////////////////////////////////////////////////////
 
M

markspace

Hi Guys, here I am again with another amazing trouble! 0:cool:

I need to create a JComboBox containing a list of Items, each of
which has its own IDentifier!

So: when I select an Item of the JComboBox, I need to get its
IDentifier (ie: its ID in a DataBase Table).

I wrote the following four classes example, and I hope this could
help someone else with my same needs!


Honestly I think your code is overly baroque, and doesn't follow Java
coding conventions. So it's not really useful.

Take a look at the tutorials here, to see some examples that I think are
more useful:

<http://netbeans.org/kb/trails/tools.html>

Let's extend this question somewhat: There's a "database inspector"
built into NetBeans, but I find it clumsy. Do people use GUI-based
database inspector/administration tools? Which ones do you like? If
you were spec-ing a Java version, what features would you like to see?
 
L

Lew

Sax@DIST said:
here I am again with another amazing trouble! 0:cool:

I need to create a JComboBox containing a list of Items, each of which has its own IDentifier!

So: when I select an Item of the JComboBox, I need to get its IDentifier (ie: its ID in a DataBase Table).

I wrote the following four classes example, and I hope this could help someone else with my same needs!

It would be a lot more useful if your code followed the Java Coding Conventions.

More seriously, you fail to respect that GUI actions must happen on the EDT.

It's a rookie mistake.

Idioms like:

String l_nam;
l_nam = this.m_jsx_java_nam;
return( l_nam );

should be replaced with the more readable

return this.jsxName;

Why create a throwaway reference?
Why the redundant parenteses on the return value?

You use code comments in an unusual way, and worse, in lieu of Javadoc comments.

}//constructor

rather than

/**
* Constructor.
*/

Import-on-demand is not a best practice.

import java.util.*;

Comments should actually provide information.

///////////////////////////////////////////////////////////////////
//
// Class.
//

Really?

Declare the widest type applicable. In particular, prefer interfaces to concrete types.

ArrayList<JSX_JAVA_NamVal> m_namval_list;

should be

List<JsxName> names;

Which brings up the point that variables should be named in terms of the problem
domain, not the implementation domain. The fact that the variable is a list in this
case should not be part of the name.

Variables should be scoped as locally as applicable, and declared as close as
convenient to their point of use.

None of this:

private void UpdateIndexAndIdentifier( )
{
Integer l_selectedIndex ;
Integer l_selectedIDentifier ;
String l_index_str ;
String l_identifier_str ;

Setting member variables to their default values in initializers or constructors is usually
unnecessary and always redundant.

this.m_jsx_java_val = 0;

You should fix these issues before proffering your code to the programming public.
As it stands your code is not professional.
 
S

Sax@DIST

Hi People,
thanks a lot for your kind replies. 0:cool:

I'll (try to) follow your suggests for JAVA programming in the future, as I'm working in JAVA language since a few days, and I didn't find any solution to my needs, so I wrote this one in the while of finding info about my needs! 0;-P

BtW: which is the best place where to find (lots of simple) examples for learning the JAVA philosophy; for example: a *good and simple* solution for this problem? 08-?

PS: I must use the Eclipse environment in the development of this small project, as it's part of a bigger one! 08-!

Thanks a lot again,
Roberto. 0:cool:
 
M

markspace

BtW: which is the best place where to find (lots of simple) examples
for learning the JAVA philosophy; for example: a *good and simple*
solution for this problem? 08-?


Well, I still don't know what the problem is, other than "I don't know
Java." (Note spelling--not all caps.) Anything you read now should
help, since you are just beginning. Here's two ideas, both from the
Java tutorial (which you should certainly read).

JCombobox:

<http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html>

Database connections:

<http://docs.oracle.com/javase/tutorial/jdbc/index.html>
 
S

Sax@DIST

Hi markspace,
thanks a lot for the two links you suggested me about the Java Programming Language! 0;-)

The first one, about the "correct" usage of JComboBox itself, is very detailed and I think I can find what I need, reading the lots of examples.

The second one, about the database connection, will be very useful for me, as I need to get the data for the items of my JComboBox from a DataBase Table.

After all, my needs are very simple:
(o) to read some records (fields required: 'IDentifier' and 'Description') from a DB-Table
(o) to show the 'Description' of the records as Items of the JComboBox
(o) to do something else with the 'IDentifier' of the selected Item within the JComboBox

Thanks again for your precious help,
Greetings.
Roberto
0:cool:
 
D

Daniele Futtorovic

Hi markspace,
thanks a lot for the two links you suggested me about the Java Programming Language! 0;-)

The first one, about the "correct" usage of JComboBox itself, is very detailed and I think I can find what I need, reading the lots of examples.

The second one, about the database connection, will be very useful for me, as I need to get the data for the items of my JComboBox from a DataBase Table.

After all, my needs are very simple:
(o) to read some records (fields required: 'IDentifier' and 'Description') from a DB-Table
(o) to show the 'Description' of the records as Items of the JComboBox
(o) to do something else with the 'IDentifier' of the selected Item within the JComboBox

Thanks again for your precious help,
Greetings.
Roberto
0:cool:

Hi Roberto,

You should create a custom data structure containing the data you need
(identifier and representation, from the looks of it), add them to the
JComboBox's contents, and use a custom renderer
(<http://docs.oracle.com/javase/6/doc...tml#setRenderer(javax.swing.ListCellRenderer)>).
That way, the _items_ in the combo box will always be your custom data
structure instances (which you can use in #setSelectedItem(Object), for
instance; or returned by #getSelectedItem()), and the renderer controls
how they're displayed.

In addition -- I believe I've already seen something like this coded
somewhere -- you can write your own custom model
(<http://docs.oracle.com/javase/6/docs/api/javax/swing/ComboBoxModel.html>)
and have that hook directly to the database, possibly with on-demand,
lightweight loading of data.

HTH,
 
S

Sax@DIST

Hi to All,
I've read all your kind replies to my posting, and I thank you all very much.

I'm now discovering which is the best (correct) way to develop the solving of my needs.

Thanks again;
to read you soon.
Best Regards,
RoB! 0:cool:


[2012/08/15]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top