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.

Results 1 to 12 of 12

Thread: Help pleeease

  1. #1
    Member
    Join Date
    Dec 2011
    Posts
    34
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help pleeease

    I created a basic notepad to store anything i need and to load it after, just for learning purposes mainly and can be useful later on. I got everything basically working now, but I want to add a background. It won't show, I'm using a .png file. Also, If you could tell me how I could change the text inside my gui to blue or red? I can only change the background's color, but not the text.

    Any help is appreciated.

    Linking me to any useful tutorial will help.

    Here's the code:
    package gui;
     
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.lang.reflect.InvocationTargetException;
    import java.util.Scanner;
     
    import javax.swing.*;
    import javax.swing.GroupLayout.Alignment;
    import javax.swing.GroupLayout.ParallelGroup;
    import javax.swing.GroupLayout.SequentialGroup;
     
    /**
     * TODO: add some more buttons including clearing the whole screen and file? with a warning message.
     * possibly add something of a search feature.
     * 
     * 
     * @author Rodrigo
     */
    @SuppressWarnings("serial")
    public class NoteMaker extends JFrame implements ActionListener {
     
     
    		private static final String NOTES_DIR = "./Notes/notes.txt";
            private JTextArea text;
            private JButton save;
            private JScrollPane jScrollPane1;
            private String[] loaded = new String[50];
            private JButton red;
            ImageIcon img = new ImageIcon("./icons/background.png");
            private boolean load = false;
     
     
            private static void load() {
            	JOptionPane.showConfirmDialog(null, "Would you like to load you're previous texts?");
            	//TODO: make sure the person clicked yes.
            	loadNP();
            }
     
    	    private  NoteMaker() {
    	    	super("NotePad");
    	    	loadCrap();
    	    	/*
    	    	 * Setting all necessities.
    	    	 */
    	    	setVisible(true);
    	    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	    	setSize(800, 700);
     
    	    	JLabel label = new JLabel(img);
    	    	add(label);
    	    	/*
    	    	 * Setting the text area.
    	    	 */
    	    	text = new JTextArea();
    	    		if(load) {
    	    			for(int j = 0; j < loaded.length; j++) {
    		    		    if(loaded[j] == null)
    		    			continue;
    			    		    text.append(loaded[j]);
    			    		    text.append("\n");
    		    		}
    	    		}
    	    	text.setColumns(20);
    	    	text.setLineWrap(true);
    	    	text.setRows(5);
    	    	text.setFont(new Font("Serif", Font.BOLD, 20));
    	    	text.setFont(new Font("Serif", Font.ITALIC, 20));
    	    	text.setWrapStyleWord(true);
     
    	    	/*
    	    	 * Now setting the save button.
    	    	 */
    	    	save = new JButton("Click me to save.");
    	    	save.addActionListener(this);
     
    	    	/*
    	    	 * Now to set the color red button.
    	    	 */
    	    	red = new JButton("Red");
    	    	red.addActionListener(this);
     
    	    	/*
    	    	 * Sets the scrolling bar.
    	    	 */
    	    	jScrollPane1 = new JScrollPane(text);
    	    	/*
    	    	 * Now position the components.
    	    	 */
    	    	GroupLayout layout = new GroupLayout(getContentPane());
    	    	getContentPane().setLayout(layout);
    	    	ParallelGroup hgroup = layout.createParallelGroup(GroupLayout.Alignment.LEADING);
    	    	SequentialGroup h1 = layout.createSequentialGroup();
    	    	ParallelGroup h2 = layout.createParallelGroup(GroupLayout.Alignment.TRAILING);
    	    	h2.addComponent(jScrollPane1, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE);
    	    	h2.addComponent(save, GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 212, Short.MAX_VALUE);
    	    	h2.addComponent(red, GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 212, Short.MAX_VALUE);
     
    	    	h1.addContainerGap();
    	    	h1.addGroup(h2);
    	    	h1.addContainerGap();
    	    	hgroup.addGroup(Alignment.TRAILING, h1);
    	    	layout.setHorizontalGroup(hgroup);
     
    	    	ParallelGroup vgroup = layout.createParallelGroup(GroupLayout.Alignment.LEADING);
    	    	SequentialGroup v1 = layout.createSequentialGroup();
    	    	v1.addContainerGap();
    	    	v1.addComponent(save);
    	    	v1.addContainerGap();
    	    	v1.addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE);
    	    	v1.addContainerGap();
    	    	v1.addComponent(red);
    	    	v1.addContainerGap();
    	    	vgroup.addGroup(v1);
    	    	layout.setVerticalGroup(vgroup);
    	    }
     
            public void loadCrap() {
        	try {
        	    Scanner scan = new Scanner(new File(NOTES_DIR));
        	    String[] crap = new String[5];
        	    int in = 0;
        	    while(scan.hasNext()) {
    	    		crap[0] = scan.next();
    	    		loaded[in] = crap[0];
    	    		in++;
    	    		crap[0] = null;
        	    }
        	} catch (FileNotFoundException e) {
        	    e.printStackTrace();
        		}
            }
     
            public void saveCrap(String text) {
        	System.out.println(text);
        	BufferedWriter bw;
        	try {
        	    bw = new BufferedWriter(new FileWriter(new File(NOTES_DIR)));
        	    bw.newLine();
        	    bw.write(text);
        	    if(bw != null)
        		bw.close();
        	} catch (IOException e) {
        		e.printStackTrace();
        		}
            }
            /**
             * This will search the files and the text inside the gui already.
             */
            public void search(String text) {
            	File file = new File(NOTES_DIR);
            	if(file.canRead()) {
            		try {
    					Scanner scan = new Scanner(file);
    					while(scan.hasNext()) {
    						if(scan.next().contains(text)) {
    							//TODO: make the search crap feature show up.
    							System.out.println(scan.next());
    						}
    					}
    				} catch (FileNotFoundException e) {
    					e.printStackTrace();
    				}
     
            	}
            }
     
            public static void loadNP() {
            	try {
    				SwingUtilities.invokeAndWait(new Runnable() {
     
    					@Override
    					public void run() {
    						new NoteMaker();
    					}
     
    				});
    			} catch (InvocationTargetException e) {
    				e.printStackTrace();
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
            }
            public static void main(String...args) {
            	load();
            }
     
            private boolean redon = false;
    		@Override
    		/**
    		 * Handle all the actions here.
    		 */
    		public void actionPerformed(ActionEvent e) {
    			if(e.getSource() == red && !redon) {
    				text.setBackground(Color.RED);
    				redon = true;
    			} else if (e.getSource() == red && redon) {
    				redon = false;
    				text.setBackground(Color.BLUE);
    			}
     
    			if(e.getSource() == save) {
    				saveCrap(text.getText());
    	    		/*String[] old = new String[10];
    	    		old[0] = text.getText();
    	    		text.append(old[0]);*/
    			}
    		}
    }

  2. #2
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default Re: Help pleeease

    You might want to check your directories. A small error can always throw you off.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Help pleeease

    how I could change the text
    Look at the various setxxxxx methods available to the JTextArea class. I'm sure there is one that will change the color of the text.

  4. #4
    Member
    Join Date
    Dec 2011
    Posts
    34
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help pleeease

    Quote Originally Posted by Norm View Post
    Look at the various setxxxxx methods available to the JTextArea class. I'm sure there is one that will change the color of the text.
    I looked through it thoroughly, all I've found was the setFont() method.

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Help pleeease

    Don't forget overridden methods like setForeground().

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Help pleeease

    I looked through it thoroughly,
    If you'd look where you found setBackground, you'd see these methods:
    setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setForeground,

  7. #7
    Member
    Join Date
    Dec 2011
    Posts
    34
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help pleeease

    Quote Originally Posted by Norm View Post
    If you'd look where you found setBackground, you'd see these methods:
    I looked through the JTextArea class but not the component class, thank you very much though. Will work with this.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Help pleeease

    Notice that following the method definitions specifically for the class, there are the methods for the classes that are extended.

  9. #9
    Member
    Join Date
    Dec 2011
    Posts
    34
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help pleeease

    Quote Originally Posted by elisha.java View Post
    You might want to check your directories. A small error can always throw you off.
    No it's not that; I'm 100% positive.

    I still need help setting the background.

  10. #10
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Help pleeease

    I still need help setting the background.
    Doesn't setBackground() work?

  11. #11
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Help pleeease

    I think the OP wants a png image as the background.

    @OP: So what have you tried?

  12. #12
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Help pleeease

    Quote Originally Posted by pbrockway2 View Post
    I think the OP wants a png image as the background.

    @OP: So what have you tried?
    To set image, as background. OP needs to make it icon or get it as a toolkit object and then draw using Graphics.