"Solved ish - if anyone knows how to stop the JFileChooser from closing after 5 seconds please help..."
Hi, I tried to do an example from a book, however for some odd reason the JFileChooser closes with an error... I searched on google but could not find any help...
Code:
And the error:Code:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class Main {
JLabel jlabMsg;
JTextArea jta;
JFileChooser jfc;
JTextField jtfFName;
JTextField jtfFind;
JButton jbtnSave;
JButton jbtnLoad;
JButton jbtnFind;
JButton jbtnFindNext;
int findIdx;
public Main() {
// Create a new JFrame container.
JFrame jfrm = new JFrame("A Simple Text Editor");
// Specify FlowLayout for the layout manager.
jfrm.getContentPane().setLayout(new FlowLayout());
// Give the frame an initial size.
jfrm.setSize(270, 420);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfc = new JFileChooser();
JMenuBar menu = new JMenuBar();
JMenu menuFile = new JMenu("File");
JMenu menuEdit = new JMenu("Edit");
JMenu menuHelp = new JMenu("Help");
JMenuItem menuItemSave = new JMenuItem("Save");
JMenuItem menuItemAbout = new JMenuItem("About");
JMenuItem menuItemSelectall = new JMenuItem("Select All");
JMenuItem menuItemOpen = new JMenuItem("Open");
JMenuItem menuItemFind = new JMenuItem("Find");
menuHelp.add(menuItemAbout);
menuEdit.add(menuItemSelectall);
menuFile.add(menuItemSave);
menuFile.add(menuItemOpen);
menuEdit.add(menuItemFind);
menu.add(menuFile);
menu.add(menuEdit);
menu.add(menuHelp);
menuItemSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
save();
}
});
menuItemOpen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
load();
}
});
jfrm.add(menu);
//menuFile.setToolTipText("YAY");
// Create the message label.
jlabMsg = new JLabel();
jlabMsg.setPreferredSize(new Dimension(200, 30));
jlabMsg.setHorizontalAlignment(SwingConstants.CENTER);
// Create an empty label to add space.
JLabel jlabSeparator = new JLabel();
jlabSeparator.setPreferredSize(new Dimension(200, 30));
// Create the Seach For and Filename labels.
JLabel jlabFind = new JLabel("Search For:");
jlabFind.setPreferredSize(new Dimension(70, 20));
jlabFind.setHorizontalAlignment(SwingConstants.RIGHT);
JLabel jlabFilename = new JLabel("Filename:");
jlabFilename.setPreferredSize(new Dimension(70, 20));
jlabFilename.setHorizontalAlignment(SwingConstants.RIGHT);
// Create the text field.
jta = new JTextArea();
jta.setBackground(Color.YELLOW);
// Put the text area into a scroll pane.
JScrollPane jscrlp = new JScrollPane(jta);
jscrlp.setPreferredSize(new Dimension(250, 200));
// Create text field for filename.
jtfFName = new JTextField(15);
// Add a caret listener for the text area. This
// handler displays number of characters in the
// file. It is updated with each caret change.
// The findIdx variable is also set to the current
// caret loction.
jta.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent ce) {
String str = jta.getText();
jlabMsg.setText("Current size: " + str.length());
findIdx = jta.getCaretPosition();
}
});
// Create the Save and Load buttons.
jbtnSave = new JButton("Save File");
jbtnLoad = new JButton("Load File");
// Add action listener for the Save button.
jbtnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
save();
}
});
// Add action listener for the Load button.
jbtnLoad.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
load();
}
});
// Create the Search For text field.
jtfFind = new JTextField(15);
// Create the Find From Top and Find Next buttons.
jbtnFind = new JButton("Find From Top");
jbtnFindNext = new JButton("Find Next");
// Add action listener for the Find button.
jbtnFind.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
findIdx = 0;
find(findIdx);
}
});
// Add action listener for the Find Next button.
jbtnFindNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
find(findIdx+1);
}
});
// Add the components to the content pane.
Container cp = jfrm.getContentPane();
cp.add(jscrlp);
cp.add(jlabFind);
cp.add(jtfFind);
cp.add(jbtnFind);
cp.add(jbtnFindNext);
cp.add(jlabSeparator);
cp.add(jlabFilename);
cp.add(jtfFName);
// cp.add(jbtnSave);
// cp.add(jbtnLoad);
cp.add(jlabMsg);
// Display the frame.
jfrm.setVisible(true);
}
// Save the file.
void save() {
FileWriter fw;
// Get the filename from the text field.
String fname = jtfFName.getText();
// Make sure that there is actually a filename present.
if(fname.length() == 0) {
jlabMsg.setText("No filename present.");
return;
}
// Save the file.
try {
fw = new FileWriter(fname);
jta.write(fw);
fw.close();
} catch(IOException exc) {
jlabMsg.setText("Error opening or writing file.");
return;
}
jlabMsg.setText("File written sucessfully.");
}
// Load the file.
void load() {
int result = jfc.showOpenDialog(null);
// try {
// Thread.sleep(2222222);
//} catch (InterruptedException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
//}
FileReader fw;
// Get the filename from the text field.
//String fname = jtfFName.getText();
String fname = jfc.getSelectedFile().getName();
fname = "notes.txt";
// Make sure that there is actually a filename present.
if(fname.length() == 0) {
jlabMsg.setText("No filename present.");
return;
}
// Load the file.
try {
//showOpenDialog();
fw = new FileReader(fname);
jta.read(fw, null);
fw.close();
} catch(IOException exc) {
jlabMsg.setText("Error opening or reading file.");
return;
}
// Reset find index when a new file is loaded.
findIdx = 0;
jlabMsg.setText("File loaded successfully.");
}
// Search the file.
void find(int start) {
// Get the current text as a string.
String str = jta.getText();
str = str.toLowerCase();
// Get the string to find.
String findStr = jtfFind.getText();
findStr = findStr.toLowerCase();
// Beginning at start, find the first
// occureance of the specified string.
int idx = str.indexOf(findStr, start);
// See if there is a match.
if(idx > -1) {
// If found, set focus to text area
// and move caret to the location.
jta.setCaretPosition(idx);
findIdx = idx; // update the find index
jlabMsg.setText("String found.");
}
else
jlabMsg.setText("String not found.");
// Set the focus to the editor window.
jta.requestFocusInWindow();
}
public static void main(String args[]) {
// Create the frame on the event dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Main();
}
});
}
}
If anyone can help... :confused:Code:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Main.load(Main.java:221)
at Main$2.actionPerformed(Main.java:72)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2015)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2338)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
at java.awt.Component.processMouseEvent(Component.java:6434)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6199)
at java.awt.Container.processEvent(Container.java:2203)
at java.awt.Component.dispatchEventImpl(Component.java:4790)
at java.awt.Container.dispatchEventImpl(Container.java:2261)
at java.awt.Component.dispatchEvent(Component.java:4616)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4803)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4463)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4393)
at java.awt.Container.dispatchEventImpl(Container.java:2247)
at java.awt.Window.dispatchEventImpl(Window.java:2671)
at java.awt.Component.dispatchEvent(Component.java:4616)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:662)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

