-
Bug
-
Resolution: Fixed
-
P3
-
1.1.6, 1.2.0
-
b01
-
x86
-
windows_nt
Name: el35337 Date: 06/09/98
We had compiled the source code (see below)
for a file viewer; the frame appeared, but when
we selected a file and pressed "Open" in the
corresponding file dialog, there was a Windows
error message in java.exe, referring to a memory
location:
Memory cannot be "read"
(after having printed the address).
CODE:
/* FileViewer.java
* A program to view files in a scrollable text area.
*/
import java.io.*;
import java.awt.*;
import java.net.*;
import java.util.*;
import java.lang.*;
import java.awt.event.*;
/* Use the following five imports if you're using Swing as a separate API*/
import com.sun.java.swing.*;
import com.sun.java.swing.tree.*;
import com.sun.java.swing.border.*;
import com.sun.java.swing.event.*;
import com.sun.java.swing.text.*;
/*The class FileViewer is an extension of the Swing class JPanel. */
public class FileViewer extends JPanel{
protected JScrollPane scrollpane;
protected JMenuBar mainmenu;
protected JMenu filemenu,window;
protected JMenuItem open,exit,close,viewer;
protected JTextArea textArea;
protected JComboBox fileSelector;
protected FileSelectorListener fileSelectorListener;
private static int lastSessionViewer = 0;
/*Constructor for the FileViewer class */
public FileViewer() {
/* Call the superclass constructor */
super();
/* Set the layout manager */
setLayout(new BorderLayout());
/*setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));*/
/*Set up the contents of the panel here */
mainmenu = new JMenuBar();
/*The File menu */
filemenu = new JMenu("File");
/*Open option in File Menu */
open = new JMenuItem("Open");
open.setActionCommand("open");
open.addActionListener(new OpenActionListener());
filemenu.add(open);
/*Exit option in the File Menu */
exit = new JMenuItem("Exit");
exit.addActionListener(new ExitActionListener());
filemenu.add(exit);
close = new JMenuItem("Close");
/*Add filemenu to the mainmenu bar*/
mainmenu.add(filemenu);
/*Window Menu*/
window = new JMenu("Window");
/*Viewer Option */
viewer = new JMenuItem("Viewer");
viewer.setActionCommand("viewer");
viewer.addActionListener(new ViewerActionListener());
window.add(viewer);
/*Add window menu to mainmenu */
mainmenu.add(window);
/* Add ScrollPanel to the window */
textArea = new JTextArea();
textArea.setFont( new Font("MonoSpaced",Font.PLAIN,12));
//We only implement a file viewer NOT a file editor
textArea.setEditable(false); // Rem to CHANGE TO FALSE
JScrollPane scrollpane = new
JScrollPane(textArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConsta
nts.HORIZONTAL_SCROLLBAR_AS_NEEDED);
fileSelector = new JComboBox();
fileSelector.setMaximumRowCount(5);
fileSelectorListener = new FileSelectorListener();
fileSelector.addItemListener(fileSelectorListener);
this.add("North",mainmenu);
this.add("Center",scrollpane);
this.add("South",fileSelector);
}
/** getFrame() returns the parent frame of this file viewer panel */
protected JFrame getFrame() {
for (Container p = getParent(); p != null; p = p.getParent()) {
if (p instanceof JFrame) {
return (JFrame) p;
}
}
return null;
}
/** Event listener class for the exit menu item */
class ExitActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e){
JFrame parent = getFrame();
lastSessionViewer -= 1;
if (lastSessionViewer == 0) {
System.exit(0);
}
else {
parent.setVisible(false);
// parent.dispose();
}
}
}
/** Event listener class for the viewer menu item */
class ViewerActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e){
createFileViewerFrame();
}
}
/** Event listener class for the open menu item */
class OpenActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFrame frame = getFrame();
FileDialog fileDialog ;
File f;
fileDialog = new FileDialog(frame,"Open Dialogue",FileDialog.LOAD)
;
fileDialog.setVisible(true);
/* File dialog handling goes here */
String dir = fileDialog.getDirectory();
String file = fileDialog.getFile();
f = new File(dir,file);
System.out.println("Just got the file");
fileDialog.dispose();
System.out.println("File Dialog disposed");
Thread loader = new FileLoader(f);
System.out.println("loader initialised");
System.out.println("Filename :" + dir+ file);
loader.start();
}
}
/** Event listener class for the file selector (combo box) */
public class FileSelectorListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
String s = (String) fileSelector.getSelectedItem();
File f = new File(s);
Thread loader = new FileLoader(f);
loader.start();
}
}
/** FileLoader is a thread class to load in a text file, put
its contents in a text area and update the file selector */
class FileLoader extends Thread {
File f;
FileLoader(File f) {
setPriority(4);
this.f = f;
}
public void run() {
if (f != null && f.isFile()) {
int len = (int) f.length();
if (len > 50000) {
System.out.println("File too large.\n");
return;
}
try {
Reader in = new FileReader(f);
char[] loadspace = new char[len];
int nch = in.read(loadspace, 0, len);
if (nch == -1) {
System.out.println("Could not read file.\n");
return;
}
textArea.setText(new String(loadspace, 0, nch));
int i;
boolean found = false;
String s = f.toString();
for (i = 0; i < fileSelector.getItemCount(); i++)
if (fileSelector.getItemAt(i).equals(s))
found = true;
System.out.println("In FileLoader");
System.out.println("Was file lastly opened found :" +
found);
fileSelector.removeItemListener(fileSelectorListener);
if (!found){
fileSelector.addItem(s);
System.out.println("Item Added");
}
fileSelector.setSelectedItem(s);
fileSelector.addItemListener(fileSelectorListener);
}
catch (Exception ex) {
System.out.println(ex);
}
}
}
}
/*createFileViewerFrame() creates a frame, adds a new
* file viewer panel to it, sets its size and shows it */
static void createFileViewerFrame(){
JFrame fr = new JFrame("Viewer");
FileViewer f = new FileViewer();
fr.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
lastSessionViewer -=1;
if (lastSessionViewer == 0) {
System.exit(0);
}
}
}
);
fr.getContentPane().add("Center",f);
fr.pack();
fr.setBounds(new Rectangle(800,600));
//ADD THE THING WITH RECTANGLE HERE
fr.setVisible(true);
lastSessionViewer += 1;
}
/** Main function, called on execution of the "java" command */
public static void main(String argv[]){
createFileViewerFrame();
}
}
***************************
The application stopped working at that point.
NOTICE that the problem disappeared with JDK 1.1.5
(Review ID: 33251)
======================================================================
- duplicates
-
JDK-4152546 Class Reader, method in() crashes when JIT enabled
-
- Closed
-
-
JDK-4156314 Notepad crashes opening text doc with swing1.1beta and JDK1.1.6 on Windows NT
-
- Closed
-