The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

How to Save and Open Files in Java????

Posted by Avin Sinanan on January 28, 2002 at 7:43 PM

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();
pane.setLayout(null);
MenuBarMaker menu = new MenuBarMaker(frame , pane);
ToolBarMaker toolbar = new ToolBarMaker(frame , pane);

frame.getContentPane().add(pane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Main Window");
frame.setSize(500 ,500);
frame.setVisible(true);

}
}

/*----------------------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");

menu1.setMnemonic(KeyEvent.VK_I);
menu1.getAccessibleContext().setAccessibleDescription("");

JMenuItem item1 = new JMenuItem("Open");
item1.setMnemonic(KeyEvent.VK_D);
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.setMnemonic(KeyEvent.VK_D);
item2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//what happens when pressed
MoveableButton move = new MoveableButton(frame , pane);

}
});

menu1.add(item1);
menu1.add(item2);
menubar.add(menu1);
frame.setJMenuBar(menubar);

}
}

/*---------------------------------------------------------*/

class ToolBarMaker
{
private JFrame frame;
private JPanel pane;
public ToolBarMaker(JFrame aFrame , JPanel apane)
{
frame = aFrame ;
pane = apane;
JToolBar toolbar = new JToolBar(JToolBar.HORIZONTAL);
toolbar.addSeparator();
JButton button = new JButton("Click to add Button to JPanel");
toolbar.add(button);
button.setMnemonic(KeyEvent.VK_D);
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);
}
});
button1.setBounds(10,15,100,40);
pane.add(button1);
pane.updateUI();
i++ ;
}
}






Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us