Hello,
I have a program that has three text areas, and I'm going to add an image viewer.

My question is how to save and load the text and images so that when the user clicks 'save'
it will save a file with the text and images, and when the user clicks 'load' and chooses
a compatible file it will load that file's text and images.

Thanks in advance!


- WolfShield

my questions are:

1/ 3 x text areas == JTextArea || 3 x text areas != JTextArea

2/ text and images is some mixed Object, are you draw words, write words to the image, or you want to synchronize some text in one file with image saved in 2nd.

3/ about Load .... only in reverse order

for above mentioned all todays Databases exists, too

edit: I just realized that you're probably a student, and if so, you may be looking to use flat (normal) files. If that is the case then I would still recommend saving the images elsewhere and referencing them (perhaps just by a pathname, if your program is a desktop app), and you could save the text in a file. So the file would consist of your text sections + a pathname to the image. You could separate your text sections in hundreds of different ways. You should look into the Scanner class

Are you concerned about where to store and retrieve the images/text or how to store them? The answer to where is definitely to do it in a database. Assuming that you could be supporting multiple users, I would use a relational database and create several tables. One table would be for storing images (ID, image). One table for storing usernames (ID, username), one table for storing text entries, and one for storing the view you described (text areas + image).

The table for storing the view you described would have a foreign key referencing the users table as well as foreign keys referencing the image for the view and the text areas for the view.

Disclaimer: I'm NOT a database developer and this approach would definitely work, but for efficiency's sake you should wait for some other opinions on this one. (And in the meantime, describe your requirements more thoroughly)

Okay,
I am not a student, I'm just programming as a hobby.

I have three JTextAreas in a JFrame (also three JLabels, but I don't think that's important).

I know nothing of saving or loading files.

Here is the code, it's easier than trying to describe it.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class notepad {
	
	private JTextArea aboutTA, matTA, descTA;
	private JLabel aboutL, matL, descL;
	private JScrollPane aboutSP, matSP, descSP;
	
	public notepad() {
		// Content Pane
		JPanel content = new JPanel();
		content.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		// Menubar
		JMenuBar menuBar = new JMenuBar();
		
		JMenu fileMenu = new JMenu("File");
		JMenuItem fileSave = new JMenuItem("Save");
		fileMenu.add(fileSave);
		fileMenu.add(new JSeparator());
		JMenuItem fileQuit = new JMenuItem("Quit");
		fileMenu.add(fileQuit);
		menuBar.add(fileMenu);
		
		JMenu picsMenu = new JMenu("Pics");
		JMenuItem picsViewPics = new JMenuItem("View Pics");
		picsMenu.add(picsViewPics);
		picsMenu.add(new JSeparator());
		JMenuItem picsAddPic = new JMenuItem("Add A Pic");
		picsMenu.add(picsAddPic);
		JMenuItem picsAddGallery = new JMenuItem("Add A Gallery");
		picsMenu.add(picsAddGallery);
		menuBar.add(picsMenu);
		
		JMenu helpMenu = new JMenu("Help");
		JMenuItem helpTutorial = new JMenuItem("Tutorial");
		helpMenu.add(helpTutorial);
		menuBar.add(helpMenu);
		
		// Content
		aboutL = new JLabel("About:");
		c.gridy=0;
		c.gridx=0;
		c.gridwidth=1;
		c.gridheight=1;
		c.ipadx=0;
		c.ipady=0;
		c.insets=new Insets(0, 0, 0, 0);
                content.add(aboutL, c);
                aboutTA = new JTextArea(11, 15);
                aboutTA.setLineWrap(true);
                aboutSP = new JScrollPane(aboutTA,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                c.gridy=1;
		c.gridx=0;
		c.gridwidth=1;
		c.gridheight=1;
		c.ipadx=0;
		c.ipady=0;
		c.insets=new Insets(0, 0, 0, 0);
                content.add(aboutSP, c);
                matL = new JLabel("Materials:");
                c.gridy=2;
		c.gridx=0;
		c.gridwidth=1;
		c.gridheight=1;
		c.ipadx=0;
		c.ipady=0;
		c.insets=new Insets(0, 0, 0, 0);
                content.add(matL, c);
                matTA = new JTextArea(25, 15);
                matTA.setLineWrap(true);
                matSP = new JScrollPane(matTA, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                c.gridy=3;
		c.gridx=0;
		c.gridwidth=1;
		c.gridheight=1;
		c.ipadx=0;
		c.ipady=0;
		c.insets=new Insets(0, 0, 0, 0);
                content.add(matSP, c);
                descL = new JLabel("Description:");
                c.gridy=0;
		c.gridx=1;
		c.gridwidth=1;
		c.gridheight=1;
		c.ipadx=0;
		c.ipady=0;
		c.insets=new Insets(0, 0, 0, 0);
                content.add(descL, c);
                descTA = new JTextArea(37, 30);
                descTA.setLineWrap(true);
                descSP = new JScrollPane(descTA, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                c.gridy=1;
		c.gridx=1;
		c.gridwidth=1;
		c.gridheight=3;
		c.ipadx=0;
		c.ipady=0;
		c.insets=new Insets(0, 30, 0, 0);
                 content.add(descSP, c);
		
		// Window
		JFrame notepadWin = new JFrame("Idea Notepad");
		notepadWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		notepadWin.setResizable(false);
		notepadWin.setJMenuBar(menuBar);
		notepadWin.setSize(600, 700);
		notepadWin.setLocation(200, 50);
		notepadWin.setContentPane(content);
		notepadWin.setVisible(true);
	}
	
	public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new notepad();
            }
        });
	}

}

