# 4
package rs;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class RandomStuff
{
public final static Dimension WINDOW_SIZE = new Dimension(784, 548);
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createGUI();
}
});
}
public static void createGUI()
{
JFrame.setDefaultLookAndFeelDecorated(false);
JFrame frame = new JFrame("RandomStuff");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
RandomPanel rp = new RandomPanel();
frame.add(rp);
frame.setJMenuBar(rp.createMenuBar());
frame.pack();
frame.setSize(RandomStuff.WINDOW_SIZE);
rp.text.requestFocus();
frame.setVisible(true);
}
static class RandomPanel extends JPanel
{
private static final long serialVersionUID = -3656094239001970830;
public ImageIcon newIcon = new ImageIcon("new.png");
public ImageIcon openIcon = new ImageIcon("open.png");
public ImageIcon saveIcon = new ImageIcon("save.png");
public ImageIcon exitIcon = new ImageIcon("exit.png");
public ArrayList<JFrame> ar = new ArrayList<JFrame>();
public static int counter = 0;
private Object[] options =
{ "Yes", "No", "Cancel" };
public JTextArea text;
private String getExtension(File f)
{
String ext = null;
String s = f.getName();
int i = s.lastIndexOf('.');
if (i > 0 && i < s.length() - 1)
{
ext = s.substring(i + 1).toLowerCase();
}
return ext;
}
private FileFilter textFilter = new FileFilter()
{
public String getDescription()
{
return "Text files";
}
public boolean accept(File f)
{
String ext = getExtension(f);
if (ext != null)
{
if (ext.equals("txt"))
return true;
else
return false;
}
return false;
}
};
private ActionListener newAction = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (text.getText().length() != 0)
{
int val = (Integer) JOptionPane.showOptionDialog(null,
"Would you like to save?", "Confirm New Nile",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE, newIcon, options,
options[0]);
if (val == JOptionPane.YES_OPTION)
{
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(textFilter);
int returnVal = chooser.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = chooser.getSelectedFile();
save(file);
}
text.setText("");
}
else if (val == JOptionPane.NO_OPTION)
text.setText("");
else
;
}
else
{
ar.add(new JFrame());
RandomPanel rp = new RandomPanel();
ar.get(counter).add(rp);
ar.get(counter).setJMenuBar(rp.createMenuBar());
ar.get(counter).setSize(RandomStuff.WINDOW_SIZE);
//ar.get(counter).setLocationRelativeTo(null);
int loc = (counter + 1) * 20;
ar.get(counter).setLocation(loc, loc);
ar.get(counter).setVisible(true);
++counter;
}
}
};
private ActionListener openAction = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(textFilter);
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = chooser.getSelectedFile();
try
{
BufferedReader br = new BufferedReader(new FileReader(
file));
text.append("" + br.readLine());
br.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
};
private ActionListener saveAction = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(textFilter);
int returnVal = chooser.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = chooser.getSelectedFile();
save(file);
}
}
};
/*
* Function to save the text in the JTextArea to a given file
*/
private void save(File file)
{
try
{
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
for (int i = 0; i < text.getText().length(); ++i)
bw.append(text.getText().charAt(i));
bw.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
public RandomPanel()
{
createComponents();
}
/*
* Deals with the creation of the components for the Random Panel
*/
private void createComponents()
{
this.setLayout(new BorderLayout());
JPanel buttonBar = createButtonBar();
this.add(buttonBar, BorderLayout.NORTH);
text = new JTextArea();
text.setPreferredSize(new Dimension(this.getWidth(), this
.getHeight()));
text.setMargin(new Insets(2, 4, 2, 4));
text.setLineWrap(true);
text.setWrapStyleWord(true);
this.add(text, BorderLayout.CENTER);
}
/*
* Creates the Button Bar for the Random Panel
*/
private JPanel createButtonBar()
{
JPanel upanel = new JPanel();
upanel.setLayout(new BoxLayout(upanel, BoxLayout.LINE_AXIS));
JButton newButton = new JButton(newIcon);
JButton openButton = new JButton(openIcon);
JButton saveButton = new JButton(saveIcon);
newButton.addActionListener(newAction);
openButton.addActionListener(openAction);
saveButton.addActionListener(saveAction);
upanel.add(newButton);
upanel.add(openButton);
upanel.add(saveButton);
return upanel;
}
/*
* Creates the Menu Bar for the Random Panel
*/
public JMenuBar createMenuBar()
{
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("<html><font color='red'><u>F</u></font>ile</html>");
JMenuItem newMenuItem = new JMenuItem("New", newIcon);
newMenuItem.addActionListener(newAction);
file.add(newMenuItem);
JMenuItem openMenuItem = new JMenuItem("Open", openIcon);
openMenuItem.addActionListener(openAction);
file.add(openMenuItem);
JMenuItem saveMenuItem = new JMenuItem("Save", saveIcon);
saveMenuItem.addActionListener(saveAction);
file.add(saveMenuItem);
JMenuItem exitMenuItem = new JMenuItem("Exit", exitIcon);
exitMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
file.add(exitMenuItem);
menuBar.add(file);
return menuBar;
}
}
}
After pressing New a few times, I keep getting an error that the ArrayList is out of bounds; but then it opens a new window after a few more clicks.
I think it's because when I click New in the new JFrame, it uses a different ArrayList and is therefore out of bounds. How should I fix this?