Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 36

Thread: Can't get it to save stuff with JFileChooser

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Angry Can't get it to save stuff with JFileChooser

    I don't know how to get the FileWriter to get the text typed in the file name area in the JFileChooser, and it never saves the file, or, if it does, it doesn't save anything and the file keeps getting blank. How do you get it to save the background and the text in the Background and everything?

    I don't know how to get it to save it so if I created a java database sorta thing with a JTable, it could be saved and keep the text in the tables there, instead of losing it, while loaded, and it would save.

    How do I do that?

    import java.awt.color.ColorSpace;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.AdjustmentEvent;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseEvent;
     
    import javax.swing.event.MenuEvent;
    import javax.swing.event.MenuListener;
    import javax.swing.event.PopupMenuEvent;
    import javax.swing.event.PopupMenuListener;
    import javax.swing.filechooser.FileSystemView;
    import javax.swing.text.DefaultStyledDocument;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.ScrollPaneConstants;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    import java.util.*;
    import java.util.jar.JarFile;
    import java.io.*;
     
    import javax.swing.ImageIcon;
     
     
    import java.awt.event.WindowEvent;
    import java.awt.print.PageFormat;
    import java.awt.print.Printable;
    import java.awt.print.PrinterException;
    import java.awt.print.PrinterJob;
    import java.awt.Window;
    import javax.swing.JScrollPane;
    import java.awt.Component;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import acm.io.*;
    import acm.graphics.*;
    import acm.program.*;
    import acm.gui.*;
    import acm.util.*;
    import java.awt.Container;
    import java.awt.PrintJob;
    import java.awt.print.*;
    /**
     * @author Paul
     * Begin Date: July 13, 2010
     * End Date:  
     * Project Title: Church Hymn Book Program
     * Description: Uses JComboBox to select all songs, shown in alphabetical order.  Also, typing in a 
     * page number will automatically call the setSelected() method to the song with that number.
     * Selecting a song will also automatically set the text in the pageNumber text area to that
     * page number.  There is another JComboBox that will choose colors. Also, there is another 
     * JTextArea that has verse numbers.  That will only display that verse, unless "Display all"
     * is checked in JCheckBox check.  There will be no exit button, but the JMenuItem exit under the 
     * JMenu options under JMenuBar menuBar will allow the user to exit.  Also, there will be a JComboBox
     * for font types and font colors.  There will be two more JMenuItem under JMenu options that will 
     * set the background color and the foreground color of the JPanel, to the color specified in each of the 
     * sub-items of those two JMenuItems.  
     */
    public class LivingHymns extends JFrame implements ActionListener,
    		ItemListener, MenuListener, Printable {
     
    	/* (non-Javadoc)
    	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
    	 */
    	int[] pageBreaks; 
    	String[] textLines;
    	Cursor cursor;
     
    	private void initTextLines() {
            if (textLines == null) {
                int numLines=200;
                textLines = new String[numLines];
                for (int i=0;i<numLines;i++) {
                    textLines[i]= "This is line number " + i;
                }
            }
        }
        JFileChooser chooser;
    	JLabel hayField, cottonField, wheatField;
    	JMenu menu, menu2, helpMenu;
    	ProgramMenuBar bar;
    	DefaultStyledDocument document;
    	JTextArea area;
    	JCheckBox box;
    	JComboBox songs, fonts, colors;
    	JScrollPane scrolls, scrolls2;
    	String[] songNames;
    	String[] fontTypes;
    	boolean printNow;
    	JMenuItem openHelpMenu;
    	JLabel clickOnMe;
    	JLabel cornField;
    	JLabel riceField;
    	JTextField beanField;
    	JScrollBar alphaAdjuster;
    	String[] colorSelector;
    	JMenuItem button, button2, button3, button4, button5, button6;
    	JMenuItem copy;
    	JTextField pageNumber, verseNumber;
    	JPanel panel;
    	String Bob;
    	JMenuItem print,save;
    	JFrame frame, frame2;
    	JRadioButton color, color2, color3, color4, color5, color6, color7, color8, color9, color10,
    	color11, color12, color13, color31, color32;
    	ButtonGroup group, group2, group3, group4;
    	Color red, green, blue, orange, yellow, pink, black, gray, lightGray, darkGray, magenta, cyan,
    	white, brown, purple;
    	JLabel background, textBackground, textForeground, pageNum;
    	JRadioButton color14, color15, color16, color17, color18, color19, color20, color21, color22,
    	color23, color24, color25, color26, color27, color28, color29, color30, color33, color34;
    	Choice choice;
    	PopupMenu popup;
    	JScrollBar redAdjuster, greenAdjuster, blueAdjuster;
    	ColorSpace space;
    	JPopupMenu lollipop, tootsiePop, allDaySucker;
    	boolean doPrint;
    	Font Aharoni, Andalus, AngsanaNew, AngsanaUPC, ArabicTypesetting, Arial, ArialBlack, ArialNarrow,
    	ArialUnicodeMS, Batang, BatangChe, BlackadderITC, BookAntiqua, BookmanOldStyle, BookshelfSymbol7,
    	BradleyHandITC, BrowalliaNew, BrowalliaUPC, Calibri, Cambria, CambriaMath, Candara, Century, 
    	CenturyGothic, ComicSansMS, Consolas, Constantia, CopperplateGothicBold, CopperplateGothicLight,
    	Corbel, CordiaNew, CordiaUPC, CourierNew, CSD12, CurlzMT, DaunPenh, David, DFKaiSB, Dialog,
    	DialogInput, DilleniaUPC, DokChampa, Dotum, DotumChe, EdwardianScriptITC, EngraversMT, ErasDemiITC,
    	ErasLightITC, EstrangeloEdessa, EucrosiaUPC, Euphemia, Eurostile, FangSong, FelixTitling,
    	FranklinGothicBook, FranklinGothicDemi, FranklinGothicDemiCond, FranklinGothicHeavy, FranklinGothicMedium,
    	FranklinGothicMediumCond, FrankRuehl, FreesiaUPC, FreestyleScript, FrenchScriptMT, Garamond, Gautami,
    	Georgia, GiovanniITCTT, Gisha, Gulim, GulimChe, Gungsuh, GungsuhChe, Impact, IrisUPC, IskoolaPota,
    	JasmineUPC, Jokerman, JuiceITC, KaiTi, Kalinga, Kartika, KodchiangUPC, KristenITC, Latha, Leelawadee,
    	LevenimMT, LilyUPC, LucidaBlackletter, LucidaBright, LucidaCalligraphy, LucidaConsole, LucidaHandwriting,
    	LucidaSans, LucidaSansTypewriter, LucidaSansUnicode, MaiandraGD, MalgunGothic, Mangal, Marlett, 
    	MatisseITC, Meiryo, MeiryoUI, MicrosoftHimalaya, MicrosoftJhengHei, MicrosoftSansSerif, MicrosoftUighur,
    	MicrosoftYaHei, MicrosoftYiBaiti, MingLiU, MingLiUExtB, MingLiU_HKSCS, MingLiU_HKSCSExtB, Miriam, MiriamFixed,
    	Mistral, MongolianBaiti, Monospaced, MonotypeCorsiva;
    	JMenuBar mBar;
    	PrinterJob job;
    	PageFormat format;
    	FileDialog saveDialog, loadDialog;
    	FileSystemView view;
    	JFrame saveFrame, openFrame;
    	FileWriter writer;
    	JarFile jar;
    	FileOutputStream river;
    	/*
    	1.) Aharoni
    	2.) Andalus
    	3.) Angsana New
    	4.) AngsanaUPC
    	5.) Arabic Typesetting
    	6.) Arial
    	7.) Arial Black
    	8.) Arial Narrow
    	9.) Arial Unicode MS
    	10.) Batang
    	11.) BatangChe
    	12.) Blackadder ITC
    	13.) Book Antiqua
    	14.) Bookman Old Style
    	15.) Bookshelf Symbol 7
    	16.) Bradley Hand ITC
    	17.) Browallia New
    	18.) BrowalliaUPC
    	19.) Calibri
    	20.) Cambria
    	21.) Cambria Math
    	22.) Candara
    	23.) Century
    	24.) Century Gothic
    	25.) Comic Sans MS
    	26.) Consolas
    	27.) Constantia
    	28.) Copperplate Gothic Bold
    	29.) Copperplate Gothic Light
    	30.) Corbel
    	31.) Cordia New
    	32.) CordiaUPC
    	33.) Courier New
    	34.) CSD12
    	35.) Curlz MT
    	36.) DaunPenh
    	37.) David
    	38.) DFKai-SB
    	39.) Dialog
    	40.) DialogInput
    	41.) DilleniaUPC
    	42.) DokChampa
    	43.) Dotum
    	44.) DotumChe
    	45.) Edwardian Script ITC
    	46.) Engravers MT
    	47.) Eras Demi ITC
    	48.) Eras Light ITC
    	49.) Estrangelo Edessa
    	50.) EucrosiaUPC
    	51.) Euphemia
    	52.) Eurostile
    	53.) FangSong
    	54.) Felix Titling
    	55.) Franklin Gothic Book
    	56.) Franklin Gothic Demi
    	57.) Franklin Gothic Demi Cond
    	58.) Franklin Gothic Heavy
    	59.) Franklin Gothic Medium
    	60.) Franklin Gothic Medium Cond
    	61.) FrankRuehl
    	62.) FreesiaUPC
    	63.) Freestyle Script
    	64.) French Script MT
    	65.) Garamond
    	66.) Gautami
    	67.) Georgia
    	68.) GiovanniITCTT
    	69.) Gisha
    	70.) Gulim
    	71.) GulimChe
    	72.) Gungsuh
    	73.) GungsuhChe
    	74.) Impact
    	75.) IrisUPC
    	76.) Iskoola Pota
    	77.) JasmineUPC
    	78.) Jokerman
    	79.) Juice ITC
    	80.) KaiTi
    	81.) Kalinga
    	82.) Kartika
    	83.) KodchiangUPC
    	84.) Kristen ITC
    	85.) Latha
    	86.) Leelawadee
    	87.) Levenim MT
    	88.) LilyUPC
    	89.) Lucida Blackletter
    	90.) Lucida Bright
    	91.) Lucida Calligraphy
    	92.) Lucida Console
    	93.) Lucida Handwriting
    	94.) Lucida Sans
    	95.) Lucida Sans Typewriter
    	96.) Lucida Sans Unicode
    	97.) Maiandra GD
    	98.) Malgun Gothic
    	99.) Mangal
    	100.) Marlett
    	101.) Matisse ITC
    	102.) Meiryo
    	103.) Meiryo UI
    	104.) Microsoft Himalaya
    	105.) Microsoft JhengHei
    	106.) Microsoft Sans Serif
    	107.) Microsoft Uighur
    	108.) Microsoft YaHei
    	109.) Microsoft Yi Baiti
    	110.) MingLiU
    	111.) MingLiU-ExtB
    	112.) MingLiU_HKSCS
    	113.) MingLiU_HKSCS-ExtB
    	114.) Miriam
    	115.) Miriam Fixed
    	116.) Mistral
    	117.) Mongolian Baiti
    	118.) Monospaced
    	119.) Monotype Corsiva
    	120.) MoolBoran
    	121.) MS Gothic
    	122.) MS Mincho
    	123.) MS PGothic
    	124.) MS PMincho
    	125.) MS Reference 1
    	126.) MS Reference 2
    	127.) MS Reference Sans Serif
    	128.) MS Reference Specialty
    	129.) MS UI Gothic
    	130.) MT Extra
    	131.) MV Boli
    	132.) Narkisim
    	133.) NSimSun
    	134.) Nyala
    	135.) OCR A Extended
    	136.) Palatino Linotype
    	137.) Papyrus
    	138.) Perpetua
    	139.) Plantagenet Cherokee
    	140.) PMingLiU
    	141.) PMingLiU-ExtB
    	142.) Poor Richard
    	143.) Pristina
    	144.) Raavi
    	145.) Rockwell
    	146.) Rockwell Extra Bold
    	147.) Rod
    	148.) SansSerif
    	149.) Segoe Print
    	150.) Segoe Script
    	151.) Segoe UI
    	152.) Serif
    	153.) Shruti
    	154.) SimHei
    	155.) Simplified Arabic
    	156.) Simplified Arabic Fixed
    	157.) SimSun
    	158.) SimSun-ExtB
    	159.) Sylfaen
    	160.) Symbol
    	161.) Tahoma
    	162.) Tempus Sans ITC
    	163.) Times New Roman
    	164.) Traditional Arabic
    	165.) Trebuchet MS
    	166.) Tunga
    	167.) Verdana
    	168.) Viner Hand ITC
    	169.) Vivaldi
    	170.) Vrinda
    	171.) Webdings
    	172.) Wingdings
    	173.) Wingdings 2
    	174.) Wingdings 3
    */
    	JFrame wenguinFrame;
    	File file;
    	/**
    	 * @param arg0
    	 */
    	/**
    	 * 
    	 */
    	public LivingHymns() {
    		setTitle("Living Hymns Song Selection for Shooting Park Road Baptist Church");
            wenguinFrame = new JFrame();
     
    		saveDialog = new FileDialog(wenguinFrame, "Save", FileDialog.SAVE);
    		openHelpMenu = new JMenuItem("Open Help Menu");
     
    		beanField = new JTextField(5);
     
           saveFrame = new JFrame();
           openFrame = new JFrame();
     
     
    		redAdjuster = new JScrollBar(JScrollBar.HORIZONTAL, 0,0,0, 255);
    		redAdjuster.setToolTipText("Click here to change amount of red in Red, Green, Blue color-configuration for background color");
    		greenAdjuster = new JScrollBar(JScrollBar.HORIZONTAL, 0, 0, 0, 255);
    		greenAdjuster.setToolTipText("Click here to change amount of green in Red, Green, Blue color-configuration for background color");
    		blueAdjuster = new JScrollBar(JScrollBar.HORIZONTAL, 0, 0, 0, 255);
    		blueAdjuster.setToolTipText("Click here to change amount of blue in Red, Green, Blue color-configuration for background color");
    		alphaAdjuster = new JScrollBar(JScrollBar.HORIZONTAL, 0, 0, 0, 255);
    		alphaAdjuster.setToolTipText("Click here to change amount of alpha in Red, Green, Blue, Alpha color-configuration for background color");
    		cottonField = new JLabel("Blue: 0");
    		cottonField.setToolTipText("Blue value");
    		wheatField = new JLabel("Green: 0");
    		wheatField.setToolTipText("Green value");
    		riceField = new JLabel("Alpha:  0");
    		riceField.setToolTipText("Alpha, don't ask what alpha is, I don't know, value");
    		Aharoni = new Font("Aharoni", Font.BOLD + Font.ITALIC, 41);
    		Andalus = new Font("Andalus", Font.BOLD + Font.ITALIC, 41);
    		AngsanaNew = new Font("Angsana New", Font.BOLD + Font.ITALIC, 41);
     
     
    		popup = new PopupMenu("Help Menu");
    		lollipop = new JPopupMenu("Help Menu");
    		button = new JMenuItem("Setting the Background Color");
    		button2 = new JMenuItem("Setting The Color of The Text Areas");
    		button3 = new JMenuItem("Selecting Fonts and Font Sizes and Font Styles");
    		button4 = new JMenuItem("Setting the Color of The Text");
    		button5 = new JMenuItem("Getting Songs from List or Getting Song By Entering Page Numbers");
    		button6 = new JMenuItem("Exit Menu");
    		cornField = new JLabel("    " + lollipop.getLabel());
    		cornField.setBackground(green);
    		cornField.setForeground(blue);
    		hayField = new JLabel("Red: 0");
    		hayField.setToolTipText("Red Value");
    		lollipop.add(cornField);
    		lollipop.addSeparator();
    		lollipop.add(button);
    		lollipop.addSeparator();
    		lollipop.add(button2);
    		lollipop.addSeparator();
    		lollipop.add(button3);
    		lollipop.addSeparator();
    		lollipop.add(button4);
    		lollipop.addSeparator();
    		lollipop.add(button5);
    		lollipop.addSeparator();
    		lollipop.add(button6);
     
    		save = new JMenuItem("Save");
     
    		menu2 = new JMenu("Edit");
    		helpMenu = new JMenu("Help Menu");
    		// menu.setBackground(Color.GREEN);
    		menu2.setBackground(Color.GREEN);
    		helpMenu.setBackground(Color.GREEN);
     
     
     
    		document = new DefaultStyledDocument();
     
     
     
     
    		frame2 = new JFrame();
     
    		colorSelector = new String[15];
    		colorSelector[0] = "Red";
    		colorSelector[1] = "Green";
    		colorSelector[2] = "Blue";
    		colorSelector[3] = "Orange";
    		colorSelector[4] = "Yellow";
    		colorSelector[5] = "Pink";
    		colorSelector[6] = "Black";
    		colorSelector[7] = "Gray";
    		colorSelector[8] = "Light Gray";
    		colorSelector[9] = "Dark Gray";
    		colorSelector[10] = "Magenta";
    		colorSelector[11] = "Cyan";
    		colorSelector[12] = "White";
    		colorSelector[13] = "Purple";
    		colorSelector[14] = "Brown";
     
    		colors = new JComboBox(colorSelector);
     
    		fontTypes = new String[174];
     
    		fontTypes[0] = "Aharoni";
    		fontTypes[1] = "Andalus";
    		fontTypes[2] = "Angsana New";
     
    		fonts = new JComboBox(fontTypes);
    		fonts.setBackground(green);
    		redAdjuster.setBackground(red);
    		greenAdjuster.setBackground(green);
    		blueAdjuster.setBackground(blue);
    		alphaAdjuster.setBackground(green);
     
     
    		pageNumber = new JTextField(3);
    		pageNum = new JLabel("Enter page number");
    		panel = new JPanel();
     
     
    		// panel.add(saveDialog);
    		bar = new ProgramMenuBar();
    	     helpMenu.add(openHelpMenu);
    		menu = new JMenu("File");
    		menu.add(save);
    		save.addActionListener(
    				new ActionListener() {
     
    					@Override
    					public void actionPerformed(ActionEvent arg0) {
    						// TODO Auto-generated method stub
    					//	saveDialog.setVisible(true);
     
    						 file = new File("C:/hymns.jar");
     
    						chooser = new JFileChooser();
    						chooser.setBackground(Color.GREEN);
     
    						chooser.setVisible(true);
    						chooser.showSaveDialog(openFrame);
     
     
     
    					}
     
    				});
    		mBar = new JMenuBar();
    		mBar.add(menu);
    		mBar.add(menu2);
    		mBar.add(helpMenu);
    		mBar.setVisible(true);
    		menu.setBackground(Color.GREEN);
    		mBar.setBackground(Color.GREEN);
    		// bar.init();
    	     panel.add(mBar);
    	     print = new JMenuItem("Print");
    	     menu.add(print);
    	     printNow = false;
    	    print.addActionListener(
    	    		new ActionListener() {
     
    					@Override
    					public void actionPerformed(ActionEvent arg0) {
    						// TODO Auto-generated method stub
     
    						 printNow = true;
    						 job = PrinterJob.getPrinterJob();
     
    						 						 job.setPrintable(new LivingHymns());
    						 						format = job.pageDialog(job.defaultPage());	 
     
    						    doPrint = job.printDialog();
    							if (doPrint) {
    								  try {
    								       job.print();
    								  } catch (PrinterException e) {
    								   /* The job did not successfully complete */
    								  }
     
     
     
     
    					}
     
     
    					}
     
    	    		});
     
     
     
     
    		panel.add(hayField);
    		hayField.setVisible(true);
    		panel.add(redAdjuster);
    		redAdjuster.setVisible(true);
    		panel.add(wheatField);
    		wheatField.setVisible(true);
    		panel.add(greenAdjuster);
    		greenAdjuster.setVisible(true);
    		panel.add(cottonField);
    		cottonField.setVisible(true);
    		panel.add(blueAdjuster);
    		blueAdjuster.setVisible(true);
    		panel.add(riceField);
    		riceField.setVisible(true);
    		panel.add(alphaAdjuster);
    		alphaAdjuster.setVisible(true);
    		panel.add(beanField);
    		beanField.setVisible(false);
    		panel.add(fonts);
    		fonts.setSize(20, 30);
    		panel.add(popup);
    		panel.setVisible(true);
    		pageNum.setVisible(true);
    		pageNumber.setVisible(true);
    		panel.add(pageNum);
    		panel.add(pageNumber);
    		panel.add(lollipop);
    		System.out.println(lollipop.getLabel());
    		lollipop.setLightWeightPopupEnabled(true);
    		System.out.println(lollipop.isVisible());
    		verseNumber = new JTextField(3);
    		// lollipop.add(verseNumber);
     
     
     
    		System.out.println(panel.isShowing());
    		panel.setLocation(10, 20);
    		clickOnMe = new JLabel("Click on me to get help menu");
     
    		panel.add(clickOnMe);
     
    		clickOnMe.setLocation(300, 200);
     
     
     
    		clickOnMe.setVisible(true);
     
     
     
    		frame = new JFrame();
    		background = new JLabel("Set Background Color");
    		color = new JRadioButton("Red");
    		color2 = new JRadioButton("Green");
    		color3 = new JRadioButton("Blue");
    		color4 = new JRadioButton("Orange");
    		color5 = new JRadioButton("Yellow");
    		color6 = new JRadioButton("Pink");
    		color7 = new JRadioButton("Black");
    		color8 = new JRadioButton("Gray");
    		color9 = new JRadioButton("Light Gray");
    		color10 = new JRadioButton("Dark Gray");
    		color11 = new JRadioButton("Magenta");
    		color12 = new JRadioButton("Cyan");
    		color13 = new JRadioButton("White");
    		color14 = new JRadioButton("Red");
    		color15 = new JRadioButton("Green");
    		color16 = new JRadioButton("Blue");
    		color17 = new JRadioButton("Orange");
    		color18 = new JRadioButton("Yellow");
    		color19 = new JRadioButton("Pink");
    		color20 = new JRadioButton("Black");
    		color21 = new JRadioButton("Gray");
    		color22 = new JRadioButton("Light Gray");
    		color23 = new JRadioButton("Dark Gray");
    		color24 = new JRadioButton("Magenta");
    		color25 = new JRadioButton("Cyan");
    		color26 = new JRadioButton("White");
    		color27 = new JRadioButton("Brown");
    		color28 = new JRadioButton("Purple");
    		color29 = new JRadioButton("Brown");
    		color30 = new JRadioButton("Purple");
    		color31 = new JRadioButton("Lighter");
    		color32 = new JRadioButton("Darker");
    		color33 = new JRadioButton("Lighter");
    		color34 = new JRadioButton("Darker");
     
    		group = new ButtonGroup();
    		group.add(color);
    		group.add(color2);
    		group.add(color3);
    		group.add(color4);
    		group.add(color5);
    		group.add(color6);
    		group.add(color7);
    		group.add(color8);
    		group.add(color9);
    		group.add(color10);
    		group.add(color11);
    		group.add(color12);
    		group.add(color13);
    		group.add(color27);
    		group.add(color28);
     
    		group3 = new ButtonGroup();
     
    		 group3.add(color31);
    		 group3.add(color32);
     
     
     
     
     
    		button.setBackground(green);
    		button2.setBackground(green);
    		button3.setBackground(green);
    		button4.setBackground(green);
    		button5.setBackground(green);
    		button6.setBackground(green);
     
    		group2 = new ButtonGroup();
    		group2.add(color14);
    		group2.add(color15);
    		group2.add(color16);
    		group2.add(color17);
    		group2.add(color18);
    		group2.add(color19);
    		group2.add(color20);
    		group2.add(color21);
    		group2.add(color22);
    		group2.add(color23);
    		group2.add(color24);
    		group2.add(color25);
    		group2.add(color26);
    		group2.add(color29);
    		group2.add(color30);
     
     
     
     
    		background.setVisible(true);
    		int x = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
    		int y = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
    		int z = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER;
    		area = new JTextArea(65, 110);
     
     
    		scrolls = new JScrollPane(area, x, z);
    		panel.add(scrolls);
    		scrolls2 = new JScrollPane(panel, x, y);
    		pageNumber.setText("0");
     
     
     
     
     
     
    		panel.add(background);
    		panel.add(color);
    		panel.add(color2);
    		panel.add(color3);
    		panel.add(color4);
    		panel.add(color5);
    		panel.add(color6);
    		panel.add(color7);
    		panel.add(color8);
    		panel.add(color9);
    		panel.add(color10);
    		panel.add(color11);
    		panel.add(color12);
    		panel.add(color13);
    		panel.add(color27);
    		panel.add(color28);
    		panel.add(color31);
    		panel.add(color32);
    		panel.setVisible(true);
    		scrolls.setVisible(true);
    		scrolls2.setVisible(true);
    		songNames = new String[835];
    		box = new JCheckBox("Box");
    		panel.add(box);
    		box.setVisible(true);
    		panel.setVisible(true);
    		panel.add(scrolls);
    		// frame.add(scrolls);
     
     
    		panel.setName("Bob");
    		frame.setTitle("Bob 2");
    		 panel.add(scrolls);
    		 red = Color.RED;
    		 green = Color.GREEN;
    		 blue = Color.BLUE;
    		 orange = Color.ORANGE;
    		 yellow = Color.YELLOW;
    		 black = Color.BLACK;
    		 pink = Color.PINK;
    		 gray = Color.GRAY;
    		 lightGray = Color.LIGHT_GRAY;
    		 darkGray = Color.DARK_GRAY;
    		 magenta = Color.MAGENTA;
    		 white = Color.WHITE;
    		 brown = new Color(102, 51, 0 );
    		 purple = new Color(102, 0, 102);
     
    		// copy = new JButton("Copy");
    		// panel.add(copy);
    	//	copy.setVisible(true);
     
    		textBackground = new JLabel("Set Background Color of Text Area");
    		panel.add(textBackground);
    		panel.add(color14);
    		panel.add(color15);
    		panel.add(color16);
    		panel.add(color17);
    		panel.add(color18);
    		panel.add(color19);
    		panel.add(color20);
    		panel.add(color21);
    		panel.add(color22);
    		panel.add(color23);
    		panel.add(color24);
    		panel.add(color25);
    		panel.add(color26);
    		panel.add(color29);
    		panel.add(color30);
     
    		doPrint = false;
    		textForeground = new JLabel("Set Color of Text in Text Area");
    		panel.add(textForeground);
    		panel.add(colors);
     
    		area.setLineWrap(true);
     
    		hayField.setText("Red: " + panel.getBackground().getRed() + " ");
    		redAdjuster.setValue(panel.getBackground().getRed());
    		wheatField.setText("Green: " + panel.getBackground().getGreen() + " ");
    		greenAdjuster.setValue(panel.getBackground().getGreen());
    		cottonField.setText("Blue: " + panel.getBackground().getBlue() + " ");
    		blueAdjuster.setValue(panel.getBackground().getBlue());
    		riceField.setText("Alpha: " + panel.getBackground().getAlpha() + " ");
    		alphaAdjuster.setValue(panel.getBackground().getAlpha());
     
     
    		pageNumber.addActionListener(
    				new ActionListener() {
    					public void actionPerformed(ActionEvent e)
    					{
    						int m = Integer.parseInt(pageNumber.getText());
    					     if (m == 1)
    							{
    					    	 String song1 = "1.) My Faith Has Found a Resting Place" + "\n" + 
    					    	 "By Lidie H. Edmunds, Norse Air, Arr. by William J. Kirkpatrick" + "\n" + "\n" +
    					    	 "1.) My faith has found a resting place, not in device nor creed;" + "\n" +
    					    	 "    I trust the Everliving One, His wounds for me shall plead." + "\n" +
    					    	 "Chorus: I need no other arguement, I need no other plea," + "\n" +
    					    	 "        It is enough that Jesus died, and that he died for me." + "\n" + "\n" +
    					    	 "2.) Enough for me that Jesus saves, this ends my fear and doubt;" + "\n" +
    					    	 "    A sinful soul I come to Him, He'll never cast me out." + "\n" +
    					    	 "Chorus: I need no other arguement, I need no other plea," + "\n" +
    					    	 "        It is enough that Jesus died, and that he died for me." + "\n" + "\n" +
    					    	 "3.) My heart is leaning on the Word, the written Word of God," + "\n" +
    					    	 "    Salvation by my Saviour's name, salvation thro' His blood." + "\n" +
    					    	 "Chorus: I need no other arguement, I need no other plea," + "\n" +
    					    	 "        It is enough that Jesus died, and that he died for me." + "\n" + "\n" +
    					    	 "4.) My great Physician heals the sick, the lost He came to save;" + "\n" +
    					    	 "    For me His precious blood He shed, for me His Life he gave." + "\n" +
    					    	 "Chorus: I need no other arguement, I need no other plea," + "\n" +
    					    	 "        It is enough that Jesus died, and that he died for me." + "\n" + "\n";
     
     
    								area.setText(song1);
    							}
     
    							else if (m == 2)
    							{
    								String song2 = "2.) Come Thou Almighty King" + "\n" +
    								"By Anon. and Felice de Giagdini" + "\n" + "\n" +
    								"1.) Come, Thou almighty King, help us Thy Name to sing," + "\n" +
    								"    Help us to praise!  Father all glorious, O'er all victorious," + "\n" +
    								"    Come and reign over us, Ancient of Days!" + "\n" + "\n" +
    								"2.) Come Thou Incarnate Word, gird of Thy mighty sword," + "\n" +
    								"    Our pray'r attend; come, and Thy people bless and give Thy" + "\n" +
    								"    word success; Spirit of holiness, on us descend!" + "\n" + "\n" +
    								"3.) Come, Holy Comforter, Thy sacred witness bear," + "\n" +
    								"    In this glad hour: Thou who almighty art, now rule in" + "\n" +
    								"    every heart, And ne'er from us depart, Spirit of power!" + "\n" + "\n" +
    								"4.) To Thee, great One in Three, eternal praises be" + "\n" +
    								"    Hence, evermore: Thy sov'reign magesty, may we in" + "\n" +
    								"    glory see, And to eternity Love and adore!" + "\n" + "\n";
    								area.setText(song2);
    							}
     
    							else if (m == 3)
    							{
    							String song3 = "3.) Praise The Savior, Ye Who Know Him!" + "\n" + 
    							               "    By Thomas Kelly.  Traditional German Melody" + "\n" + "\n" +
    							               "1.) Praise the Savior, ye who know Him!" + "\n" +
    							               "    Who can tell how much we owe Him?" + "\n" +
    							               "    Gladly let us render to Him all we are and have." + "\n" + "\n" +
    							               "2.) Jesus is the name that charms us;" + "\n" +
    							               "    He for conflict fits and arms us;" + "\n" +
    							               "    Nothing moves and nothing harms us while we trust in Him." + "\n" +"\n" +
    							               "3.) Trust in Him, ye saints, forever;" + "\n" +
    							               "    He is faithful, changing never;" + "\n" +
    							               "    Neither force nor guile can sever those He loves from Him." + "\n" + "\n" +
    							               "4.) Keep us, Lord, oh keep us cleaving;" + "\n" +
    							               "    to Thyself and still still believing," + "\n" +
    							               "    Till the hour of our receiving promised joys with Thee." + "\n" + "\n" +
    							               "5.) Then we shall be where we would be," + "\n" +
    							               "    then we shall be what we should be;" + "\n" +
    							               "    Things that are not now, nor could be, soon shall be our own." + "\n" + "\n";
    							             area.setText(song3);
     
    							}
    					}
    				});
     
    		redAdjuster.addAdjustmentListener(
    				new AdjustmentListener() {
     
    					@Override
    					public void adjustmentValueChanged(AdjustmentEvent arg0) {
    						// TODO Auto-generated method stub
    						hayField.setText("Red: " + arg0.getValue()+ " ");
    						repaint();
    						Color pretty = new Color(arg0.getValue(), panel.getBackground().getGreen(), panel.getBackground().getBlue());
    						panel.setBackground(pretty);
    						color.setBackground(pretty);
    						color2.setBackground(pretty);
    						color3.setBackground(pretty);
    						color4.setBackground(pretty);
    						color5.setBackground(pretty);
    						color6.setBackground(pretty);
     
     
    					}
     
    				});
     
    		greenAdjuster.addAdjustmentListener(
    				new AdjustmentListener() {
     
    					@Override
    					public void adjustmentValueChanged(AdjustmentEvent arg0) {
    						// TODO Auto-generated method stub
    						wheatField.setText("Green: " + arg0.getValue()+ " ");
    						repaint();
    						Color pretty2 = new Color(panel.getBackground().getRed(), arg0.getValue(), panel.getBackground().getBlue());
    						panel.setBackground(pretty2);
    					}
     
    				});
     
    		blueAdjuster.addAdjustmentListener(
    				new AdjustmentListener() {
     
    					@Override
    					public void adjustmentValueChanged(AdjustmentEvent arg0) {
    						// TODO Auto-generated method stub
    						cottonField.setText("Blue: " + arg0.getValue()+ " ");
    						repaint();
    						Color pretty3 = new Color(panel.getBackground().getRed(),  panel.getBackground().getGreen(), arg0.getValue());
    						panel.setBackground(pretty3);
    					}
     
    				});
     
    		alphaAdjuster.addAdjustmentListener(
    				new AdjustmentListener() {
     
    					@Override
    					public void adjustmentValueChanged(AdjustmentEvent arg0) {
    						// TODO Auto-generated method stub
    						riceField.setText("Alpha: " + arg0.getValue()+ " ");
    						repaint();
    						Color pretty4 = new Color(panel.getBackground().getRed(),  panel.getBackground().getGreen(), panel.getBackground().getBlue(), arg0.getValue());
    						panel.setBackground(pretty4);
    					}
     
    				});
     
    		openHelpMenu.addActionListener(
    				new ActionListener() {
     
    					@Override
    					public void actionPerformed(ActionEvent arg0) {
    						// TODO Auto-generated method stub
    						lollipop.setVisible(true);
    					}
     
    				});
     
     
     
    		 color.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(red);
    						 hayField.setText("Red: " + panel.getBackground().getRed());
    						 redAdjuster.setValue(panel.getBackground().getRed());
    						 wheatField.setText("Green: " + panel.getBackground().getGreen());
    						 greenAdjuster.setValue(panel.getBackground().getGreen());
    						 cottonField.setText("Blue: " + panel.getBackground().getBlue());
    						 blueAdjuster.setValue(panel.getBackground().getBlue());
    						 riceField.setText("Alpha: " + panel.getBackground().getAlpha());
    						 alphaAdjuster.setValue(panel.getBackground().getAlpha());
     
    					 }
    				 });
     
    		 color2.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(green);
    						 hayField.setText("Red: " + panel.getBackground().getRed());
    						 redAdjuster.setValue(panel.getBackground().getRed());
    						 wheatField.setText("Green: " + panel.getBackground().getGreen());
    						 greenAdjuster.setValue(panel.getBackground().getGreen());
    						 cottonField.setText("Blue: " + panel.getBackground().getBlue());
    						 blueAdjuster.setValue(panel.getBackground().getBlue());
    						 riceField.setText("Alpha: " + panel.getBackground().getAlpha());
    						 alphaAdjuster.setValue(panel.getBackground().getAlpha());
    					 }
    				 });
     
    		 color3.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(blue);
    						 hayField.setText("Red: " + panel.getBackground().getRed());
    						 redAdjuster.setValue(panel.getBackground().getRed());
    						 wheatField.setText("Green: " + panel.getBackground().getGreen());
    						 greenAdjuster.setValue(panel.getBackground().getGreen());
    						 cottonField.setText("Blue: " + panel.getBackground().getBlue());
    						 blueAdjuster.setValue(panel.getBackground().getBlue());
    						 riceField.setText("Alpha: " + panel.getBackground().getAlpha());
    						 alphaAdjuster.setValue(panel.getBackground().getAlpha());
    					 }
    				 });
     
    		 color4.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(orange);
    						 hayField.setText("Red: " + panel.getBackground().getRed());
    						 redAdjuster.setValue(panel.getBackground().getRed());
    						 wheatField.setText("Green: " + panel.getBackground().getGreen());
    						 greenAdjuster.setValue(panel.getBackground().getGreen());
    						 cottonField.setText("Blue: " + panel.getBackground().getBlue());
    						 blueAdjuster.setValue(panel.getBackground().getBlue());
    						 riceField.setText("Alpha: " + panel.getBackground().getAlpha());
    						 alphaAdjuster.setValue(panel.getBackground().getAlpha());
    					 }
    				 });
     
    		 color5.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(yellow);
    						 hayField.setText("Red: " + panel.getBackground().getRed());
    						 redAdjuster.setValue(panel.getBackground().getRed());
    						 wheatField.setText("Green: " + panel.getBackground().getGreen());
    						 greenAdjuster.setValue(panel.getBackground().getGreen());
    						 cottonField.setText("Blue: " + panel.getBackground().getBlue());
    						 blueAdjuster.setValue(panel.getBackground().getBlue());
    						 riceField.setText("Alpha: " + panel.getBackground().getAlpha());
    						 alphaAdjuster.setValue(panel.getBackground().getAlpha());
    					 }
    				 });
     
    		 color6.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(pink);
    						 hayField.setText("Red: " + panel.getBackground().getRed());
    						 redAdjuster.setValue(panel.getBackground().getRed());
    						 wheatField.setText("Green: " + panel.getBackground().getGreen());
    						 greenAdjuster.setValue(panel.getBackground().getGreen());
    						 cottonField.setText("Blue: " + panel.getBackground().getBlue());
    						 blueAdjuster.setValue(panel.getBackground().getBlue());
    						 riceField.setText("Alpha: " + panel.getBackground().getAlpha());
    						 alphaAdjuster.setValue(panel.getBackground().getAlpha());
    					 }
    				 });
     
    		 color7.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(black);
    						 hayField.setText("Red: " + panel.getBackground().getRed());
    						 redAdjuster.setValue(panel.getBackground().getRed());
    						 wheatField.setText("Green: " + panel.getBackground().getGreen());
    						 greenAdjuster.setValue(panel.getBackground().getGreen());
    						 cottonField.setText("Blue: " + panel.getBackground().getBlue());
    						 blueAdjuster.setValue(panel.getBackground().getBlue());
    						 riceField.setText("Alpha: " + panel.getBackground().getAlpha());
    						 alphaAdjuster.setValue(panel.getBackground().getAlpha());
    					 }
    				 });
     
    		 color8.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(gray);
    						 hayField.setText("Red: " + panel.getBackground().getRed());
    						 redAdjuster.setValue(panel.getBackground().getRed());
    						 wheatField.setText("Green: " + panel.getBackground().getGreen());
    						 greenAdjuster.setValue(panel.getBackground().getGreen());
    						 cottonField.setText("Blue: " + panel.getBackground().getBlue());
    						 blueAdjuster.setValue(panel.getBackground().getBlue());
    						 riceField.setText("Alpha: " + panel.getBackground().getAlpha());
    						 alphaAdjuster.setValue(panel.getBackground().getAlpha());
    					 }
    				 });
     
    		 color9.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(lightGray);
    						 hayField.setText("Red: " + panel.getBackground().getRed());
    						 redAdjuster.setValue(panel.getBackground().getRed());
    						 wheatField.setText("Green: " + panel.getBackground().getGreen());
    						 greenAdjuster.setValue(panel.getBackground().getGreen());
    						 cottonField.setText("Blue: " + panel.getBackground().getBlue());
    						 blueAdjuster.setValue(panel.getBackground().getBlue());
    						 riceField.setText("Alpha: " + panel.getBackground().getAlpha());
    						 alphaAdjuster.setValue(panel.getBackground().getAlpha());
    					 }
    				 });
     
    		 color10.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(darkGray);
    						 hayField.setText("Red: " + panel.getBackground().getRed());
    						 redAdjuster.setValue(panel.getBackground().getRed());
    						 wheatField.setText("Green: " + panel.getBackground().getGreen());
    						 greenAdjuster.setValue(panel.getBackground().getGreen());
    						 cottonField.setText("Blue: " + panel.getBackground().getBlue());
    						 blueAdjuster.setValue(panel.getBackground().getBlue());
    						 riceField.setText("Alpha: " + panel.getBackground().getAlpha());
    						 alphaAdjuster.setValue(panel.getBackground().getAlpha());
    					 }
    				 });
     
    		 color11.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(magenta);
    						 hayField.setText("Red: " + panel.getBackground().getRed());
    						 redAdjuster.setValue(panel.getBackground().getRed());
    						 wheatField.setText("Green: " + panel.getBackground().getGreen());
    						 greenAdjuster.setValue(panel.getBackground().getGreen());
    						 cottonField.setText("Blue: " + panel.getBackground().getBlue());
    						 blueAdjuster.setValue(panel.getBackground().getBlue());
    						 riceField.setText("Alpha: " + panel.getBackground().getAlpha());
    						 alphaAdjuster.setValue(panel.getBackground().getAlpha());
    					 }
    				 });
     
    		 color12.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(cyan);
    						 hayField.setText("Red: " + panel.getBackground().getRed());
    						 redAdjuster.setValue(panel.getBackground().getRed());
    						 wheatField.setText("Green: " + panel.getBackground().getGreen());
    						 greenAdjuster.setValue(panel.getBackground().getGreen());
    						 cottonField.setText("Blue: " + panel.getBackground().getBlue());
    						 blueAdjuster.setValue(panel.getBackground().getBlue());
    						 riceField.setText("Alpha: " + panel.getBackground().getAlpha());
    						 alphaAdjuster.setValue(panel.getBackground().getAlpha());
    					 }
    				 });
     
    		 color13.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(white);
    						 hayField.setText("Red: " + panel.getBackground().getRed());
    						 redAdjuster.setValue(panel.getBackground().getRed());
    						 wheatField.setText("Green: " + panel.getBackground().getGreen());
    						 greenAdjuster.setValue(panel.getBackground().getGreen());
    						 cottonField.setText("Blue: " + panel.getBackground().getBlue());
    						 blueAdjuster.setValue(panel.getBackground().getBlue());
    						 riceField.setText("Alpha: " + panel.getBackground().getAlpha());
    						 alphaAdjuster.setValue(panel.getBackground().getAlpha());
    					 }
    				 });
     
    		 color14.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(red);
    						 pageNumber.setBackground(red);
    					 }
    				 });
     
    		 color15.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(green);
    						 pageNumber.setBackground(green);
    					 }
    				 });
     
    		 color16.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(blue);
    						 pageNumber.setBackground(blue);
    					 }
    				 });
     
    		 color17.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(orange);
    						 pageNumber.setBackground(orange);
    					 }
    				 });
     
    		 color18.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(yellow);
    						 pageNumber.setBackground(yellow);
    					 }
    				 });
     
    		 color19.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(pink);
    						 pageNumber.setBackground(pink);
    					 }
    				 });
     
    		 color20.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(black);
    						 pageNumber.setBackground(black);
    					 }
    				 });
     
    		 color21.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(gray);
    						 pageNumber.setBackground(gray);
    					 }
    				 });
     
    		 color22.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(lightGray);
    						 pageNumber.setBackground(lightGray);
    					 }
    				 });
     
    		 color23.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(darkGray);
    						 pageNumber.setBackground(darkGray);
    					 }
    				 });
     
    		 color24.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(magenta);
    						 pageNumber.setBackground(magenta);
    					 }
    				 });
     
    		 color25.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(cyan);
    						 pageNumber.setBackground(cyan);
    					 }
    				 });
     
    		 color26.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(white);
    						 pageNumber.setBackground(white);
    					 }
    				 });
     
    		 color27.addActionListener(
    				 new ActionListener() {
     
    					@Override
    					public void actionPerformed(ActionEvent e) {
    						// TODO Auto-generated method stub
    						panel.setBackground(brown);
    						hayField.setText("Red: " + panel.getBackground().getRed());
    						 redAdjuster.setValue(panel.getBackground().getRed());
    						 wheatField.setText("Green: " + panel.getBackground().getGreen());
    						 greenAdjuster.setValue(panel.getBackground().getGreen());
    						 cottonField.setText("Blue: " + panel.getBackground().getBlue());
    						 blueAdjuster.setValue(panel.getBackground().getBlue());
    						 riceField.setText("Alpha: " + panel.getBackground().getAlpha());
    						 alphaAdjuster.setValue(panel.getBackground().getAlpha());
    					}
     
    				 });
     
    		 color28.addActionListener(
    				 new ActionListener(){
    					 public void actionPerformed(ActionEvent e)
    					 {
    						 panel.setBackground(purple);
    						 hayField.setText("Red: " + panel.getBackground().getRed());
    						 redAdjuster.setValue(panel.getBackground().getRed());
    						 wheatField.setText("Green: " + panel.getBackground().getGreen());
    						 greenAdjuster.setValue(panel.getBackground().getGreen());
    						 cottonField.setText("Blue: " + panel.getBackground().getBlue());
    						 blueAdjuster.setValue(panel.getBackground().getBlue());
    						 riceField.setText("Alpha: " + panel.getBackground().getAlpha());
    						 alphaAdjuster.setValue(panel.getBackground().getAlpha());
    					 }
    				 });
     
    		 color29.addActionListener(
    				 new ActionListener() {
    					 public void actionPerformed(ActionEvent e)
    					 {
    						 area.setBackground(brown);
    						 pageNumber.setBackground(brown);
    					 }
    				 });
     
    		 color30.addActionListener(
    				 new ActionListener() {
    					 public void actionPerformed(ActionEvent e)
    					 {
    						 area.setBackground(purple);
    						 pageNumber.setBackground(purple);
    					 }
    				 });
     
    		 clickOnMe.addMouseListener(
    				 new MouseListener(){
     
    					@Override
    					public void mouseClicked(MouseEvent arg0) {
    						// TODO Auto-generated method stub
    						lollipop.setVisible(true);
    						cornField.setBackground(green);
     
    					}
     
    					@Override
    					public void mouseEntered(MouseEvent arg0) {
    						// TODO Auto-generated method stub
     
    					}
     
    					@Override
    					public void mouseExited(MouseEvent arg0) {
    						// TODO Auto-generated method stub
     
    					}
     
    					@Override
    					public void mousePressed(MouseEvent arg0) {
    						// TODO Auto-generated method stub
     
    					}
     
    					@Override
    					public void mouseReleased(MouseEvent arg0) {
    						// TODO Auto-generated method stub
     
    					}
     
    				 });
     
    		 color31.addActionListener(
    				 new ActionListener() {
    					 public void actionPerformed(ActionEvent f)
    					 {
    						Color c = panel.getBackground().brighter();
    						panel.setBackground(c);
    						hayField.setText("Red: " + panel.getBackground().getRed());
    						 redAdjuster.setValue(panel.getBackground().getRed());
    						 wheatField.setText("Green: " + panel.getBackground().getGreen());
    						 greenAdjuster.setValue(panel.getBackground().getGreen());
    						 cottonField.setText("Blue: " + panel.getBackground().getBlue());
    						 blueAdjuster.setValue(panel.getBackground().getBlue());
    						 riceField.setText("Alpha: " + panel.getBackground().getAlpha());
    						 alphaAdjuster.setValue(panel.getBackground().getAlpha());
    					 }
    				 });
    		 color32.addActionListener(
    				 new ActionListener() {
    					 public void actionPerformed(ActionEvent a)
    					 {
    						 Color d = panel.getBackground().darker();
    						 panel.setBackground(d);
    						 hayField.setText("Red: " + panel.getBackground().getRed());
    						 redAdjuster.setValue(panel.getBackground().getRed());
    						 wheatField.setText("Green: " + panel.getBackground().getGreen());
    						 greenAdjuster.setValue(panel.getBackground().getGreen());
    						 cottonField.setText("Blue: " + panel.getBackground().getBlue());
    						 blueAdjuster.setValue(panel.getBackground().getBlue());
    						 riceField.setText("Alpha: " + panel.getBackground().getAlpha());
    						 alphaAdjuster.setValue(panel.getBackground().getAlpha());
    					 }
    				 });
     
     
    		 colors.addItemListener(
    				 new ItemListener() {
    					 public void itemStateChanged(ItemEvent e)
    					 { // beginning of method
    						 if (e.getSource() == colors)
    						 { // beginning of if
    							 if (colors.getSelectedItem() == "Red")
    							 { // beginning of if
    								 if(e.getStateChange() == ItemEvent.SELECTED)
    								 { // beginning of if
    									 area.setForeground(red);
    									 pageNumber.setForeground(red);
     
    								 } // end of if
     
    								 if (e.getStateChange() == ItemEvent.DESELECTED)
    								 { // beginning of if
    									 // do nothing
    								 } // end of if
     
    							 } // end of if
     
     
    						 if (colors.getSelectedItem() == "Green")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(green);
    								 pageNumber.setForeground(green);
     
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 if (colors.getSelectedItem() == "Blue")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(blue);
    								 pageNumber.setForeground(blue);
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 if (colors.getSelectedItem() == "Orange")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(orange);
    								 pageNumber.setForeground(orange);
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 if (colors.getSelectedItem() == "Yellow")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(yellow);
    								 pageNumber.setForeground(yellow);
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 if (colors.getSelectedItem() == "Pink")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(pink);
    								 pageNumber.setForeground(pink);
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 if (colors.getSelectedItem() == "Black")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(black);
    								 pageNumber.setForeground(black);
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 if (colors.getSelectedItem() == "Gray")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(gray);
    								 pageNumber.setForeground(gray);
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 if (colors.getSelectedItem() == "Light Gray")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(lightGray);
    								 pageNumber.setForeground(lightGray);
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 if (colors.getSelectedItem() == "Dark Gray")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(darkGray);
    								 pageNumber.setForeground(darkGray);
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 if (colors.getSelectedItem() == "Magenta")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(magenta);
    								 pageNumber.setForeground(magenta);
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 if (colors.getSelectedItem() == "Cyan")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(cyan);
    								 pageNumber.setForeground(cyan);
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 if (colors.getSelectedItem() == "White")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(white);
    								 pageNumber.setForeground(white);
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 if (colors.getSelectedItem() == "Brown")
    						 {
    							 if (e.getStateChange() == ItemEvent.SELECTED)
    							 {
    								 area.setForeground(brown);
    								 pageNumber.setForeground(brown);
    							 }
    						 }
     
    						 if(colors.getSelectedItem() == "Purple")
    						 {
    							 if (e.getStateChange() == ItemEvent.SELECTED)
    							 {
    								 area.setForeground(purple);
    								 pageNumber.setForeground(purple);
    							 }
    						 }
    						 } // end of if
    				 }});
     
    		 panel.setLayout(new BoxLayout(panel, 1));
     
    		 addWindowListener(
    				 new WindowListener(){
     
    					@Override
    					public void windowActivated(WindowEvent window) {
    						// TODO Auto-generated method stub
     
    					}
     
    					@Override
    					public void windowClosed(WindowEvent window) {
    						// TODO Auto-generated method stub
     
    					}
     
    					@Override
    					public void windowClosing(WindowEvent window) {
    						// TODO Auto-generated method stub
    						Object[] closing=  {"Yes" , "No"};
    						int a = JOptionPane.showOptionDialog(frame2,
    								"Exit.",
    								"Are you sure you want to exit?",
    								JOptionPane.YES_NO_OPTION,
    								JOptionPane.QUESTION_MESSAGE,
    								null,        closing,     closing[0]); 
    						frame2.setBackground(green);
    						frame2.setForeground(green);
    						frame2.setFont(Aharoni);
    						frame2.setVisible(true);
    						if (a == JOptionPane.YES_OPTION) 
    						{
    							System.exit(0);
    						}
     
    						else if (a == JOptionPane.NO_OPTION)
    						{ // beginning of else if
     
    							setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    						} // end of else if
     
    					}
     
    					@Override
    					public void windowDeactivated(WindowEvent window) {
    						// TODO Auto-generated method stub
     
    					}
     
    					@Override
    					public void windowDeiconified(WindowEvent window) {
    						// TODO Auto-generated method stub
     
    					}
     
    					@Override
    					public void windowIconified(WindowEvent arg0) {
    						// TODO Auto-generated method stub
     
    					}
     
    					@Override
    					public void windowOpened(WindowEvent arg0) {
    						// TODO Auto-generated method stub
    						String str = "Welcome to the hymn book song selector.  Choose from" + "\n" +
    						" different options to set background color, text area color, font color, and font type." + "\n" +
    						"  Chose the songs from the list or enter page number to get songs.  It will show all verses as I don't" + "\n" +
    						" have time to set it to do it for each verse so you have to scroll down to get to the next verses.";
    						JOptionPane.showMessageDialog(null, str, "Welcome", JOptionPane.INFORMATION_MESSAGE);
     
     
    					}
     
    				 });
     
    		 setFocusable(true);
    		 addKeyListener(
    				 new KeyListener() {
     
    					@Override
    					public void keyPressed(KeyEvent arg0) {
    						// TODO Auto-generated method stub
     
    							if (arg0.getKeyCode() == KeyEvent.VK_ESCAPE)
    							{
    							Object[] Esc=  {"Yes" , "No"};
    							int a = JOptionPane.showOptionDialog(frame2,
    									"Exit.",
    									"Are you sure you want to exit?",
    									JOptionPane.YES_NO_OPTION,
    									JOptionPane.QUESTION_MESSAGE,
    									null,        Esc,     Esc[0]); 
    							if (a == JOptionPane.YES_OPTION) 
    							{
    								System.exit(0);
    							}
     
    							else if (a == JOptionPane.NO_OPTION)
    							{ // beginning of else if
     
    								setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    							} // end of else if
    						}
     
    							if (arg0.getKeyCode() == KeyEvent.VK_H || arg0.getKeyCode() == KeyEvent.VK_F1)
    							{
    								lollipop.show(null, 100, 100);
    								lollipop.setVisible(true);
    							} 
     
     
     
     
    					}
     
    					@Override
    					public void keyReleased(KeyEvent arg0) {
    						// TODO Auto-generated method stub
     
    					}
     
    					@Override
    					public void keyTyped(KeyEvent arg0) {
    						// TODO Auto-generated method stub
     
    					}
     
    				 });
     
    		 button6.addActionListener(
    				 new ActionListener() {
     
    					@Override
    					public void actionPerformed(ActionEvent arg0) {
    						// TODO Auto-generated method stub
    						Object[] leavingMenu=  {"Yes" , "No"};
    						int a = JOptionPane.showOptionDialog(frame2,
    								"Exit Menu.",
    								"Are you sure you want to exit?",
    								JOptionPane.YES_NO_OPTION,
    								JOptionPane.QUESTION_MESSAGE,
    								null,        leavingMenu,     leavingMenu[0]); 
    						if (a == JOptionPane.YES_OPTION) 
    						{
     
    							lollipop.setVisible(false);
    						}
     
    						else if (a == JOptionPane.NO_OPTION)
    						{ // beginning of else if
                            lollipop.setVisible(true);
     
     
    						} // end of else if	
     
    					}
     
    				 });
     
    		 fonts.addItemListener(
    				 new ItemListener() {
    					 public void itemStateChanged(ItemEvent e)
    					 { // beginning of method
    					if (e.getSource() == fonts)
    					{ // beginning of if
    						if (fonts.getSelectedItem() == "Aharoni")
    						{ // beginning of if
    							if(e.getStateChange() == ItemEvent.SELECTED)
    							{ // beginning of if
    								area.setFont(Aharoni);
     
    							} // end of if
    						} // end of if
     
    						if (fonts.getSelectedItem() == "Andalus")
    						{ // beginning of if
    							if (e.getStateChange() == ItemEvent.SELECTED)
    							{ // beginning of if
    								area.setFont(Andalus);
    							} // end of if
     
    						} // end of if
    					} // end of if
    					 } // end of method
    				 });
     
     
     
    		 	 getContentPane().add(scrolls2);
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    		// TODO Auto-generated constructor stub
    	}
     
    	public void showGUI2(){
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(300,300);
    		setLocation(100,100);
    		setVisible(true);
    	} 
     
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    	/* (non-Javadoc)
    	 * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
    	 */
    	@Override
    	public void itemStateChanged(ItemEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    	/* (non-Javadoc)
    	 * @see javax.swing.event.MenuListener#menuCanceled(javax.swing.event.MenuEvent)
    	 */
    	@Override
    	public void menuCanceled(MenuEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    	/* (non-Javadoc)
    	 * @see javax.swing.event.MenuListener#menuDeselected(javax.swing.event.MenuEvent)
    	 */
    	@Override
    	public void menuDeselected(MenuEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    	/* (non-Javadoc)
    	 * @see javax.swing.event.MenuListener#menuSelected(javax.swing.event.MenuEvent)
    	 */
    	@Override
    	public void menuSelected(MenuEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
         LivingHymns hymn = new LivingHymns();
         hymn.showGUI2();
     
    	}
     
    	@Override
    	public int print(Graphics g, PageFormat pf, int pageIndex)
        throws PrinterException {
     
    Font font = new Font("Serif", Font.PLAIN, 10);
    FontMetrics metrics = g.getFontMetrics(font);
    int lineHeight = metrics.getHeight();
     
    if (pageBreaks == null) {
       initTextLines();
       int linesPerPage = (int)(pf.getImageableHeight()/lineHeight);
       int numBreaks = (textLines.length-1)/linesPerPage;
       pageBreaks = new int[numBreaks];
       for (int b=0; b<numBreaks; b++) {
           pageBreaks[b] = (b+1)*linesPerPage; 
       }
    }
     
    if (pageIndex > pageBreaks.length) {
       return NO_SUCH_PAGE;
    }
     
    /* User (0,0) is typically outside the imageable area, so we must
    * translate by the X and Y values in the PageFormat to avoid clipping
    * Since we are drawing text we
    */
    Graphics2D g2d = (Graphics2D)g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());
     
    /* Draw each line that is on this page.
    * Increment 'y' position by lineHeight for each line.
    */
    int y = 0; 
    int start = (pageIndex == 0) ? 0 : pageBreaks[pageIndex-1];
    int end   = (pageIndex == pageBreaks.length)
                    ? textLines.length : pageBreaks[pageIndex];
    for (int line=start; line<end; line++) {
       y += lineHeight;
       g.drawString(textLines[line], 0, y);
    }
     
    /* tell the caller that this page is part of the printed document */
    return PAGE_EXISTS;
    }
     
     
     
    }
    Just look at the save ActionListener part.

    I only added the imports and stuff because you keep asking for them.

    Also, when I tell it to exit and then tell it no, it opens a blank JFrame. I'm wondering if it's saving but somehow saving nothing. Also, why is it doing that when I exit?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    Your program is too large to work with.
    Can you make a small program that compiles and executes to demonstrate the problem?

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    Ok, here's a question.

    Where do you tell it what data to save to the file?

    I dont know anything about saving using JFileChooser, but I can google. See if the saveFile() method helps you with actually telling the file what to contain: Java JFileChooser Examples

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    Where do you tell it what data to save to the file?
    Hard question to answer.
    The quick answer is somewhere in your code.
    The "what data" is really hard. What do you want to be able to retrieve later?

    I dont know anything about saving using JFileChooser,
    JFileChooser is a way to have the user select where to write a file. That's what it does, let the user tell the program where or if.
    You can logically replace using that by just hard coding a path in your program.
    Given a path to where the file should go (choosen or hardcoded) then you write the file at that path.

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    I want the everything to be there. For instance, make a JTable, add text to it, hit save, and then load it and have the text still in it. Plus I wouldn't mind having the background and the JPanel come up too. I basically, though I quite realize to do so might well be anything but basic, want to have it show what I had made originally in the program come up, but with certain text fields, textAreas, and JTables, that's for another one that I'm practicing here first before I make it, to be there.

    Basically to almost get an auto setText() for certain things. Like saving the text and settings in a Microsoft Word document is about the best I can can describe what I'm trying to do.


    Also, what does FileOutputStream class do?

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    I usually hardcode my output paths and use JFileChoosers for my input paths. It actually never occured to me that I could let the user select where it goes. However, usually the only file manipulation I do involves either directly editing the input file or creating an output file identical to my input file with a few changes and preserve the input file. So my files usually go in the same directory as the input file and I just never thought about it.

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    Quote Originally Posted by javapenguin View Post
    Also, what does FileOutputStream class do?
    A file output stream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open.

    FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter.

    Got that from the API. It actually makes sense, which is more than can be said for most of the class descriptions.


    I would think that you need to figure out how to write to either .doc,docx, or .rtf and use those formats to write your files. Or, some would criticize this, but if you wanted the largest range of formatting options, .xlsx would probably be the best.

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Can't get it to save stuff with JFileChooser

    If you are working to save certain data into a file, you must first settle on a specific file type so that you know where and what things are when you reopen the file. Just saving it to a file is one thing, but having the means to make sense of it is a whole other issue - especially when you are talking about saving data from multiple components. One option you may have is to Serialize the classes into a given file....this will allow you to use an ObjectOutputStream and ObjectInputStream and rely on the java virtual machine to do the file writing and reading, all you need to do is tell the stream which object(s) to read write (the drawback to this method can come into play later when making changes to objects - previously saved files may not be forward compatible).

    As to why your code results in emtpy files, I concur with Norm that it is much easier to lead you in the right direction if you can boil that code down to as few lines as possible to reproduce the problem you are having.

  9. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (August 4th, 2010)

  10. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Thumbs up Re: Can't get it to save stuff with JFileChooser

    Quote Originally Posted by copeg View Post
    If you are working to save certain data into a file, you must first settle on a specific file type so that you know where and what things are when you reopen the file. Just saving it to a file is one thing, but having the means to make sense of it is a whole other issue - especially when you are talking about saving data from multiple components. One option you may have is to Serialize the classes into a given file....this will allow you to use an ObjectOutputStream and ObjectInputStream and rely on the java virtual machine to do the file writing and reading, all you need to do is tell the stream which object(s) to read write (the drawback to this method can come into play later when making changes to objects - previously saved files may not be forward compatible).

    As to why your code results in emtpy files, I concur with Norm that it is much easier to lead you in the right direction if you can boil that code down to as few lines as possible to reproduce the problem you are having.
    Is there a way I can add the Components to the saved version? How do you Serialize something?

    Suppose I have a JPanel, which has a title as class extends JFrame, a JTextArea, and a couple of buttons.

    How do you tell the saver
    save
    {
     
    Panel
     
    Panel.setVisible(true);
     
    JTextArea
     
    JTextArea setVisible(true);
     
    JTextArea(setText(userInputtedText));
     
    add(button);
    add(button2);
    add(button3);
     
    button.setVisible(true);
    button2.setVisible(true);
    button3.setVisible(true);
     
    add(button's ActionListener());
     
    add(button2's ActionListener());
     
    add(button3's ActionListener());
     
    }
    Last edited by helloworld922; August 4th, 2010 at 04:04 PM.

  11. #10
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    Quote Originally Posted by copeg View Post
    If you are working to save certain data into a file, you must first settle on a specific file type so that you know where and what things are when you reopen the file. Just saving it to a file is one thing, but having the means to make sense of it is a whole other issue - especially when you are talking about saving data from multiple components. One option you may have is to Serialize the classes into a given file....this will allow you to use an ObjectOutputStream and ObjectInputStream and rely on the java virtual machine to do the file writing and reading, all you need to do is tell the stream which object(s) to read write (the drawback to this method can come into play later when making changes to objects - previously saved files may not be forward compatible).

    As to why your code results in emtpy files, I concur with Norm that it is much easier to lead you in the right direction if you can boil that code down to as few lines as possible to reproduce the problem you are having.
    What do you mean previously saved files may not be forward compatible? Are you saying that once saved, the user can't keep saving over it and updating it?

    import java.awt.color.ColorSpace;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.AdjustmentEvent;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseEvent;
     
    import javax.swing.event.MenuEvent;
    import javax.swing.event.MenuListener;
    import javax.swing.event.PopupMenuEvent;
    import javax.swing.event.PopupMenuListener;
    import javax.swing.filechooser.FileSystemView;
    import javax.swing.text.DefaultStyledDocument;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.ScrollPaneConstants;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    import java.util.*;
    import java.util.jar.JarFile;
    import java.io.*;
     
    import javax.swing.ImageIcon;
     
     
    import java.awt.event.WindowEvent;
    import java.awt.print.PageFormat;
    import java.awt.print.Printable;
    import java.awt.print.PrinterException;
    import java.awt.print.PrinterJob;
    import java.awt.Window;
    import javax.swing.JScrollPane;
    import java.awt.Component;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import acm.io.*;
    import acm.graphics.*;
    import acm.program.*;
    import acm.gui.*;
    import acm.util.*;
    import java.awt.Container;
    import java.awt.PrintJob;
    import java.awt.print.*
     
    public class AClass extends JFrame
    {
     
    File file;
     
    JFileChooser chooser;
     
    JPanel panel;
     
    JMenuBar bar;
     
    JMenu menu;
     
    JMenuItem save;
    JTextArea area;
     
    JButton button, button2, button3;
     
    JFrame openFrame;
     
    ObjectOutputStream ouputStream;
     
     
    public AClass()
    {
    setTitle("Hello World!");
    panel = new JPanel();
    openFrame = new JFrame();
     
    bar = new JMenuBar();
     
    menu = new JMenu("File");
     
    bar.add(menu);
     
    menu.setVisible(true);
    bar.setVisible(true);
     
    save = new JMenuItem("Save");
     
    bar.add(save);
     
    save.setVisible(true);
     
    panel.add(bar);
     
    button = new JButton("Button");
    button2 = new JButton("Button 2");
    button3 = new JButton("Button 3");
     
    button.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    JOptionPane.showMessageDialog("Hello", "You clicked button 1", JOptionPane.INFORMATION_MESSAGE);
     
    }
    });
     
    button2.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    JOptionPane.showMessageDialog("Hello", "You clicked button 2", JOptionPane.INFORMATION_MESSAGE);
     
    }
    });
     
    button3.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    JOptionPane.showMessageDialog("Hello", "You clicked button 3", JOptionPane.INFORMATION_MESSAGE);
     
    }
    });
     
    button.setVisible(true);
    button2.setVisible(true);
    button3.setVisible(true);
     
    panel.add(button);
    panel.add(button2);
    panel.add(button3);
     
    save.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    chooser = new FileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    chooser.showSaveDialog(openFrame);
    file = chooser.getSelectedFile();
    // how do I serialize classes and make them into objects and save them?  
     
    }
     
    });
     
    getContentPane(panel);
    }
     
    public static void main(String[] args)
    {
    AClass refVar = new AClass();
    }
    }

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    I'd suggest that you write a small test program with a few simple classes and work on writing them to a disk file using a ObjectOutputStream and then read them back with a ObjectInputStream and verify that you get back all of what was written out.
    No GUI needed. Use println() to show success or failure.
    When the technique is mastered, then move it into the larger program.
    Last edited by Norm; August 4th, 2010 at 04:16 PM. Reason: Wrong class used

  13. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (August 4th, 2010)

  14. #12
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Can't get it to save stuff with JFileChooser

    JFileChooser only lets you select one or more files/directories. The actual saving out of the data is a separate issue and should behave exactly the same as when using a hard-coded path.

    In order to serialize an object, you just need to have the object implement the Serializable interface (easy), and also make sure every component inside of that object is Serializable (hard because you have to include all fields that get inherited).

    To write out an object, simple create an ObjectOutputStream to the file in question, then call writeObject() on the object you want to write out.

    For an example of this, see Writing Objects to a File with ObjectOutputStream. Reading objects back in has a similar idea. See: Reading objects from file using ObjectInputStream.

  15. The Following User Says Thank You to helloworld922 For This Useful Post:

    javapenguin (August 4th, 2010)

  16. #13
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Question Re: Can't get it to save stuff with JFileChooser

    Quote Originally Posted by helloworld922 View Post
    JFileChooser only lets you select one or more files/directories. The actual saving out of the data is a separate issue and should behave exactly the same as when using a hard-coded path.

    In order to serialize an object, you just need to have the object implement the Serializable interface (easy), and also make sure every component inside of that object is Serializable (hard because you have to include all fields that get inherited).

    To write out an object, simple create an ObjectOutputStream to the file in question, then call writeObject() on the object you want to write out.

    For an example of this, see Writing Objects to a File with ObjectOutputStream. Reading objects back in has a similar idea. See: Reading objects from file using ObjectInputStream.
    What do you mean that I have to include all of the fields that get inherited? Also, can it only be saved once and not saved over? What's not forward compatible? By fields, do you mean if I have a text area and it gets text I have to serialize the text too, or do you mean all the fields, like INFORMATION_MESSAGE, or HORIZONTAL_SCROLLBAR_ALWAYS, etc?

    Also, does any class have a method to check to see if a Class/component is serializable?
    Last edited by javapenguin; August 5th, 2010 at 07:15 PM.

  17. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    a method to check to see if a Class/component is serializable?
    Try instanceof to test a class.
    Last edited by Norm; August 5th, 2010 at 07:24 PM.

  18. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (August 5th, 2010)

  19. #15
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Can't get it to save stuff with JFileChooser

    if(theObject instanceof Serializable)
    {
        // the object implements Serializable
    }

  20. The Following User Says Thank You to helloworld922 For This Useful Post:

    javapenguin (August 5th, 2010)

  21. #16
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Smile Re: Can't get it to save stuff with JFileChooser

    Quote Originally Posted by helloworld922 View Post
    if(theObject instanceof Serializable)
    {
        // the object implements Serializable
    }
    And if it isn't an instance of Serializable, does that mean I can't use it? If I can use it, what did you mean by "fields"?

    -----------------------------------------Editing-----------------------------------------------------------------
    Hey, I got something to work!

    However, it's not quite what I was hoping for. Maybe if it were .exe or .jar it would work. Also, are a JTextArea and its text Serializable?
    Attached Files Attached Files
    Last edited by javapenguin; August 5th, 2010 at 10:16 PM.

  22. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    if it isn't an instance of Serializable, does that mean I can't use it
    Yes, you can only serialize objects that are.

  23. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (August 6th, 2010)

  24. #18
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Post Re: Can't get it to save stuff with JFileChooser

    Quote Originally Posted by Norm View Post
    Yes, you can only serialize objects that are.
    I thought I could actually, but I could only if I serialized all of the fields and all the fields it inherited.

    So how do you write Objects that aren't serializable?

    ---------------Editing-------------------------

    Hey, I just thought of something. Couldn't I just, if a class doesn't implement Serializable, could I make a subclass of it that does and thus be able to write it?

    For instance, say JTextArea won't serialize. Could I make a subclass called myJTextArea that extends JTextArea and that implements Serializable?
    Last edited by javapenguin; August 6th, 2010 at 09:43 AM. Reason: I got an ideer!

  25. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    Best way to answer all these "what if" questions is to write a small test program and see what happens.

  26. #20
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Lightbulb Re: Can't get it to save stuff with JFileChooser

    I have no clue what sort of file a regular GUI would be compatible with. I tried .rtf and it gave me just a Word doc with code, though it wasn't the same code, exactly anyway, that I had used to make the components. i tried jar and it didn't like it. I tried .exe and it didn't like it. Do I have to write something to make it loadable from anywhere or just from the program itself?

    How do you get it to "de-serialize" stuff?]

    import java.util.*;
    import java.io.*;
    import java.awt.event;
    import java.awt;
    import javax.swing;
     
    public class loadNSave extends JFrame implements Serializable
    {
    JButton button, button2, button3, button4, button5;
    JTextArea area;
    JMenuBar bar;
    JMenu menu;
    JMenuItem save, load;
    JPanel panel;
    JScrollPane pane, pane2;
    JFrame frame, saveFrame, loadFrame;
    File file, file2;
    ObjectInputStream inputStr;
    ObjectOutputStream outputStr;
    FileInputStream lake;
    FileOutputStream pond;
    JFileChooser chooser, chooser2;
     
    public loadNSave() 
    {
    setTitle("Title");
    saveFrame = new JFrame();
    loadFrame = new JFrame();
    button = new JButton("Button");
    panel = new JPanel();
    pane = new JScrollPane(panel, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    frame = new JFrame();
    button.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    JOptionPane.showMessageDialog(frame, "Message", "Another Message", JOptionPane.INFORMATION_MESSAGE);
    } );
    button2 = new JButton("Button");
    button2.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    JOptionPane.showMessageDialog(frame, "Message", "Another Message", JOptionPane.PLAIN_MESSAGE);
    } );
     
    button3 = new JButton("Button");
    button3.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    JOptionPane.showMessageDialog(frame, "Message", "Another Message", JOptionPane.QUESTION_MESSAGE);
    } );
     
    button4 = new JButton("Button");
    button4.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    JOptionPane.showMessageDialog(frame, "Message", "Another Message", JOptionPane.WARNING_MESSAGE);
    } );
     
    button5 = new JButton("Button):
    button5.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    JOptionPane.showMessageDialog(frame, "Message", "Another Message", JOptionPane.ERROR_MESSAGE);
    } );
     
    area = new JTextArea(100, 100);
    pane2 = new JScrollPane(area, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLBAR_AS_NEEDED);
     
    bar = new JMenuBar();
    menu = new JMenu("File");
    bar.add(menu);
    save = new JMenuItem("Save");
    load = new JMenuItem("Load");
    menu.add(save);
    menu.addSeparator();
    menu.add(load);
    save.addActionListener(
    					new ActionListener() {
     
    						@Override
    						public void actionPerformed(ActionEvent arg0) {
    							// TODO Auto-generated method stub
    						//	saveDialog.setVisible(true);
     
     
     
    							chooser = new JFileChooser();
    							chooser.setApproveButtonToolTipText("Click here to save File");
     
     
    							chooser.setBackground(Color.GREEN);
    							chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
     
    							 chooser.setVisible(true);
    							chooser.showSaveDialog(saveFrame);
    							System.out.println(chooser.getSelectedFile());
    							file = chooser.getSelectedFile();
    							String bob3 = chooser.getSelectedFile().getName();
     
    							System.out.println(file.getFreeSpace());
    							file.setReadable(true);
    							try {
    								lake = new FileOutputStream(file);
    							} catch (FileNotFoundException e) {
    								// TODO Auto-generated catch block
    								e.printStackTrace();
    							}
    							try {
    								outputStr = new ObjectOutputStream(lake);
    							} catch (IOException e) {
    								// TODO Auto-generated catch block
    								e.printStackTrace();
    							}
     
     
    							try {
    								outputStr.writeObject(panel);
    							} catch (IOException e) {
    								// TODO Auto-generated catch block
    								e.printStackTrace();
    							}
     
     
     
    							try {
    								outputStr.writeObject(pane2);
    							} catch (IOException e) {
    								// TODO Auto-generated catch block
    								e.printStackTrace();
    							}
     
    							try {
    								outputStr.writeObject(button);
    							} catch (IOException e) {
    								// TODO Auto-generated catch block
    								e.printStackTrace();
    							}
     
     
    							try {
    								outputStr.close();
    							} catch (IOException e) {
    								// TODO Auto-generated catch block
    								e.printStackTrace();
    							}
     
    						}
     
    					});
     
    load.addActionListener(
    				 new ActionListener() {
    					 public void actionPerformed(ActionEvent e)
    					 {
    						 chooser2 = new JFileChooser();
    						 chooser2.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
     
    						 chooser2.setVisible(true);
    						chooser2.showOpenDialog(openFrame);
     
    						System.out.println(chooser2.getSelectedFile());
    						file2 = chooser2.getSelectedFile();
     
     
    						String bob = file2.getName() + " doesn't exist!";
    						String bob2 = file2.getName() + " does exist.";
     
    						if (file2.exists() == true)
    						{
    							JOptionPane.showMessageDialog(null, "File exists", bob2, JOptionPane.INFORMATION_MESSAGE);
    						}
     
    						else if (file2.exists() == false)
    						{
    							JOptionPane.showMessageDialog(null, "File not found", bob, JOptionPane.ERROR_MESSAGE);
    						}
     
    						try {
    							pond = new FileInputStream(file2);
    						} catch (FileNotFoundException e1) {
    							// TODO Auto-generated catch block
    							e1.printStackTrace();
    						}
    						try {
    							inputStr = new ObjectInputStream(pond);
    						} catch (IOException e1) {
    							// TODO Auto-generated catch block
    							e1.printStackTrace();
    						}
    						try {
    							pond.read();
    						} catch (IOException e1) {
    							// TODO Auto-generated catch block
    							e1.printStackTrace();
    						}
    						try {
    							pond.getFD();
    						} catch (IOException e1) {
    							// TODO Auto-generated catch block
    							e1.printStackTrace();
    						}
    						try {
    							System.out.println(pond.getFD());
    						} catch (IOException e1) {
    							// TODO Auto-generated catch block
    							e1.printStackTrace();
    						}
     
    						try {
    							inputStr.readObject();
    						} catch (IOException e2) {
    							// TODO Auto-generated catch block
    							e2.printStackTrace();
    						} catch (ClassNotFoundException e2) {
    							// TODO Auto-generated catch block
    							e2.printStackTrace();
    						}
     
    						System.out.println(pond.getChannel());
     
    						System.out.println(pond,.getChannel().isOpen());
     
    						System.out.println(file2.canRead());
    						System.out.println(file2.canExecute());
    						System.out.println(file2.getTotalSpace());
    						file2.setExecutable(true);
     
    					 }
    				 });
    panel.add(button);
    panel.setVisible(true);
    panel.add(button2);
    panel.add(button3);
    panel.add(button4);
    panel.add(button5);
    panel.add(pane2);
    panel.add(bar);
     
     
     
     
     
    getContentPane(pane);
    }
     
    public static void main(String[[ args)
    {
    loadNSave ref = new loadNSave();
    }
     
     
    }
    Last edited by javapenguin; August 10th, 2010 at 03:15 PM.

  27. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    If you serialize an object to a file using an output class, you use the corresponding input class to read the object back into a java program and recreate the object as it existed when it was written out. There is nothing you can do with the file that contains the object except read it back into a java program.

  28. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (August 9th, 2010)

  29. #22
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Question Re: Can't get it to save stuff with JFileChooser

    Quote Originally Posted by Norm View Post
    If you serialize an object to a file using an output class, you use the corresponding input class to read the object back into a java program and recreate the object as it existed when it was written out. There is nothing you can do with the file that contains the object except read it back into a java program.
    Ok, but how do you get it to read the objects back in?

    I gave a sample program.

    Last edited by javapenguin; August 9th, 2010 at 08:58 PM.

  30. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    but how do you get it to read the objects back in?
    How did you write them out? Take the class name and replace Output with Input

  31. #24
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    Quote Originally Posted by Norm View Post
    How did you write them out? Take the class name and replace Output with Input
    I know I use ObjectInputStream and FileInputStream

    but what method?

    I tried using an ObjectInputStream.readObject() but it doesn't appear to do what I want.

    Perhaps I need to get it to remove all my current components first before reading in the updated ones.

    Also, when it's reading in, do I need to originally save it in a .java in order to be able to read it back in correctly?

    How do you get default de-serializing?

    Also, if default de-serializing happens to be abstract, what kind of super calls do I need?

    Also, do I have to tell it to close the reading in when it runs out of objects?

  32. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    it doesn't appear to do what I want.
    Please explain "what I want".

    do I need to originally save it in a .java
    No you can use any file extension that you want. Chose one that won't confuse the OS. Like .dat or .ser

    Best way to answer all your questions is to write a small simple program with a small simple class with some data and a get method. Create an object from the small simple class with some data, serialize it to a file, read the object back in and call the method to get the saved data to see it matches what was saved originally.
    Last edited by Norm; August 10th, 2010 at 11:56 AM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Using JFileChooser to open and display an image
    By JavaN0ob in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 31st, 2010, 06:01 PM
  2. Noob, Helloworld stuff not working? Help?
    By Bdavis93 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 7th, 2010, 08:29 PM
  3. JFileChooser
    By FretDancer69 in forum AWT / Java Swing
    Replies: 2
    Last Post: February 3rd, 2010, 06:35 AM
  4. VB can do stuff Java can`t ??
    By JFujy in forum Java Theory & Questions
    Replies: 2
    Last Post: September 22nd, 2009, 10:25 AM
  5. Replies: 2
    Last Post: March 6th, 2009, 03:00 PM