I'm going to add a JMenuItem that opens another JFrame that will load, view, delete, and save images.
Hopefully I've helped clarify things.

Also, if there are different ways to save. Whichever is the best, as in which
one would be the one used if it were a publicly used program. Like Windows Notepad,
or a program like that is the saving type I would like to use.

- WolfShield

You can load image using the java.awt.image.BufferedImage class, and you want to do some operation on the image, To save an image to an output file you need to import the javax.imageio.ImageIO class.


File f = new File( <String> filename); // e.g - "a.jpg"

ImageIO.write( <BufferedImage> imgObj, <String> format , <File> f ); // throws IOException

Simplest possible method I can think of - not the best, but maybe not too bad, either - would be to store each element (text area contents/image file reference) as a property in a properties file, and read the properties file to reconstruct the saved data. You'd have to distinguish between "/users/wolfshield/images/my_picture.png" as a literal text string and that text as a reference to an image, but that's not too hard to deal with.

Then you just need to read the properties file to get a list of document elements and insert them. This is very easy if you have, as you say, a fixed element set. Your propst file might look like

textfield1="blah blah blah"
textfield2="whatever"
textfield3="something else"
img_location="/users/wolfshield/images/my_picture.png"

It gets more complicated if you want to use a free format - say, a user-generated document with embedded images. I wouldn't recommend this if you need something scalable.

Alright then,
I'm having a little trouble understanding this.

If I post some code here for a small window with one (1) textarea in it,
would someone be willing to post the code for saving it and loading it
for me, so that I can better understand how to save?

I swear I'm not, like, cheating on a test or anything!

Thanks,

- WolfShield

If you're interested in my suggestion, I'd rather you look into the use of Properties files and try to implement the idea. After all, I've already given you most of it.

before that you have to decide:

synchronize with
- database
http://download.oracle.com/javase/tutorial/jdbc/basics/jdbcswing.html
http://download.oracle.com/javase/tutorial/jdbc/

- log file (flat text file)
http://download.oracle.com/javase/tutorial/essential/io/index.html

- Xml file (two API for xml in java, but)
http://download.oracle.com/javase/tutorial/jaxp/index.html

- java properties file
maybe this one is better http://download.oracle.com/javase/tutorial/i18n/resbundle/index.html


1/ so confortable and very easily with database, because you can put all Object from GUI to one row in database, id + text1 + text2 + test3 + BLOB (CLOB or File depends of db Engine)

2/ with File (flat or Xml) you'll probably some issue with Folder and File synchronizations (Strict FileName Stricture)

3/ with Java properies file (same issue as with file), but be sure that this properties file must be placed outside of jar,

because basically you can use properties file packed into jar, and placed outside jar,

properties file compiled into jar is unchangeable (sure there are exist 2 ways but these methods isn't appropriate without strong Java knowledge)
http://download.oracle.com/javase/tutorial/deployment/jar/index.html

more examples http://www.java2s.com/


check JTextArea#Methods for Style and Formats

I can help you in case of only images...

To load an image you can use the following code

import java.io.*;
import javax.imageio.*;
import java.awt.image.*;
import java.awt.*;

public class ImageLoad
{        
     BufferedImage image;
    String fileName;

    public ImageLoad()
    { 
          File f = new File( fileName );

          try
          {
               image = ImageIO.read( f );    // or ImageIO.read( <URL> imageSRC );
          }

          catch( IOException exp )
          {
              System.err.println( exp.getMessage() );
          }
    }
}

// You can drawImage using an Applet or whatever you are using

     public void paint(Graphics g)    // or paintComponent(Graphics g)
     {
           g.drawImage( image, 10, 10, null );
     }

You can do several operations on the image and after doing some operation you can save the image to an output image file

the following code will do this..

public void saveImage()

    {

	

        String outputFileName = "a.png";
        File outputFile;

    	try

    	{

    		outputFile = new File( outputFileName );

    		ImageIO.write( image , "png" , outputFile );

    	}

    	catch( IOException exp )

    	{

    		System.out.println ( exp.getMessage() );

    	}

    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.