Java Reference
In-Depth Information
}
}
Note the circular reference between the two classes. There is an ArrayList of transac-
tions associated with every bank account. Conversely, every transaction is associated with
a bank account. Since both the classes Transaction and BankAccount are Serializable ,
Java will correctly handle storing objects of both types and every object will be stored just
once in the file.
As a second example of how binary files work, let us revisit our poor man's Notepad
application. This time, however, we will store the font, the point size, and the value of the
bold and italic check boxes in the file together with the text. This way, we will preserve the
formatting of the file. Below is the complete code for the program.
import java .awt .
;
import java .awt. event .
;
import java . io .
;
import javax . swing .
;
public class Notepad {
public static void main(String [] args) {
NotepadFrame f = new NotepadFrame () ;
f . setVisible( true );
}
}
class NotepadFrame extends JFrame
{
private static int WIDTH = 600;
private static int HEIGHT = 600;
public NotepadFrame () {
setDefaultCloseOperation(JFrame.DISPOSEON CLOSE) ;
setSize(WIDTH, HEIGHT);
final JTextArea textArea = new JTextArea () ;
JMenuBar menuBar = new JMenuBar ( ) ;
setJMenuBar(menuBar) ;
JMenu fileMenu = new JMenu( "File" );
menuBar . add( f i leMenu ) ;
JMenuItem openMenuItem = new JMenuItem( "Open..." );
fileMenu . add(openMenuItem) ;
add( new JScrollPane(textArea) , BorderLayout.CENTER) ;
JPanel controlPanel = new JPanel () ;
controlPanel . setLayout( new FlowLayout(FlowLayout .LEFT) ) ;
String [ ] fontNames = GraphicsEnvironment .
getLocalGraphicsEnvironment () .
getAvailableFontFamilyNames () ;
final JComboBox fontComboBox = new JComboBox(fontNames) ;
fontComboBox. setSelectedItem( "SansSerif" );
final JComboBox sizeComboBox = new JComboBox ( ) ;
final JCheckBox boldCheckBox = new JCheckBox( "Bold" );
final JCheckBox italicCheckBox = new JCheckBox( "Italic" );
for ( int i=8;i < = 72; i++) {
sizeComboBox . addItem( i ) ;
}
 
Search WWH ::




Custom Search