Appendix A: Complete code listings
charExample.java: character example
import javax.swing.*;
public class charExample
{
public static void main( String[] args )
{
boolean bFirst = true;
char aChar[] = {
'A', // character
65, // decimal
0x41, // hex
0101, // octal
'\u0041' // Unicode escape
};
char myChar = 256;
for( int i = 0; i < aChar.length; i++ )
{
System.out.print( aChar[ i ]++ + " " );
if( i == (aChar.length - 1) )
{
System.out.println( "\n---------" );
if( bFirst )
{
i = -1;
bFirst = !bFirst;
}
}
} // end for
// the result of adding two chars is an int
System.out.println( "aChar[0] + aChar[1] equals: " +
(aChar[0] + aChar[1]) );
System.out.println( "myChar at 256: " + myChar );
System.out.println( "myChar at 20116 or \\u4E94: " +
( myChar = 20116 ) );
// show integer value of the char
System.out.println( "myChar numeric value: " +
(int)myChar );
JFrame jf = new JFrame();
JOptionPane.showMessageDialog( jf,
"myChar at 20116 or \\u4E94: " +
( myChar = 20116 ) +
"\nmyChar numeric value: " +
(int)myChar,
"charExample", JOptionPane.ERROR_MESSAGE);
jf.dispose();
System.exit(0);
} // end main
} // End class charExample
|
ByTheNumbers.java: PropertyResourceBundle example
ByTheNumbersrb.properties
# Default properties in English 0=Zero: 1=One: 2=Two: 3=Three: 4=Four: 5=Five: 6=Six: 7=Seven: 8=Eight: 9=Nine: 10=Ten: random=Random title=Key in numbers to match the words: |
ByTheNumbersrb_de.properties
# Default properties in German 0=Null: 1=Eins: 2=Zwei: 3=Drei: 4=Vier: 5=Fünf: 6=Sechs: 7=Sieben: 8=Acht: 9=Neun: 10=Zehn: random=aufs Geratewohl |
ByTheNumbersrb_en.properties
# Default properties in English 0=Zero: 1=One: 2=Two: 3=Three: 4=Four: 5=Five: 6=Six: 7=Seven: 8=Eight: 9=Nine: 10=Ten: random=Random title=Key in numbers to match the words: |
ByTheNumbersrb_fr.properties
# Default properties in French 0=Z豯: 1=Uun: 2=deux: 3=Trois: 4=Quatre: 5=cinq: 6=Six: 7=Sept: 8=Huit: 9=Neuf: 10=Dix: random=au hasard |
ByTheNumbersrb_ru.properties
# Default properties in Russian 0=\u041D\u0443\u043B\u044C: 1=\u041E\u0434\u0438\u043D: 2=\u0414\u0432\u0430: 3=\u0422\u0440\u0438: 4=\u0427\u0435\u0442\u044B\u0440\u0435: 5=\u041F\u044F\u0442\u044C: 6=\u0428\u0435\u0441\u0442\u044C: 7=\u0441\u0435\u043C\u044C: 8=\u0412\u043E\u0441\u0435\u043C\u044C: 9=\u0414\u0435\u0432\u044F\u0442\u044C: 10=\u0414\u0435\u0441\u044F\u0442\u044C: random=\u041D\u0430\u0443\u0433\u0430\u0434 |
ByTheNumbers.java (Part 1)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class ByTheNumbers extends JFrame
implements ActionListener,
WindowListener
{
static final int sfiSIZE = 11;
boolean bRandomize = false;
Font fLucida;
int iSelIndex = 0;
int[] aiOrder = new int[sfiSIZE];
JButton jbOK = new JButton( "OK" ),
jbRandom = new JButton();
JComboBox jcb = null;
JLabel[] ajl = new JLabel[sfiSIZE];
JTextField[] ajtf = new JTextField[sfiSIZE];
JPanel jpNorth = new JPanel(
new GridLayout(0,1) ),
jpCenter = new JPanel(),
jpCenterNest = new JPanel(
new GridLayout(0,2) ),
jpSouth = new JPanel();
Locale[] alSupported = {
Locale.US,
Locale.FRANCE,
Locale.GERMANY,
new Locale( "ru", "RU" )
};
Random rNumber = new Random();
ResourceBundle rb;
String sRBName = getClass().getName() + "rb";
String[] asDNames;
public ByTheNumbers()
{
int i = 0;
JLabel jlTemp = null;
JTextField jtfTemp = null;
setTitle( "ByTheNumbers" );
addWindowListener( this );
Font fJB = jbOK.getFont();
fLucida = new Font("Lucida Sans",
fJB.getStyle(),
fJB.getSize() );
Container cp = getContentPane();
asDNames = new String[ alSupported.length ];
Locale lDefault = Locale.getDefault();
for( i = 0; i < alSupported.length; i++ )
{
asDNames[i] =
alSupported[i].getDisplayName();
if( iSelIndex == 0 &&
lDefault.equals( alSupported[i] ) )
{ iSelIndex = i; }
} // end for
jcb = new JComboBox( asDNames );
jcb.setFont( fLucida );
jcb.setSelectedIndex( iSelIndex );
jcb.addActionListener( this );
for( i = 0; i < ajl.length; i++ )
{
jlTemp = new JLabel();
jlTemp.setFont( fLucida );
jlTemp.setName( i + "" ); // set Name
jtfTemp = new JTextField(3);
jtfTemp.setHorizontalAlignment( JTextField.RIGHT );
ajl[i] = jlTemp;
ajtf[i] = jtfTemp;
jpCenterNest.add( jlTemp );
jpCenterNest.add( jtfTemp );
}
loadFromResourceBundle(); // get localized labels
jbOK.addActionListener( this );
jbRandom.setFont( fLucida );
jbRandom.setText( rb.getString( "random" ) );
jbRandom.addActionListener( this );
jpNorth.add( jcb );
jlTemp = new JLabel( rb.getString( "title" ) );
jlTemp.setFont( fLucida );
jpNorth.add( jlTemp );
jpCenter.add( jpCenterNest );
jpSouth.add(jbOK);
jpSouth.add(jbRandom);
cp.add( jpNorth, BorderLayout.NORTH );
cp.add( jpCenter, BorderLayout.CENTER );
cp.add( jpSouth, BorderLayout.SOUTH );
pack();
setResizable( false );
show();
} // end constructor
|
ByTheNumbers.java (Part 2)
public void checkAnswers()
{
boolean b = true;
JTextField jtf = null;
String s = null;
for( int i = 0; i < sfiSIZE; i++ )
{
jtf = ajtf[i];
s = jtf.getText().trim();
if( !s.equals( ajl[i].getName() ) )
{
jtf.requestFocus();
jtf.selectAll();
b = false;
break;
}
} // end for
JOptionPane.showMessageDialog( this,
b ? "Congratulations!" : "Keep Trying!",
"", JOptionPane.ERROR_MESSAGE);
} // end checkAnswers
public void loadFromResourceBundle()
{
try
{ // get the PropertyResourceBundle
rb = ResourceBundle.getBundle(
sRBName,
alSupported[iSelIndex] );
// get data associated with keys
for( int i = 0; i < sfiSIZE; i++ )
{
aiOrder[i] = i;
ajl[i].setText( rb.getString( ajl[i].getName() ) );
}
bRandomize = false;
} // end try
catch( MissingResourceException mre )
{
JOptionPane.showMessageDialog( this,
"ResourceBundle problem;\n" +
"Specific error: " + mre.getMessage(),
"", JOptionPane.ERROR_MESSAGE);
}
} // end loadFromResourceBundle
public void loadGUI()
{
boolean bFirst = true;
int i = 0,
j = 0;
if( bRandomize )
{
for( i = 0; i < sfiSIZE; i++ )
{
if( bFirst )
{ // init array to known value
aiOrder[i] = 99;
if( i == ( sfiSIZE - 1 ))
{
bFirst = !bFirst;
i = -1;
}
continue;
} // end if bFirst
while( true )
{
j = rNumber.nextInt( sfiSIZE );
if( aiOrder[j] == 99 )
{
aiOrder[j] = i;
break;
}
} // end while
} // end for
} // end if bRandomize
jpCenterNest.removeAll();
for( i = 0; i < sfiSIZE; i++ )
{
j = aiOrder[i];
ajtf[j].setText("");
jpCenterNest.add( ajl[j] );
jpCenterNest.add( ajtf[j] );
}
jpCenterNest.revalidate();
} // loadGUI
// ActionListener Implementation
public void actionPerformed(ActionEvent ae)
{
Object oSource = ae.getSource();
if( oSource == jbRandom )
{
bRandomize = true;
loadGUI();
return;
}
if( oSource == jbOK )
{
checkAnswers();
return;
}
if( oSource == jcb )
{
iSelIndex = jcb.getSelectedIndex();
loadFromResourceBundle();
loadGUI();
return;
}
} // End actionPerformed
// Window Listener Implementation
public void windowOpened(WindowEvent we) {}
public void windowClosing(WindowEvent we)
{
dispose();
System.exit(0);
}
public void windowClosed(WindowEvent we) {}
public void windowIconified(WindowEvent we) {}
public void windowDeiconified(WindowEvent we) {}
public void windowActivated(WindowEvent we) {}
public void windowDeactivated(WindowEvent we) {}
// End Window Listener Implementation
public static void main(String[] args)
{
new ByTheNumbers();
} // end main
} // end class ByTheNumbers |
JIBDateGUI.java: DateFormat example
JIBDateGUI.java (Part 1)
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
public class JIBDateGUI extends JFrame
implements ActionListener,
WindowListener
{
boolean bToggleFlag = true;
DateFormat dfLocal,
dfSelected;
java.sql.Date jsqlDate = new java.sql.Date(
System.currentTimeMillis() );
Font fLucida;
int iFormat,
iSelIndex = 0;
JButton jbOK = new JButton( "OK" ),
jbToggle = new JButton(
"Toggle Display Names" );
JComboBox jcb = null;
JPanel jpNorth = new JPanel(),
jpCenter = new JPanel(),
jpSouth = new JPanel();
JTextField jtI = new JTextField( 10 ),
jtD = new JTextField( 10 ),
jtP = new JTextField( 10 );
Locale lDefault = Locale.getDefault();
Locale[] alSupported;
String[] asDNames,
asLDNames;
public JIBDateGUI()
{
this( DateFormat.SHORT );
} // end default constructor
|
JIBDateGUI.java (Part 2)
public JIBDateGUI( int argiFormat )
{
String s1 = null;
setTitle( "JIBDateGUI" );
addWindowListener( this );
Font fJCB = jbToggle.getFont();
fLucida = new Font("Lucida Sans",
fJCB.getStyle(),
fJCB.getSize() );
iFormat = argiFormat;
dfLocal = DateFormat.getDateInstance(
iFormat );
alSupported = Locale.getAvailableLocales();
asDNames = new String[ alSupported.length ];
asLDNames = new String[ alSupported.length ];
for( int i = 0; i < alSupported.length; i++ )
{
asDNames[i] =
alSupported[i].getDisplayName();
s1 =
alSupported[i].getDisplayName( alSupported[i] );
if( fLucida.canDisplay( s1.charAt( 0 ) ) )
{ asLDNames[i] = s1; }
else
{ asLDNames[i] = s1 + " - font can't display."; }
if( iSelIndex == 0 &&
lDefault.equals( alSupported[i] ) )
{ iSelIndex = i; }
} // end for
toggleDisplayNames();
jtI.setText( dfLocal.format( jsqlDate ) );
// cause ActionPerformed event
jcb.setSelectedIndex( iSelIndex );
jbOK.addActionListener( this );
jbToggle.addActionListener( this );
jtD.setEditable( false );
jtD.setFont( fLucida );
jtP.setEditable( false );
jtP.setFont( fLucida );
dfLocal.setLenient( false );
jpNorth.add(new JLabel("Input a Date:"));
jpNorth.add(jtI);
jpNorth.add(jbOK);
JLabel jlTemp = new JLabel("Default = " +
lDefault.getDisplayName() );
jlTemp.setFont( fLucida );
jpNorth.add( jlTemp );
jpCenter.add(new JLabel("Display:"));
jpCenter.add(jtD);
jpCenter.add(new JLabel("Parsed ISO:"));
jpCenter.add(jtP);
Container cp = getContentPane();
cp.add( jpNorth, BorderLayout.NORTH );
cp.add( jpCenter, BorderLayout.CENTER );
cp.add( jpSouth, BorderLayout.SOUTH );
pack();
show();
} // end constructor
public void toggleDisplayNames()
{
boolean bjcbExist = false;
if( jcb != null )
{
jpSouth.remove( jcb );
jpSouth.remove( jbToggle );
iSelIndex = jcb.getSelectedIndex();
bjcbExist = true;
}
if( bToggleFlag )
{ jcb = new JComboBox( asDNames ); }
else
{ jcb = new JComboBox( asLDNames ); }
bToggleFlag = !bToggleFlag;
if( bjcbExist )
{ jcb.setSelectedIndex( iSelIndex ); }
jcb.setFont( fLucida );
jcb.addActionListener( this );
jpSouth.add( jcb );
jpSouth.add( jbToggle );
} // end toggleDisplayNames
// ActionListener Implementation
public void actionPerformed(ActionEvent ae)
{
Object oSource = ae.getSource();
if( oSource == jbToggle )
{
toggleDisplayNames();
pack();
return;
}
if( oSource == jcb )
{
dfSelected = DateFormat.getDateInstance(
iFormat,
alSupported[ jcb.getSelectedIndex() ] );
} // end if jcb, continue on
jtD.setText( "" );
jtP.setText( "" );
try
{
java.util.Date d = dfLocal.parse(
jtI.getText() );
jtI.setText( dfLocal.format( d ) );
jtI.setCaretPosition(0);
jtD.setText( dfSelected.format( d ) );
jtD.setCaretPosition(0);
d = dfSelected.parse( jtD.getText() );
// get new java.sql.Date
jsqlDate = new java.sql.Date( d.getTime() );
jtP.setText( jsqlDate.toString() );
}
catch( ParseException pe )
{
JOptionPane.showMessageDialog( this,
pe.getMessage(), "", JOptionPane.ERROR_MESSAGE);
}
} // End actionPerformed
// Window Listener Implementation
public void windowOpened(WindowEvent we) {}
public void windowClosing(WindowEvent we)
{
dispose();
System.exit(0);
}
public void windowClosed(WindowEvent we) {}
public void windowIconified(WindowEvent we) {}
public void windowDeiconified(WindowEvent we) {}
public void windowActivated(WindowEvent we) {}
public void windowDeactivated(WindowEvent we) {}
// End Window Listener Implementation
public static void main(String[] args)
{
int i = DateFormat.SHORT;
String s = null;
if( args.length == 1 )
{
if( args[0].equals( "full" ) )
{ i = DateFormat.FULL; }
else
if( args[0].equals( "long" ) )
{ i = DateFormat.LONG; }
else
if( args[0].equals( "medium" ) )
{ i = DateFormat.MEDIUM; }
}
new JIBDateGUI( i );
} // end main
} // end class JIBDateGUI |
JIBNumberGUI.java: NumberFormat example
JIBNumberGUI.java (Part 1)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
public class JIBNumberGUI extends JFrame
implements ActionListener,
WindowListener
{
boolean bToggleFlag = true,
bNumberFormat = true;
Font fLucida;
int iSelIndex = 0;
JButton jbOK = new JButton( "OK" ),
jbToggle = new JButton(
"Toggle Display Names" );
JComboBox jcb = null,
jcbDP = null;
JPanel jpNorth = new JPanel(),
jpCenter = new JPanel(),
jpSouth = new JPanel();
JTextField jtI = new JTextField( 10 ),
jtD = new JTextField( 10 ),
jtP = new JTextField( 10 );
Locale lDefault = Locale.getDefault();
Locale[] alSupported;
NumberFormat nfLocal = NumberFormat.getNumberInstance(),
nfSelected;
String[] asDNames,
asLDNames,
asDP = { "Number", "Percent"};
public JIBNumberGUI()
{
String s1 = null;
setTitle( "JIBNumberGUI" );
addWindowListener( this );
Font fJCB = jbToggle.getFont();
fLucida = new Font("Lucida Sans",
fJCB.getStyle(),
fJCB.getSize() );
alSupported = Locale.getAvailableLocales();
asDNames = new String[ alSupported.length ];
asLDNames = new String[ alSupported.length ];
for( int i = 0; i < alSupported.length; i++ )
{
asDNames[i] =
alSupported[i].getDisplayName();
s1 =
alSupported[i].getDisplayName( alSupported[i] );
if( fLucida.canDisplay( s1.charAt( 0 ) ) )
{ asLDNames[i] = s1; }
else
{ asLDNames[i] = s1 + " - font can't display."; }
if( iSelIndex == 0 &&
lDefault.equals( alSupported[i] ) )
{ iSelIndex = i; }
} // end for
toggleDisplayNames();
jtI.setText( nfLocal.format( 123456.7 ) );
// cause ActionPerformed event
jcb.setSelectedIndex( iSelIndex );
jcbDP = new JComboBox( asDP );
jcbDP.addActionListener( this );
jbOK.addActionListener( this );
jbToggle.addActionListener( this );
jtD.setEditable( false );
jtD.setFont( fLucida );
jtP.setEditable( false );
jtP.setFont( fLucida );
jpNorth.add(new JLabel("Input:"));
jpNorth.add(jcbDP);
jpNorth.add(jtI);
jpNorth.add(jbOK);
JLabel jlTemp = new JLabel("Default = " +
lDefault.getDisplayName() );
jlTemp.setFont( fLucida );
jpNorth.add( jlTemp );
jpCenter.add(new JLabel("Display:"));
jpCenter.add(jtD);
jpCenter.add(new JLabel("Parsed:"));
jpCenter.add(jtP);
Container cp = getContentPane();
cp.add( jpNorth, BorderLayout.NORTH );
cp.add( jpCenter, BorderLayout.CENTER );
cp.add( jpSouth, BorderLayout.SOUTH );
pack();
show();
} // end constructor
public void toggleDisplayNames()
{
boolean bjcbExisted = false;
if( jcb != null )
{
jpSouth.remove( jcb );
jpSouth.remove( jbToggle );
iSelIndex = jcb.getSelectedIndex();
bjcbExisted = true;
}
if( bToggleFlag )
{ jcb = new JComboBox( asDNames ); }
else
{ jcb = new JComboBox( asLDNames ); }
bToggleFlag = !bToggleFlag;
if( bjcbExisted )
{ jcb.setSelectedIndex( iSelIndex ); }
jcb.setFont( fLucida );
jcb.addActionListener( this );
jpSouth.add( jcb );
jpSouth.add( jbToggle );
} // end toggleDisplayNames
|
JIBNumberGUI.java (Part 2)
// ActionListener Implementation
public void actionPerformed(ActionEvent ae)
{
Number n = null;
Object oSource = ae.getSource();
if( oSource == jbToggle )
{
toggleDisplayNames();
pack();
return;
}
if( oSource == jcbDP )
{
if( jcbDP.getSelectedIndex() == 0 )
{
bNumberFormat = true;
try { n = nfLocal.parse( jtI.getText() ); }
catch( ParseException pe ) {}
nfLocal = NumberFormat.getNumberInstance();
}
else
{
bNumberFormat = false;
try { n = nfLocal.parse( jtI.getText() ); }
catch( ParseException pe ) {}
nfLocal = NumberFormat.getPercentInstance();
}
jtI.setText( nfLocal.format( n ) );
// set to perform jcb operation
oSource = jcb;
}
if( oSource == jcb )
{
if( bNumberFormat )
{
nfSelected = NumberFormat.getNumberInstance(
alSupported[ jcb.getSelectedIndex() ] );
}
else
{
nfSelected = NumberFormat.getPercentInstance(
alSupported[ jcb.getSelectedIndex() ] );
}
} // end if jcb, continue on
jtD.setText( "" );
jtP.setText( "" );
try
{
n = nfLocal.parse( jtI.getText() );
jtI.setText( nfLocal.format( n ) );
jtD.setText( nfSelected.format( n ) );
n = nfSelected.parse( jtD.getText() );
jtP.setText( n.toString() );
}
catch( ParseException pe )
{
JOptionPane.showMessageDialog( this,
pe.getMessage(), "", JOptionPane.ERROR_MESSAGE);
}
} // End actionPerformed
// Window Listener Implementation
public void windowOpened(WindowEvent we) {}
public void windowClosing(WindowEvent we)
{
dispose();
System.exit(0);
}
public void windowClosed(WindowEvent we) {}
public void windowIconified(WindowEvent we) {}
public void windowDeiconified(WindowEvent we) {}
public void windowActivated(WindowEvent we) {}
public void windowDeactivated(WindowEvent we) {}
// End Window Listener Implementation
public static void main(String[] args)
{
new JIBNumberGUI();
} // end main
} // end class JIBNumberGUI |
JIBCurrencyGUI.java: CurrencyFormat example
JIBCurrencyGUI.java (Part 1)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
public class JIBCurrencyGUI extends JFrame
implements ActionListener,
ItemListener,
WindowListener
{
boolean bRequireSymbol = true,
bToggleFlag = true;
Font fLucida;
int iSelIndex = 0;
JButton jbOK = new JButton( "OK" ),
jbToggle = new JButton(
"Toggle Display Names" );
JCheckBox jchkb = new JCheckBox(
"Require Symbol");
JComboBox jcb = null;
JPanel jpNorth = new JPanel(),
jpCenter = new JPanel(),
jpSouth = new JPanel();
JTextField jtI = new JTextField( 10 ),
jtD = new JTextField( 10 ),
jtP = new JTextField( 10 );
Locale lDefault = Locale.getDefault();
Locale[] alSupported;
NumberFormat cfLocal = NumberFormat.getCurrencyInstance(),
cfSelected,
nfLocal = NumberFormat.getInstance();
String[] asDNames,
asLDNames;
String sCurSymbol = "";
public JIBCurrencyGUI()
{
int i;
String s1 = null;
setTitle( "JIBCurrencyGUI");
addWindowListener( this );
Font fJCB = jbToggle.getFont();
fLucida = new Font("Lucida Sans",
fJCB.getStyle(),
fJCB.getSize() );
alSupported = Locale.getAvailableLocales();
asDNames = new String[ alSupported.length ];
asLDNames = new String[ alSupported.length ];
for( i = 0; i < alSupported.length; i++ )
{
asDNames[i] =
alSupported[i].getDisplayName();
s1 =
alSupported[i].getDisplayName( alSupported[i] );
if( fLucida.canDisplay( s1.charAt( 0 ) ) )
{ asLDNames[i] = s1; }
else
{ asLDNames[i] = s1 + " - font can't display."; }
if( iSelIndex == 0 &&
lDefault.equals( alSupported[i] ) )
{ iSelIndex = i; }
} // end for
toggleDisplayNames();
jtI.setText( cfLocal.format( 150.75 ) );
// cause ActionPerformed event
jcb.setSelectedIndex( iSelIndex );
jbOK.addActionListener( this );
jbToggle.addActionListener( this );
jchkb.setSelected(true);
jchkb.addItemListener( this );
jtD.setEditable( false );
jtD.setFont( fLucida );
jtP.setEditable( false );
jtP.setFont( fLucida );
jpNorth.add(new JLabel("Input:"));
jpNorth.add(jtI);
jpNorth.add(jbOK);
jpNorth.add(jchkb);
JLabel jlTemp = new JLabel("Default = " +
lDefault.getDisplayName() );
jlTemp.setFont( fLucida );
jpNorth.add( jlTemp );
jpCenter.add(new JLabel("Display:"));
jpCenter.add(jtD);
jpCenter.add(new JLabel("Parsed:"));
jpCenter.add(jtP);
Container cp = getContentPane();
cp.add( jpNorth, BorderLayout.NORTH );
cp.add( jpCenter, BorderLayout.CENTER );
cp.add( jpSouth, BorderLayout.SOUTH );
pack();
show();
if( cfLocal instanceof DecimalFormat )
{
DecimalFormatSymbols dfs =
((DecimalFormat)cfLocal).getDecimalFormatSymbols();
sCurSymbol = dfs.getCurrencySymbol();
char chMDS = dfs.getMonetaryDecimalSeparator();
if( chMDS != dfs.getDecimalSeparator() )
{
dfs.setDecimalSeparator( chMDS );
}
if( nfLocal instanceof DecimalFormat )
{
((DecimalFormat)nfLocal).setDecimalFormatSymbols(
dfs );
}
else
{ jchkb.setEnabled( false ); }
} // end if cfLocal instanceof DecimalFormat
else
{ jchkb.setEnabled( false ); }
} // end constructor
|
JIBCurrencyGUI.java (Part 2)
public void toggleDisplayNames()
{
boolean bjcbExist = false;
if( jcb != null )
{
jpSouth.remove( jcb );
jpSouth.remove( jbToggle );
iSelIndex = jcb.getSelectedIndex();
bjcbExist = true;
}
if( bToggleFlag )
{ jcb = new JComboBox( asDNames ); }
else
{ jcb = new JComboBox( asLDNames ); }
bToggleFlag = !bToggleFlag;
if( bjcbExist )
{ jcb.setSelectedIndex( iSelIndex ); }
jcb.setFont( fLucida );
jcb.addActionListener( this );
jpSouth.add( jcb );
jpSouth.add( jbToggle );
} // end toggleDisplayNames
// ActionListener Implementation
public void actionPerformed(ActionEvent ae)
{
Object oSource = ae.getSource();
Number n = null;
String sText = jtI.getText();
if( oSource == jbToggle )
{
toggleDisplayNames();
pack();
return;
}
if( oSource == jcb )
{
cfSelected = NumberFormat.getCurrencyInstance(
alSupported[ jcb.getSelectedIndex() ] );
} // end if jcb, continue on
jtD.setText( "" );
jtP.setText( "" );
try
{
if( bRequireSymbol )
{
n = cfLocal.parse( sText );
}
else
{ // currency symbol may still be present, check
if( sText.indexOf( sCurSymbol ) == -1 )
{
n = nfLocal.parse( sText );
}
else
{
n = cfLocal.parse( sText );
}
}
jtI.setText( cfLocal.format( n ) );
jtD.setText( cfSelected.format( n ) );
n = cfSelected.parse( jtD.getText() );
jtP.setText( n.toString() );
}
catch( ParseException pe )
{
JOptionPane.showMessageDialog( this,
pe.getMessage(), "", JOptionPane.ERROR_MESSAGE);
}
} // End actionPerformed
// ItemListener Implementation
public void itemStateChanged(ItemEvent ie)
{
bRequireSymbol = !bRequireSymbol;
} // End itemStateChanged
// Window Listener Implementation
public void windowOpened(WindowEvent we) {}
public void windowClosing(WindowEvent we)
{
dispose();
System.exit(0);
}
public void windowClosed(WindowEvent we) {}
public void windowIconified(WindowEvent we) {}
public void windowDeiconified(WindowEvent we) {}
public void windowActivated(WindowEvent we) {}
public void windowDeactivated(WindowEvent we) {}
// End Window Listener Implementation
public static void main(String[] args)
{
new JIBCurrencyGUI();
} // end main
} // end class JIBCurrencyGUI |
JIBSEM.java: Sanitation Engineer Maintenance example
JIBSEMrb.properties
# Default properties in English # Label for Title title=Sanitation Engineer Maintenance # Engineer Table Column 1 Title and Employee label Engineer=Engineer # Name Table Column 2 Title Name=Name # Label for Edit Edit=Edit # Label for Current Current=Current # Label for Salary input field Salary=Salary: # Label for Hire Date input field Date=Date: # Label for Tons input field Tons=Tons: |
JIBSEMrb_de.properties
# Default properties in German # Engineer Table Column 1 Title and Employee label Engineer=Ingenieur # Name Table Column 2 Title Name=Name # Label for Edit Edit=Bearbeiten # Label for Current Current=Aktuell # Label for Salary input field Salary=Löhne: # Label for Hire Date input field Date=Date: # Label for Tons input field Tons=Tonne: |
JIBSEMrb_fr.properties
# Default properties in French # Engineer Table Column 1 Title and Employee label Engineer=Ing譩eur # Name Table Column 2 Title Name=Nom # Label for Edit Edit=Editer # Label for Current Current=Actuel # Label for Salary input field Salary=Salaire: # Label for Hire Date input field Date=Date: # Label for Tons input field Tons=Tonne: |
JIBSEMrb_ru.properties
# Default properties in Russian # Engineer Table Column 1 Title and Employee label Engineer=\u0418\u043D\u0436\u0435\u043D\u0435\u0440 # Name Table Column 2 Title Name=\u0418\u043C\u044F # Label for Edit Edit=\u0420\u0435\u0434\u0430\u043A\u0442\u043E\u0440 # Label for Current Current=\u0422\u0435\u043A\u0443\u0449\u0438\u0439 # Label for Salary input field Salary=\u041E\u043A\u043B\u0430\u0434: # Label for Hire Date input field Date=\u0414\u0430\u0442\u0430: # Label for Tons input field Tons=\u0422\u043E\u043D\u043D\u044b: |
JIBSEM.java (Part 1)
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
public class JIBSEM extends JFrame
implements ActionListener,
FocusListener,
ListSelectionListener,
WindowListener
{
DateFormat[] aDF;
DateFormat dfSelected;
Font fLucida,
fLucidaNormal,
fLucidaTitle;
int iRowIndex = 0;
JIBSEMATM ATM;
JIBSEMRow[] aRows = new JIBSEMRow[4];
JButton jbOK = new JButton( "OK" );
JLabel jlCI = new JLabel(),
jlCurrent = new JLabel(
"",
SwingConstants.CENTER),
jlDI = new JLabel(),
jlE = new JLabel(),
jlEID = new JLabel(),
jlEName = new JLabel(),
jlEdit = new JLabel(
"",
SwingConstants.CENTER),
jlLName = new JLabel(),
jlNI = new JLabel(),
jlNow,
jlTitle = new JLabel();
JPanel jpNorth = new JPanel(
new BorderLayout() ),
jpNorthFlow = new JPanel(),
jpCenter = new JPanel(
new BorderLayout() ),
jpCenterNorth = new JPanel(),
jpCenterSouth = new JPanel(),
jpCenterSouthNest = new JPanel(
new GridLayout(0,3) ),
jpSouth = new JPanel();
JScrollPane jsp;
JTable jtbl;
JTextField jtCD = new JTextField( 9 ),
jtCI = new JTextField( 9 ),
jtDD = new JTextField( 9 ),
jtDI = new JTextField( 9 ),
jtND = new JTextField( 9 ),
jtNI = new JTextField( 9 );
Locale lDefault = Locale.getDefault();
Locale[] alSupported = {
Locale.US,
Locale.FRANCE,
Locale.GERMANY,
new Locale( "ru", "RU" )
};
NumberFormat[] aCF,
aNF;
NumberFormat cfSelected,
nfSelected;
Object[][] aoTableData = new Object[4][2];
ResourceBundle rb;
String[] asHeaders = new String[2];
String sRBName = getClass().getName() + "rb";
public JIBSEM()
{
int i = 0;
setTitle( "JIBSEM" );
addWindowListener( this );
Font fJB = jbOK.getFont();
fLucida = new Font("Lucida Sans",
fJB.getStyle(),
fJB.getSize() );
fLucidaNormal = new Font("Lucida Sans",
Font.PLAIN,
fJB.getSize() );
fLucidaTitle = new Font("Lucida Sans",
fJB.getStyle(),
fJB.getSize() + 4 );
aCF = new NumberFormat[ alSupported.length ];
aDF = new DateFormat[ alSupported.length ];
aNF = new NumberFormat[ alSupported.length ];
boolean bLocaleMatched = false;
Locale lTemp;
for( i = 0; i < alSupported.length; i++ )
{
lTemp = alSupported[i];
aCF[i] = NumberFormat.getCurrencyInstance(
lTemp );
aDF[i] = DateFormat.getDateInstance(
DateFormat.SHORT,
lTemp );
aDF[i].setLenient( false );
aNF[i] = NumberFormat.getNumberInstance(
lTemp );
if( lDefault.equals( lTemp ) )
{
bLocaleMatched = true;
}
} // end for
if( !bLocaleMatched ) { lDefault = Locale.US; }
jlNow = new JLabel( DateFormat.getDateInstance(
DateFormat.FULL, lDefault ).format( new Date(
System.currentTimeMillis() )) +
" Default = " +
lDefault.getDisplayName(),
SwingConstants.CENTER );
loadFromResourceBundle(); // get localized labels
jbOK.addActionListener( this );
jlTitle.setFont( fLucidaTitle );
jlNow.setFont( fLucidaNormal );
jlTitle.setHorizontalAlignment( SwingConstants.CENTER );
jlCI.setFont( fLucida );
jlDI.setFont( fLucida );
jlCurrent.setFont( fLucida );
jlE.setFont( fLucida );
jlE.setText( asHeaders[0] + ":" );
jlEdit.setFont( fLucida );
jlEID.setFont( fLucida );
jlEID.setForeground( Color.black );
jlEName.setFont( fLucida );
jlLName.setFont( fLucidaNormal );
jlEName.setForeground( Color.black );
jlNI.setFont( fLucida );
jtCD.addFocusListener(this);
jtCD.setForeground( Color.green.darker().darker() );
jtCD.setHorizontalAlignment( JTextField.RIGHT );
jtCI.setHorizontalAlignment( JTextField.RIGHT );
jtDD.addFocusListener(this);
jtDD.setForeground( Color.green.darker().darker() );
jtDD.setHorizontalAlignment( JTextField.RIGHT );
jtDI.setHorizontalAlignment( JTextField.RIGHT );
jtND.addFocusListener(this);
jtND.setForeground( Color.green.darker().darker() );
jtND.setHorizontalAlignment( JTextField.RIGHT );
jtNI.setHorizontalAlignment( JTextField.RIGHT );
loadData();
ATM = new JIBSEMATM( aoTableData, asHeaders );
jtbl = new JTable( ATM );
jtbl.setFont( fLucidaNormal );
jtbl.getTableHeader().setFont( fLucidaNormal );
Dimension dim = jtCI.getPreferredSize();
TableColumnModel tcm = jtbl.getColumnModel();
TableColumn tc = tcm.getColumn(0);
dim.width = tc.getPreferredWidth();
tc = tcm.getColumn(1);
i = tc.getPreferredWidth() * 3;
tc.setPreferredWidth(i);
dim.width += i;
dim.height = jtbl.getRowHeight() * 4;
jtbl.setPreferredScrollableViewportSize( dim );
jtbl.setSelectionMode(
ListSelectionModel.SINGLE_SELECTION );
jtbl.getSelectionModel().
addListSelectionListener( this );
jtbl.setRowSelectionInterval(0, 0);
jsp = new JScrollPane(jtbl);
jpNorth.add( jlTitle, BorderLayout.NORTH );
jpNorth.add(jlNow, BorderLayout.CENTER );
jpNorthFlow.add( jsp );
jpNorth.add( jpNorthFlow, BorderLayout.SOUTH );
jpCenterNorth.add( jlE );
jpCenterNorth.add( jlEID );
jpCenterNorth.add(new JLabel(" "));
jpCenterNorth.add( jlEName );
jpCenterNorth.add( jlLName );
jpCenterSouthNest.add(new JLabel(" "));
jpCenterSouthNest.add(jlEdit);
jpCenterSouthNest.add(jlCurrent);
jpCenterSouthNest.add(jlCI);
jpCenterSouthNest.add(jtCI);
jpCenterSouthNest.add(jtCD);
jpCenterSouthNest.add(jlDI);
jpCenterSouthNest.add(jtDI);
jpCenterSouthNest.add(jtDD);
jpCenterSouthNest.add(jlNI);
jpCenterSouthNest.add(jtNI);
jpCenterSouthNest.add(jtND);
jpCenterSouth.add(jpCenterSouthNest);
jpCenter.add(jpCenterNorth, BorderLayout.NORTH);
jpCenter.add(jpCenterSouth, BorderLayout.SOUTH);
jpSouth.add(jbOK);
Container cp = getContentPane();
cp.add( jpNorth, BorderLayout.NORTH );
cp.add( jpCenter, BorderLayout.CENTER );
cp.add( jpSouth, BorderLayout.SOUTH );
pack();
show();
} // end constructor
|
JIBSEM.java (Part 2)
public String formatCurrency( double dSalary )
{
cfSelected = aCF[iRowIndex];
return cfSelected.format( dSalary );
} // end formatCurrency
public String formatDate( java.util.Date d )
{
dfSelected = aDF[iRowIndex];
return dfSelected.format( d );
} // end formatDate
public String formatNumber( double dTonnage )
{
nfSelected = aNF[iRowIndex];
return nfSelected.format( dTonnage );
} // end formatDate
public void loadData()
{
aRows[0] = new JIBSEMRow(
12345, "Annie Oakley",
50000.00f, java.sql.Date.valueOf("1998-05-19"),
25000.5, Locale.US );
aRows[1] = new JIBSEMRow(
22345, "Jeanne d'Arc",
379077.5f, java.sql.Date.valueOf("1999-06-15"),
25000.5, Locale.FRANCE );
aRows[2] = new JIBSEMRow(
32345, "Ludi Beethoven",
113027.5f, java.sql.Date.valueOf("2000-12-01"),
25000.5, Locale.GERMANY );
aRows[3] = new JIBSEMRow(
42345, "\u0414\u044f\u0434\u044f " +
"\u0412\u0430\u043D\u044F",
1551500f, java.sql.Date.valueOf("2001-03-15"),
25000.5, new Locale( "ru", "RU") );
for( int i = 0; i < aRows.length; i++ )
{
aoTableData[i][0] = new Integer(aRows[i].getID());
aoTableData[i][1] = aRows[i].getName();
}
} // end loadData
public void loadFromResourceBundle()
{
try
{ // get the PropertyResourceBundle
rb = ResourceBundle.getBundle( sRBName,
getLocale() );
// get data associated with keys
jlTitle.setText( rb.getString( "title" ));
asHeaders[0] = rb.getString( "Engineer" );
asHeaders[1] = rb.getString( "Name" );
jlE.setText( asHeaders[0] + ":" );
jlEdit.setText( rb.getString( "Edit" ));
jlCurrent.setText( rb.getString( "Current" ));
jlCI.setText( rb.getString( "Salary" ));
jlDI.setText( rb.getString( "Date" ));
jlNI.setText( rb.getString( "Tons" ));
} // end try
catch( MissingResourceException mre )
{
JOptionPane.showMessageDialog( this,
"ResourceBundle problem;\n" +
"Specific error: " + mre.getMessage(),
"", JOptionPane.ERROR_MESSAGE);
}
} // end loadFromResourceBundle
// ActionListener Implementation
public void actionPerformed(ActionEvent ae)
{
Object oSource = ae.getSource();
boolean bError = false;
java.util.Date d = null;
Number n = null,
nCur = null;
try
{
nCur = cfSelected.parse( jtCI.getText() );
d = dfSelected.parse( jtDI.getText() );
n = nfSelected.parse( jtNI.getText() );
}
catch( ParseException pe )
{
JOptionPane.showMessageDialog( this,
pe.getMessage(), "", JOptionPane.ERROR_MESSAGE);
bError = true;
}
if( bError == false )
{
aRows[iRowIndex].setSalary( nCur.floatValue() );
jtCD.setText( jtCI.getText() );
aRows[iRowIndex].setHireDate( d );
jtDD.setText( jtDI.getText() );
aRows[iRowIndex].setTonnage( n.doubleValue() );
jtND.setText( jtDI.getText() );
jtbl.requestFocus();
}
} // End actionPerformed
// FocusListener Implementation
public void focusGained(FocusEvent fe)
{
((Component)fe.getSource()).transferFocus();
} // End focusGained
public void focusLost(FocusEvent fe) {}
// ListSelectionListener Implementation
public void valueChanged(ListSelectionEvent lse)
{
if( lse.getValueIsAdjusting() ) { return; }
iRowIndex = jtbl.getSelectedRow();
JIBSEMRow row = aRows[iRowIndex];
jlEID.setText( "" + row.getID() );
jlEName.setText(row.getName());
jlLName.setText(row.getLocale().getDisplayName());
cfSelected = aCF[iRowIndex];
nfSelected = aNF[iRowIndex];
jtCI.setText( formatCurrency( row.getSalary() ));
jtCD.setText( jtCI.getText());
jtDI.setText( formatDate( row.getHireDate() ));
jtDD.setText( jtDI.getText());
jtNI.setText( formatNumber(row.getTonnage() ));
jtND.setText( jtNI.getText());
} // end valueChanged
// Window Listener Implementation
public void windowOpened(WindowEvent we) {}
public void windowClosing(WindowEvent we)
{
dispose();
System.exit(0);
}
public void windowClosed(WindowEvent we) {}
public void windowIconified(WindowEvent we) {}
public void windowDeiconified(WindowEvent we) {}
public void windowActivated(WindowEvent we) {}
public void windowDeactivated(WindowEvent we) {}
// End Window Listener Implementation
public static void main(String[] args)
{
new JIBSEM();
} // end main
} // end class JIBSEM |
JIBSEMATM.java
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
public class JIBSEMATM extends AbstractTableModel
{
Object rowData[][];
Object columnNames[];
public JIBSEMATM( Object[][] oData,
Object[] oCloumns )
{
rowData = oData;
columnNames = oCloumns;
} // end constructor
public String getColumnName(int column)
{
return columnNames[column].toString();
}
public int getRowCount()
{
return rowData.length;
}
public int getColumnCount()
{
return columnNames.length;
}
public Object getValueAt(int row, int col)
{
return rowData[row][col];
}
} // end class JIBSEMATM
|
JIBSEMRow.java
import java.util.*;
public class JIBSEMRow
{
// Employee ID
private int ID;
// Employee name
private String Name;
// Salary
private float Salary;
// Hire Date
private Date HireDate;
// Responsible Tonnage
private double Tonnage;
// Locale
private Locale Locale;
public JIBSEMRow( int iID, String sName,
float fSalary, Date dHireDate,
double dTonnage, Locale lLocale )
{
ID = iID;
Name = sName;
Salary = fSalary;
HireDate = dHireDate;
Tonnage = dTonnage;
this.Locale = lLocale;
} // end constructor
public int getID()
{
return ID;
}
public String getName()
{
return Name;
}
public float getSalary()
{
return Salary;
}
public Date getHireDate()
{
return HireDate;
}
public double getTonnage()
{
return Tonnage;
}
public java.util.Locale getLocale()
{
return this.Locale;
}
public void setID( int iID )
{
ID = iID;
}
public void setName( String sName )
{
Name = sName;
}
public void setSalary( float fSalary )
{
Salary = fSalary;
}
public void setHireDate( Date dHireDate )
{
HireDate = dHireDate;
}
public void setTonnage( double dTonnage )
{
Tonnage = dTonnage;
}
public void setLocale( java.util.Locale lLocale )
{
this.Locale = lLocale;
}
public void report()
{
System.out.println( "JIBSEMRow reporting: " );
System.out.println( toString() );
}
public String toString()
{
return ( "ID is: " + ID +
", Name is: " + Name +
", Salary is: " + Salary +
", HireDate is : " + HireDate +
", Tonnage is : " + Tonnage +
", Locale is: " + this.Locale );
}
} // end class JIBSEMRow
|


