Java Reference
In-Depth Information
before calling nextInt()
after calling nextInt()
23 56 84
FIGURE 13.2:
Moving the file cursor.
import
java . io .
∗
;
import
java . util .
∗
;
public 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) ;
openMenuItem. addActionListener(
new
ActionListener ()
{
public void
actionPerformed(ActionEvent e)
{
JFileChooser fileChooser =
new
JFileChooser () ;
if
( f i leChooser . showDialog (NotepadFrame .
this
,
"Open"
)==
JFileChooser .APPROVEOPTION)
{
File newFile = fileChooser . getSelectedFile() ;
try
(Scanner fileHandler =
new
Scanner(newFile))
{
textArea . setText(
""
);
while
( fileHandler . hasNext () )
{
String line = fileHandler .nextLine() ;
textArea . append( l ine +
"\n"
);
}
}
catch
(Exception exception)
{
}
}
}
}
);