R
Roberto Sagoleo
Hi Guys,
here I am again with another amazing trouble! 0
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
///////////////////////////////////////////////////////////////////
//
// 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"
///////////////////////////////////////////////////////////////////
here I am again with another amazing trouble! 0
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
///////////////////////////////////////////////////////////////////
//
// 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"
///////////////////////////////////////////////////////////////////