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 19 of 19

Thread: Hi there! I have a project and I need your help dear friends :D

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Hi there! I have a project and I need your help dear friends :D

    I am new here and this is a very simple code since I am just on my first year in college. By the way, I am very sorry if this code is absurd because honestly Im not yet familliar with all of the terms of compiler. Well, my question is How can i add a background image for this code without disappearing the button and label? I want a frame that has a background and still shows the buttons and label that I place. and how can I insert Buffered Reader in this code, this is a file organizer, but it only shows first line of a text file. I want to show the whole text file. And is it possible to also show path of the file? Thank you so much in advance for your help guys!!


    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    import javax.swing.JOptionPane;
    import javax.swing.ImageIcon;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
    public class project extends JFrame implements ActionListener{
    public static final int WIDTH= 700;
    public static final int HEIGHT = 600;
    public static final int NUMBER_OF_CHAR=30;
     
    JLabel labelresult;
     
    private JTextField filenameField;
     
    public project ()
    {
    setSize(WIDTH, HEIGHT);
    Container contentPane = getContentPane();
    contentPane.setLayout(new FlowLayout());
     
    JFrame frame = new JFrame ();
    setTitle("A Simple File Organizer");
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    JButton showButton = new JButton("Show Results");
    showButton.addActionListener(this);
    showButton.setForeground(Color.GREEN);
    showButton.setBackground(Color.BLACK);
    contentPane.add(showButton);
     
    JButton resetButton = new JButton("Reset");
    resetButton.addActionListener(this);
    resetButton.setForeground(Color.GREEN);
    resetButton.setBackground(Color.BLACK);
    contentPane.add(resetButton);
     
    filenameField = new JTextField(NUMBER_OF_CHAR);
    contentPane.add(filenameField);
    filenameField.setText("Enter File Name Here");
     
    labelresult = new JLabel ("");
    labelresult.setSize(500, 400);
    labelresult.setForeground(Color.BLUE);
    getContentPane().add(labelresult);
     
    }
     
    public void actionPerformed(ActionEvent e)
    {
    String actionCommand = e.getActionCommand();
    if(actionCommand.equals("Show Results"))
    showfirstLine();
    else if(actionCommand.equals("Reset"))
    resetFields();
    else 
    labelresult.setText("Unexpected Error");
    }
    private void showfirstLine ()
    {
    Scanner fileInput = null;
    String filename = filenameField.getText();
    File fileObject = new File(filename);
     
    if(!fileObject.exists())
    labelresult.setText("Sorry, this file does not exist. Click reset button and try again.");
    else if (!fileObject.canRead())
    labelresult.setText("That File is not Readable");
    else{
    try
    {
    fileInput = new Scanner (fileObject);
    }
    catch(FileNotFoundException e)
    {
    labelresult.setText("Error opening the file" + " " + filename);
    }
    String firstline = fileInput.nextLine();
    labelresult.setText(firstline);
    fileInput.close();
    }
    }
    private void resetFields()
    {
    filenameField.setText("Enter file name here:");
    labelresult.setText("");
    }
    public static void main (String [ ] args)
    {
    project gui = new project ();
    gui.setVisible(true);
    project frame = new project ();
    }
    }
    Last edited by vannaj; March 10th, 2014 at 01:23 PM.


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

    Default Re: Hi there! I have a project and I need your help dear friends :D

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hi there! I have a project and I need your help dear friends :D

    done

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

    Default Re: Hi there! I have a project and I need your help dear friends :D

    The code has lost its formatting. Logically Nested statements should be indented.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Hi there! I have a project and I need your help dear friends :D

    Search 'jpanel background image'.

  6. #6
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Hi there! I have a project and I need your help dear friends :D

    Not to sound like a jerk but most of the answer can be found through a simple google search. You mentioned it is your first year of college so take it one step at a time. It can be very daunting and discouraging to jump in and try to get everything one at once.

    Now for the issues you are having. I am not to familiar with swing and gui related stuff so I can't chime in. As for the reading of a file lets look at the logic.

    private void showfirstLine ()
    {
      Scanner fileInput = null;
      String filename = filenameField.getText();
      File fileObject = new File(filename);
     
      if(!fileObject.exists())
        labelresult.setText("Sorry, this file does not exist. Click reset button and try again.");
      else if (!fileObject.canRead())
        labelresult.setText("That File is not Readable");
      else{
        try
        {
          fileInput = new Scanner (fileObject); // Sets up you scanner object to get data from your file
        }
        catch(FileNotFoundException e)
        {
          labelresult.setText("Error opening the file" + " " + filename);
        }
        String firstline = fileInput.nextLine(); // This line reads a line and stores it into your variable "firstline"
        labelresult.setText(firstline); // This line sets the label to the value of the variable
        fileInput.close(); // This line closes your reader
      }
    }

    Now what happens if you do:
    firstLine = fileInput.nextLine();
    Place right after you read the first line.

    What do you see? Well if the file has a second line it has now set your firstLine variable to the second line in the file. So every time you call fileInput.nextLine(); it reads the next line.

    What can you set up that will execute the same piece of code withouth having to write it over and over? Take a look at how loops work.

  7. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hi there! I have a project and I need your help dear friends :D

    Thanks for the idea Ubiquitous and i already modified this by using loop here. but it is now showing only the last line... can anyone tell me what exactly can i do to show the entire text file??
    thank you in advance

    try {
                FileReader fileReader = 
                    new FileReader(filename);
     
                BufferedReader bufferedReader = 
                    new BufferedReader(fileReader);
     
                while((line = bufferedReader.readLine()) != null) {
                    labelresult.setText(line);
                }	
     
                bufferedReader.close();			
            }

    import java.io.*;
    import java.util.Scanner;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Scanner;
    import java.io.FileNotFoundException;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.JLabel;
     
    public class panget3 extends JFrame implements ActionListener{
    public static final int WIDTH= 700;
    public static final int HEIGHT = 600;
    public static final int NUMBER_OF_CHAR=30;
     
    JLabel labelresult;
     
    private JTextField filenameField;
     
    public panget3 ()
    {
    Scanner input = new Scanner(System.in);
    setSize(WIDTH, HEIGHT);
    Container contentPane = getContentPane();
     
    contentPane.setBackground(Color.BLACK);
     
    contentPane.setLayout(new FlowLayout());
     
    JFrame frame = new JFrame ();
    setTitle("A Simple File Organizer");
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    JButton showButton = new JButton("Show Results");
    showButton.addActionListener(this);
    showButton.setForeground(Color.GREEN);
    showButton.setBackground(Color.BLACK);
    contentPane.add(showButton);
     
    JButton resetButton = new JButton("Reset");
    resetButton.addActionListener(this);
    resetButton.setForeground(Color.GREEN);
    resetButton.setBackground(Color.BLACK);
    contentPane.add(resetButton);
     
    filenameField = new JTextField(NUMBER_OF_CHAR);
    contentPane.add(filenameField);
    filenameField.setForeground(Color.GREEN);
    filenameField.setBackground(Color.BLACK);
    filenameField.setText("Enter File Name Here");
     
    labelresult = new JLabel ("");
    labelresult.setSize(500, 400);
    labelresult.setForeground(Color.YELLOW);
    getContentPane().add(labelresult);
     
    }
     
    public void actionPerformed(ActionEvent e)
    {
    String actionCommand = e.getActionCommand();
    if(actionCommand.equals("Show Results"))
    showFile();
    else if(actionCommand.equals("Reset"))
    resetFields();
    else 
    labelresult.setText("Unexpected Error");
    }
    private void showFile ()
    {
    		String filename = filenameField.getText();
    		File fileObject = new File(filename);
    		String line = null;
     
    if(!fileObject.exists())
    labelresult.setText("Sorry, this file does not exist. Click reset button and try again.");
    else if (!fileObject.canRead())
    labelresult.setText("That File is not Readable");
    else{
    try {
                FileReader fileReader = 
                    new FileReader(filename);
     
                BufferedReader bufferedReader = 
                    new BufferedReader(fileReader);
     
                while((line = bufferedReader.readLine()) != null) {
                    labelresult.setText(line);
                }	
     
                bufferedReader.close();			
            }
            catch(FileNotFoundException ex) {
                labelresult.setText(
                    "Unable to open file");				
            }
            catch(IOException e) {
                labelresult.setText("Error reading file");			}
    	}
    		}
    private void resetFields()
    {
    filenameField.setText("Enter file name here:");
    labelresult.setText("");
    }
    public static void main (String [ ] args)
    {
    panget3 gui = new panget3 ();
    gui.setVisible(true);
    panget3 frame = new panget3 ();
    }
    }

  8. #8
    Member
    Join Date
    Dec 2013
    Posts
    40
    My Mood
    Amazed
    Thanks
    2
    Thanked 11 Times in 11 Posts

    Default Re: Hi there! I have a project and I need your help dear friends :D

    In order that it shows all the lines in your text field, then for each iteration of your while loop, ensure you get the text currently in the field, append the next line to it by concatenation and finally, set the field with the newly formed text.
    while ((line=bufferedReader.readLine())!=null) {
    labelresult.setText(labelresult.getText()+line);
    }

    You could also use the StringBuilder or StringBuffer to execute the same task.
    Last edited by Mugambbo; March 12th, 2014 at 12:55 AM.
    Who holds the KEY to all knowledge?

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

    vannaj (March 12th, 2014)

  10. #9
    Junior Member
    Join Date
    Mar 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hi there! I have a project and I need your help dear friends :D

    Thank You so much @Mugambbo!!

  11. #10
    Junior Member
    Join Date
    Mar 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hi there! I have a project and I need your help dear friends :D

    Thank You so much @Mugambbo!!

    I have one LAST question sir,

    When I run the code in gui, this is how it looks like

    sample1.jpg

    How can I show it like this

    sample2.jpg

    it reads line by line when i am doing a code and run it in cmd,

    I am almost there, thank you again!

  12. #11
    Junior Member
    Join Date
    Mar 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hi there! I have a project and I need your help dear friends :D

    anyone pls?? do you think it has something to do with the size of JTextArea?? i really cannot figure it. Im almost there.

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Hi there! I have a project and I need your help dear friends :D

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Junior Member
    Join Date
    Mar 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hi there! I have a project and I need your help dear friends :D

    i did your instructions and this is what ive got. funny, the sample text file that im trying to open is a song lyrics. haha. anyway, can you help me now sir??
    how can i post it line by line like what you see below, but when i run it in gui, the text files is only showing in one line. what might be the problem here??

    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation. All rights reserved.

    C:\Users\ADMIN>cd desktop

    C:\Users\ADMIN\Desktop>javac Test0.java

    C:\Users\ADMIN\Desktop>java Test0
    Enter filename
    song.txt
    Againts All Odds - Mariah Carey

    How can I just let you walk away,
    Just let you leave without a trace?
    When I stand here taking every breath with you, ooh ooh
    You're the only one who really knew me at all

    How can you just walk away from me
    When all I can do is watch you leave?
    'Cause we've shared the laughter and the pain
    And even shared the tears
    You're the only one who really knew me at all

    So take a look at me now
    Well there's just an empty space
    And there's nothing left here to remind me
    Just the memory of your face
    Take a look at me now

    Well there's just an empty space
    And you coming back to me is against the odds
    And that's what I've got to face, oh

    I wish I could just make you turn around
    Turn around and see me cry
    There's so much I need to say to you
    So many reasons why
    You're the only one who really knew me at all

    So take a look at me now
    Well there's just an empty space
    And there's nothing left here to remind me
    Just the memory of your face

    Take a look at me now
    Well there's just an empty space
    But to wait for you is all I can do
    And that's what I've got to face

    Take a good look at me now
    'Cause I'll still be standing here
    And you coming back to me is against the odds
    And it's what I've got to face
    Take a look at me now

    C:\Users\ADMIN\Desktop>

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Hi there! I have a project and I need your help dear friends :D

    but when i run it in gui, the text files is only showing in one line.
    Where is that one line shown? What is the difference between how the code is executed when all the lines are shown as in post#13 and when only 1 line is shown?

    Which line is shown when run in gui? The first, the last or which one?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Hi there! I have a project and I need your help dear friends :D

    Are you setting the JTextArea content to each line of text read, or are you append()ing?

    Oh, and if you're using a JTextField, the append() method you probably need is not available, so I suggest you change to a JTextArea.

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Hi there! I have a project and I need your help dear friends :D

    Describe what you see in the GUI
    If all the file's lines are on one line, then you need to add a lineend character ("\n") at the end of each line to make the line go one the next line in the GUI
    If you don't understand my answer, don't ignore it, ask a question.

  18. #17
    Junior Member
    Join Date
    Mar 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hi there! I have a project and I need your help dear friends :D

    uhm, i realy dont know how to explain but, when run in gui he whole text file was shown but it is only one liner. like this

    sample1.jpg

    i want it to show like in the cmd.

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Hi there! I have a project and I need your help dear friends :D

    whole text file was shown but it is only one line
    See post#16
    Add a "\n" to the end of each line
    If you don't understand my answer, don't ignore it, ask a question.

  20. #19
    Junior Member
    Join Date
    Mar 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hi there! I have a project and I need your help dear friends :D

    Hi! ive been busy with my defense about my very simple program.. and you guys really helped me a lot!! again, thank you!

Similar Threads

  1. dear all
    By rita java in forum Java Theory & Questions
    Replies: 1
    Last Post: December 22nd, 2012, 10:38 AM
  2. Dear all.
    By tremandes in forum Member Introductions
    Replies: 1
    Last Post: February 13th, 2012, 05:39 PM
  3. Dear Friends,
    By kaleesp_java in forum Member Introductions
    Replies: 1
    Last Post: April 22nd, 2011, 11:43 AM
  4. hi my dear experts
    By khamis50 in forum Object Oriented Programming
    Replies: 2
    Last Post: March 1st, 2011, 09:57 AM
  5. Dear sir
    By sandeep_mca in forum Member Introductions
    Replies: 1
    Last Post: February 11th, 2011, 08:37 AM