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

Thread: I have got an assignment and I really need help or any tips..thanks

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    8
    My Mood
    Cool
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default I have got an assignment and I really need help or any tips..thanks

    Hi I have got to codes one it is basically the interface and the other is the code.
    Basically my program needs to this:
    import java.io.File;
     
    public class Main {
      public static void main(String[] argv) throws Exception {
        File dir = new File("C:/Program Files/java");
     
        String[] children = dir.list();
        if (children == null) {
          System.out.println("does not exist or is not a directory");
        } else {
          for (int i = 0; i < children.length; i++) {
            String filename = children[i];
            System.out.println(filename);
          }
        }
      }
    }

    If you compile and run you will see that will show files saved under the entered folder.

    I can creat the interface apart but I don't know how to combine both code and make it run.

    The path of the folder (C:/Program Files/java) needs to be entered in a text box and need two buttons one is OK and the other Cancel.
    The files need to be listed or displayed in another windows which also has a button Ok which return to the previous window.

    Any help is wellcome.
    Thanks.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: I have got an assignment and I really need help or any tips..thanks

    So you need to write a GUI. You would not have been set this assignment if you have not covered the material in class. You need to revise your course material, read your textbook and read the online tutorials. Once you have done that you can start to write some code and when you have some code you can post it here and ask specific questions. Nobody is going to do it for you.
    Improving the world one idiot at a time!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I have got an assignment and I really need help or any tips..thanks

    You can also look at the Java Tutorials for ideas and code samples:
    The Really Big Index
    Go to this site and do a Find for GUI

  4. #4
    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: I have got an assignment and I really need help or any tips..thanks

    A text box.

    Hmmmmm, are you allowed to use JOptionPane? If so, it can make a text box, and an ok and cancel button for you.

    You'll have to use the right JOptionPane method and tell it what to do if cancel or ok is hit.

    If you aren't allowed to do that,

    Try this:

    JTextField jtf = new JTextField(Enter a character limit, a.k.a. size, here);

    JButton ok = new JButton("Ok");
    JButton cancel = new JButton("Cancel");

    and to get it to tell what the buttons to do

    simply

    ok.addActionListener(have an Action Listener as a parameter here).

    Either use an anonymous class, i.e. a new ActionListener with the method actionPerformed enclosed or have another class, an inner class that's part of your class but probably private, extend ActionListener and use that.

    As for the files being listed, I have an idea to show them.

    You could possibly make a JLabel and make a for loop to add each index of the array to it and use html encoding

    <html><br> "A line" </br> <br> "Another line" </br> <br> "A third line" </br> etc </html> to add each index, one per line

    Or you could simply pass the array you got as a parameter to a new JList and it'll show them in a list, though if there are lots of files and stuff, you should use a JScrollPane to make it so all of it can be seen and scrolled to.

    JList fileList = new JList(children);

    Also, make sure you have some kind of Container, look up Container in the Java API, to put all your stuff into.

    Some like JFrame and JPanel, among others, require you to call setVisible(true) to make them visible

    and JFrame normally will hide but not stop running on close so if you use that

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    to make sure it exits when you hit the X button, though if you use two JFrames, make sure only the main one that you want open has that close operation, otherwise your entire program will exit if either of those windows are closed.

    Look at JOptionPane (Java Platform SE 7 )

    Look at the subclasses of Component (Java Platform SE 6)

  5. The Following User Says Thank You to javapenguin For This Useful Post:

    celz (August 9th, 2011)

  6. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    8
    My Mood
    Cool
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: I have got an assignment and I really need help or any tips..thanks

    I got nice ideas from your post guys, for helping me out.
    If any of you got any other ideas I would appreciate.

    I managed to creat the interface code, now I need some light to combine both codes:

    import java.awt.event.*;
    import javax.swing.*;

    public class Events extends JFrame
    implements ActionListener {

    JButton btnClick = new JButton("OK");
    JButton btnClick2 = new JButton("CANCEL");
    JLabel label = new JLabel("Path");
    JTextField text = new JTextField(15);
    JList fileList = new JList(children);

    public Events() {
    super("Input");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    btnClick.addActionListener(this);

    JPanel pane = new JPanel();

    pane.add(label);
    pane.add(text);
    pane.add(btnClick);
    pane.add(btnClick2);

    add(pane);
    setSize(400,200);
    pack();
    setVisible(true);
    }

    public void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();

    if (source.equals(btnClick)) {
    JOptionPane.showMessageDialog(null, "The files saved in a folder should be displayed here(Think so)");
    }
    }

    public static void main(String args[]) {
    new Events();
    }

    }
    Last edited by celz; August 9th, 2011 at 05:27 AM.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I have got an assignment and I really need help or any tips..thanks

    I need some light to combine both codes:
    Can you explain what your problem is?
    Where are the two pieces of of code you want to combine?
    You posted some GUI code. Describe what you want added to it.

  8. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    8
    My Mood
    Cool
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: I have got an assignment and I really need help or any tips..thanks

    import java.awt.event.*;
    import javax.swing.*;

    public class Events extends JFrame
    implements ActionListener {

    JButton btnClick = new JButton("OK");
    JButton btnClick2 = new JButton("CANCEL");
    JLabel label = new JLabel("Path");
    JTextField text = new JTextField(15);
    JList fileList = new JList(children);

    public Events() {
    super("Input");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    btnClick.addActionListener(this);

    JPanel pane = new JPanel();

    pane.add(label);
    pane.add(text);
    pane.add(btnClick);
    pane.add(btnClick2);

    add(pane);
    setSize(400,200);
    pack();
    setVisible(true);
    }

    public void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();

    if (source.equals(btnClick)) {
    JOptionPane.showMessageDialog(null, "The files saved in a folder should be displayed here(Think so)");
    }
    }

    public static void main(String args[]) {
    new Events();
    }

    }

    This code Above is the interface.

    The next cod is basically what the programs need to do:
    import java.io.File;

    public class Main {
    public static void main(String[] argv) throws Exception {
    File dir = new File("C:/Program Files/java");

    String[] children = dir.list();
    if (children == null) {
    System.out.println("does not exist or is not a directory");
    } else {
    for (int i = 0; i < children.length; i++) {
    String filename = children[i];
    System.out.println(filename);
    }
    }
    }
    }

    If you run you'll se that there is a text box wich needs to be filled with a path like:C:/Program Files/java
    and what needs to be displayed, will be displayed after you click OK then generate another windows or form with a list of files under the folder chosen previsouly.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I have got an assignment and I really need help or any tips..thanks

    text box wich needs to be filled with a path like:C:/Program Files/java
    The text field class has a method that will set the text of the text field to a String that you provide.

    what needs to be displayed, will be displayed after you click OK
    What needs to be displayed?
    Where should it be displayed?

    generate another windows or form with a list of files under the folder chosen
    You can create another window using the same technique you used to create the first one:
    new JFrame() and then add components to it and then set it visible.
    You can get a list of files in a folder by using the File class.


    Please wrap you code in code tags to preserve its formatting. [code]
    See: http://www.javaprogrammingforums.com...do=bbcode#code

  10. #9
    Junior Member
    Join Date
    Aug 2011
    Posts
    8
    My Mood
    Cool
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: I have got an assignment and I really need help or any tips..thanks

    It needs to be displayed a list of files save any folder as showed like for example:
    I enter the following paht C:/Program Files/java
    It needs to be display any file which is inside of the folder java.
    It should be displayed on the second windows, the one that you recommed to use the same technique which a used adding a new frame and its componets.

    I will be able to create the second windows, yet as you suggested I do not have any idea of how write the file class and make all program work properly, so I don't know if you willing to creat for me, or just show me a simple way and I modify somethings doing in my way or you could suggest any tutorial, website or book where I can find something like.

    The thing is that I used to program in C# and now the new program state that I need to learn Java, so I don't have bases I can understand codes but I am not yet capable to write the all code by own, that is why I need some help.

    Thanks.

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I have got an assignment and I really need help or any tips..thanks

    You should look through the Java Tutorial to see how to do the various things you need.
    The Java™ Tutorials
    The Really Big Index

    Pickout and do one small step at a time.
    For example use the File class to get a list of files in a folder.

    How do you want to display the list of files?
    As a list of lines of text
    In a selectable list where a file could be chosen?

  12. #11
    Junior Member
    Join Date
    Aug 2011
    Posts
    8
    My Mood
    Cool
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: I have got an assignment and I really need help or any tips..thanks

    It needs to be displayed in a list of lines.

    Thanks for the tutorials.

    If a creat the second window what's the code that I will be using to once I click on the button OK display the second window.

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I have got an assignment and I really need help or any tips..thanks

    what's the code that I will be using to once I click on the button OK display the second window.
    You can create another window using the same technique you used to create the first one:
    new JFrame() and then add components to it and then set it visible.

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

    celz (August 9th, 2011)

  15. #13
    Junior Member
    Join Date
    Aug 2011
    Posts
    8
    My Mood
    Cool
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: I have got an assignment and I really need help or any tips..thanks

    Okay, although I did not fully understand I will try to analyse what you have said and my code. Thanks

  16. #14
    Junior Member
    Join Date
    Aug 2011
    Posts
    8
    My Mood
    Cool
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: I have got an assignment and I really need help or any tips..thanks

    Okay, although I did not fully understand I will try to analyse what you have said and my code. Thanks

  17. #15
    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: I have got an assignment and I really need help or any tips..thanks

    Can't recall what the JTextField object was, but you can get a String from it by

    (I have to make up an object to use for demonstration)

    JTextField field = new JTextField(40);

    String path = field.getText();
    File dir;
    try
    {
    dir = new File(path);
    }

    catch(NullPointerException npeRef)
    {
    // what to do if the file pathname is null
    }

    String[] children = dir.list();

    if (children == null) {
    System.out.println("does not exist or is not a directory");
    } else {
    for (int i = 0; i < children.length; i++) {
    String filename = children[i];
    System.out.println(filename);
    }
    }

    if (children != null)
    {
    JList list = new JList(children);

    JScrollPane jsp = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    pane.add(jsp);
    }

    Also, I can't see why your main method or whatever needs to be throwing any Exceptions itself, i.e. with a throws clause like you're doing.

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

    celz (August 10th, 2011)

Similar Threads

  1. Some java tips
    By meghadwivedi in forum Member Introductions
    Replies: 1
    Last Post: May 16th, 2011, 09:44 AM
  2. Assignment please Help :(
    By pagalas07 in forum Collections and Generics
    Replies: 3
    Last Post: March 30th, 2011, 08:52 AM
  3. Need some tips...
    By nine05 in forum JDBC & Databases
    Replies: 1
    Last Post: March 20th, 2011, 11:37 AM
  4. Looking for tips
    By TimoElPrimo in forum Member Introductions
    Replies: 3
    Last Post: February 15th, 2011, 08:51 PM
  5. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM