new JFrames cascading?

In my program, I would like for the new JFrames to appear cascading on top of another (like Microsoft Word does when you click New File when there's a blank document). Is there any way to do this? I usually set my JFrames like this:

JFrame frame =new JFrame();

frame.setLocationRelativeTo(null);

to get the frames in the center of the screen.

Thanks! :)

[452 byte] By [keseldudea] at [2007-11-27 7:33:59]
# 1

You can manually do that:

JInternalFrame[] frames = desktopPane.getAllFrames();

int x = 0;

int y=0;

for (int i=0;i<frames.length;i++){

frame.setLocation( x + (10*i) , y + (10*i) );

}

you can use the same with the JFrame.

Message was edited by:

Ahmad_qauod>

Ahmad_qauoda at 2007-7-12 19:14:22 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thank you for the quick response!EDIT: Wouldn't that redraw the positions of the frames already there? I want it to cascade relative to where the previous frame is.Message was edited by: keseldude
keseldudea at 2007-7-12 19:14:23 > top of Java-index,Desktop,Core GUI APIs...
# 3
public static int frameCount = 0;...int loc = (frameCount * 20);frameCount++;if(frameCount > 10) frameCount = 0; // resetframe.setLocation(loc, loc);
bsampieria at 2007-7-12 19:14:23 > top of Java-index,Desktop,Core GUI APIs...
# 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?

keseldudea at 2007-7-12 19:14:23 > top of Java-index,Desktop,Core GUI APIs...