• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Save and Open files ( realistically) ?? code included

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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++ ;
}
}
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Enjoy,
Manfred.
 
My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic