wood burning stoves
The moose likes Swing / AWT / SWT and the fly likes Save and Open files ( realistically) ?? code included Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Save and Open files ( realistically) ?? code included" Watch "Save and Open files ( realistically) ?? code included" New topic

Save and Open files ( realistically) ?? code included

Avin Sinanan
Ranch Hand

Joined: Nov 07, 2001
Posts: 109
Hello does anyone know how to opena and save files in java. I've included some code. The code already does the following:
1) I creates a menubar and toolbar.
2)The toolbar contains a button.If the button is pressed a button is added to the JPanel. If it is pressed again it adds another button.
3)The buttons can be moves anywhere on the screen using the mouse.
4)The open option on the menubar allows one to view and open all files on ones hardrive in MS-DOS.
Ok this is what I need the code altered to do. I am wondering if anyone can alter the code so that the application does the following :
1)I want the program to work like a real program. Lets say I added 3 buttons to the screen and I positioned them how I want.
I would like the save this configuration by using the save option in the menubar.
2)lets say I exited the program. Then later I can back and I recomplied my program using JCreator again.When the frame opens up there will be no buttons on the screen becasue I haven't added any yet. However I would like it if I went to the menubar and choose Open and I am allowed to open back the program I saved earlier. Its like Microsoft word. You can save and open programs within the same window. How do you do this?
3)If anyone gives the sloution could u please explain it to.. Thanks a million.
here is the code:-->
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.beans.*;
/*----------------------START HERE-------------------------*/
class AvinMenuSaveOpen
public static void main(String args[])
MakeFrame makeFrame = new MakeFrame();

/*---------------------Frame Maker Class---------------------------*/
class MakeFrame
public MakeFrame()
JFrame frame = new JFrame("File Chooser");
JPanel pane = new JPanel();
MenuBarMaker menu = new MenuBarMaker(frame , pane);
ToolBarMaker toolbar = new ToolBarMaker(frame , pane);

frame.setTitle("Main Window");
frame.setSize(500 ,500);

/*----------------------MenuMaker Class------------------------*/
class MenuBarMaker
private JFrame frame;
private JPanel pane;

public MenuBarMaker(JFrame aFrame ,JPanel apane )
frame= aFrame;
pane = apane ;
JMenuBar menubar = new JMenuBar();

JMenu menu1 = new JMenu("File");


JMenuItem item1 = new JMenuItem("Open");
item1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
/* When pressed it opens a Open Window to view all files
on the hardrive */

OpenClass open =new OpenClass();


JMenuItem item2 = new JMenuItem("Save");
item2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//what happens when pressed
MoveableButton move = new MoveableButton(frame , pane);


class ToolBarMaker
private JFrame frame;
private JPanel pane;
public ToolBarMaker(JFrame aFrame , JPanel apane)
frame = aFrame ;
pane = apane;
JToolBar toolbar = new JToolBar(JToolBar.HORIZONTAL);
JButton button = new JButton("Click to add Button to JPanel");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){

MoveableButton move = new MoveableButton(frame,pane);


frame.getContentPane().add("North", toolbar);
/* this class makes the added button moveable!!!*/
class MoveableButton
private int xOffset,yOffset,x1,y1 ;
private static int i =0 ;
private JButton button1 ;
private JFrame frame;
private JPanel pane;
public MoveableButton(JFrame aframe , JPanel apane)
frame = aframe;
pane = apane;
button1 = new JButton("" + i);
button1.addMouseListener(new MouseListener(){
public void mousePressed(MouseEvent e)
xOffset = e.getX();
yOffset = e.getY();
public void mouseClicked(MouseEvent e){}
public void mouseReleased(MouseEvent e){}

public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
button1.addMouseMotionListener(new MouseMotionAdapter() {
public void mousePressed(MouseEvent mee) {
// store cursor-position relative to active component
public void mouseDragged(MouseEvent mee) {
/*Minus the offset so that the curser does not jump to the
upper left hand side of button when dragging it. However it does not work*/
x1 = mee.getComponent().getX() + mee.getX() - xOffset;
y1 = mee.getComponent().getY() + mee.getY() - yOffset;
button1.setLocation(x1, y1);
i++ ;

yours respectfully<br />Avin Sinanan
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Avin,
I have looked at your code and made some changes. To save the button locations I used a simple ASCII file that prints out the following for each Moveable button:
label, xLocation, yLocation, width, height
Each line containing the above information for a single button. Therefore, the number of lines equals the number of buttons.
To try and have some fail-safe mechanism I made the first line of the file be:
Avin Property File
If the reader doesn't see this line as the first one than the file is not used to load in properties because it will probably have the wrong format. Since we are dealing with as ASCII file I also make sure that each lines contains 5 separate entries. If a line doesn't then it is ignored.
The major change I made was to your MoveableButton class. I made it extend JButton because it makes more sense and is easier to handle as a component rather than as a container (as you had it).
The code is supplied below.

I agree. Here's the link: http://aspose.com/file-tools
subject: Save and Open files ( realistically) ?? code included
It's not a secret anymore!