Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #17655 > unrolled thread
| Started by | Roberto Sagoleo <roberto.sagoleo@gmail.com> |
|---|---|
| First post | 2012-08-10 14:41 -0700 |
| Last post | 2012-08-22 03:30 -0700 |
| Articles | 9 — 6 participants |
Back to article view | Back to comp.lang.java.programmer
JComboBox with IDentifiers Roberto Sagoleo <roberto.sagoleo@gmail.com> - 2012-08-10 14:41 -0700
Re: JComboBox with IDentifiers markspace <-@.> - 2012-08-10 15:14 -0700
Re: JComboBox with IDentifiers Lew <lewbloch@gmail.com> - 2012-08-10 15:26 -0700
Re: JComboBox with IDentifiers "Sax@DIST" <roberto.sagoleo@gmail.com> - 2012-08-12 14:03 -0700
Re: JComboBox with IDentifiers markspace <-@.> - 2012-08-12 14:37 -0700
Re: JComboBox with IDentifiers "Sax@DIST" <roberto.sagoleo@gmail.com> - 2012-08-13 09:22 -0700
Re: JComboBox with IDentifiers Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-08-13 18:49 +0200
Re: JComboBox with IDentifiers Roedy Green <see_website@mindprod.com.invalid> - 2012-08-14 18:02 -0700
Re: JComboBox with IDentifiers "Sax@DIST" <roberto.sagoleo@gmail.com> - 2012-08-22 03:30 -0700
| From | Roberto Sagoleo <roberto.sagoleo@gmail.com> |
|---|---|
| Date | 2012-08-10 14:41 -0700 |
| Subject | JComboBox with IDentifiers |
| Message-ID | <08a4e9e5-be46-4a15-94c4-e6bfab71d781@googlegroups.com> |
Hi Guys,
here I am again with another amazing trouble! 08-)
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] 08-)
///////////////////////////////////////////////////////////////////
//
// 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"
///////////////////////////////////////////////////////////////////
[toc] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2012-08-10 15:14 -0700 |
| Message-ID | <k0414b$rra$1@dont-email.me> |
| In reply to | #17655 |
On 8/10/2012 2:41 PM, Roberto Sagoleo wrote: > Hi Guys, here I am again with another amazing trouble! 08-) > > 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?
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2012-08-10 15:26 -0700 |
| Message-ID | <79bebafa-1f43-4a8b-aff1-df1493d1c05a@googlegroups.com> |
| In reply to | #17655 |
Sax@DIST wrote:
> here I am again with another amazing trouble! 08-)
>
> 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.
--
Lew
[toc] | [prev] | [next] | [standalone]
| From | "Sax@DIST" <roberto.sagoleo@gmail.com> |
|---|---|
| Date | 2012-08-12 14:03 -0700 |
| Message-ID | <2eae09a5-6d01-401a-acb0-cc54ba8b516e@googlegroups.com> |
| In reply to | #17655 |
Hi People, thanks a lot for your kind replies. 08-) 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. 08-) On Friday, August 10, 2012 11:41:33 PM UTC+2, Sax@DIST wrote: > Hi Guys, > here I am again with another amazing trouble! 08-) > 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! > ... >
[toc] | [prev] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2012-08-12 14:37 -0700 |
| Message-ID | <k097nl$1is$1@dont-email.me> |
| In reply to | #17768 |
On 8/12/2012 2:03 PM, Sax@DIST wrote: > 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>
[toc] | [prev] | [next] | [standalone]
| From | "Sax@DIST" <roberto.sagoleo@gmail.com> |
|---|---|
| Date | 2012-08-13 09:22 -0700 |
| Message-ID | <5a6762d4-5fb9-451d-86eb-98c26f3ef95a@googlegroups.com> |
| In reply to | #17770 |
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 08-) On Sunday, August 12, 2012 11:37:51 PM UTC+2, markspace wrote: > On 8/12/2012 2:03 PM, Sax@DIST wrote: > > 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> >
[toc] | [prev] | [next] | [standalone]
| From | Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> |
|---|---|
| Date | 2012-08-13 18:49 +0200 |
| Message-ID | <k0bb6s$k60$1@dont-email.me> |
| In reply to | #17787 |
On 13/08/2012 18:22, Sax@DIST allegedly wrote: > 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 > 08-) 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/docs/api/javax/swing/JComboBox.html#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, -- DF.
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2012-08-14 18:02 -0700 |
| Message-ID | <78tl28d7khrud6uu32ths75jo6pql2rteb@4ax.com> |
| In reply to | #17655 |
On Fri, 10 Aug 2012 14:41:33 -0700 (PDT), Roberto Sagoleo <roberto.sagoleo@gmail.com> wrote, quoted or indirectly quoted someone who said : >I need to create a JComboBox containing a list of Items, each of which has its own IDentifier! see http://mindprod.com/jgloss/jcombobox.html for sample code. I usually make each option an enum constant. -- Roedy Green Canadian Mind Products http://mindprod.com A new scientific truth does not triumph by convincing its opponents and making them see the light, but rather because its opponents eventually die, and a new generation grows up that is familiar with it. ~ Max Planck 1858-04-23 1947-10-04
[toc] | [prev] | [next] | [standalone]
| From | "Sax@DIST" <roberto.sagoleo@gmail.com> |
|---|---|
| Date | 2012-08-22 03:30 -0700 |
| Message-ID | <a20f581b-396b-4868-ae4b-9318ddbf74ce@googlegroups.com> |
| In reply to | #17655 |
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! 08-) [2012/08/15] > Hi Guys, > here I am again with another amazing trouble! 08-) > 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] 08-) >
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web