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

Thread: Really need some help with Objects inside an ArrayList

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Really need some help with Objects inside an ArrayList

    Okay, so we have this task where we need to program a CD library. It has 4 textfields where you'll put in artist, title, length and number of tracks. Were using JFrame for this and I've managed to make the layout pretty well. The program also has 5 buttons:

    Add: Is supposed to add these 4 variables into an object (CD) and save them to an arraylist
    Print: Is supposed to print out all of the objects that you've added and show them in the text field below.
    Sort: Is supposed to sort the CDs after Artist name
    Abort: This one I've managed to solve on my own
    Exit: This one I've managed to solve on my own

    My teacher refuses to help me since this is the last project, and my friends can't solve it either. I know this sounds like I'm a lazy idiot that's looking for an easy way out, but I could really need some help here. The code is just full of diffrent trials of thought right now, I'm not the best programmer...

    If you could solve this for me or just give me a tip on how to think I would be very grateful. Tell me if there is something you don't understand. And sorry for the swedish comments by the side...



    package katalogen;
     
    public class Katalogen {
     
        public static void main(String[] args) {
     
            Window W = new Window();
     
        }
    }

    package katalogen;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.lang.String.*;
    import java.util.ArrayList;
     
    public class Window extends JFrame implements ActionListener { //Gör så att klassen kan använda JFrame och ActionListener
     
        private JPanel p1 = new JPanel(); //Skapar ny panel
        private JPanel p2 = new JPanel(); //Skapar ny panel
        private JPanel p3 = new JPanel(); //Skapar ny panel
        private JButton b1 = new JButton("Add"); //Skapar knappen "Lägg till"
        private JButton b2 = new JButton("Print"); //Skapar knappen "Skriv ut"
        private JButton b3 = new JButton("Sort"); //Skapar knappen "Sortera"
        private JButton b4 = new JButton("Abort"); //Skapar knappen "Avbryt"
        private JButton b5 = new JButton("Exit"); //Skapar knappen "Avsluta"
        private JTextField r1 = new JTextField(10); //Skapar ett textfält
        private JTextField r2 = new JTextField(10); //Skapar ett textfält
        private JTextField r3 = new JTextField(10); //Skapar ett textfält
        private JTextField r4 = new JTextField(10); //Skapar ett textfält
        private JLabel t1 = new JLabel("Artist:"); //Skapar texten "Atrist"
        private JLabel t2 = new JLabel("Tracks:"); //Skapar texten "Antal spår"
        private JLabel t3 = new JLabel("Title:"); //Skapar texten "Titel"
        private JLabel t4 = new JLabel("Lenght:"); //Skapar texten "Längd"
        private JTextArea a1 = new JTextArea(30, 60); //Skapar textarean (utskriftområdet)
        private Object list;
        private String nyTrack;
     
        public Window() {
     
            setLayout(new BorderLayout()); //Sätter layouttypen för hela klassen
     
            p1.setLayout(new FlowLayout()); //Sätter layouttypen för delpanelen
     
            p1.add(t1); //Lägger till t1 i första panelen
            p1.add(r1); //Lägger till r1 i första panelen
            p1.add(t2); //Lägger till t2 i första panelen
            p1.add(r2); //Lägger till r2 i första panelen
            p1.add(t3); //Lägger till t3 i första panelen
            p1.add(r3); //Lägger till r3 i första panelen
            p1.add(t4); //Lägger till t4 i första panelen
            p1.add(r4); //Lägger till r4 i första panelen
            add(p1, BorderLayout.NORTH); //Sätter panelens position till uppe
     
            p2.setLayout(new FlowLayout()); //Sätter layouttypen för delpanelen
     
            p2.add(b1); //Lägger till b1 i andra panelen
            p2.add(b2); //Lägger till b2 i andra panelen
            p2.add(b3); //Lägger till b3 i andra panelen
            p2.add(b4); //Lägger till b4 i andra panelen
            p2.add(b5); //Lägger till b5 i andra panelen
            add(p2, BorderLayout.CENTER); //Sätter panelens position till i mitten
     
            p3.setLayout(new FlowLayout()); //Sätter layouttypen för delpanelen
     
            p3.add(a1); //Lägger till a1 i tredje panelen
            add(p3, BorderLayout.SOUTH); //Sätter panelens position till nere
     
            b1.addActionListener(this); //Lägger till en lyssnare för knappen b1
            b1.setActionCommand("Add"); //Lägger till ett kommando för knappen b1
            b2.addActionListener(this); //Lägger till en lyssnare för knappen b2
            b2.setActionCommand("Print"); //Lägger till ett kommando för knappen b2
            b3.addActionListener(this); //Lägger till en lyssnare för knappen b3
            b3.setActionCommand("Sort"); //Lägger till ett kommando för knappen b3
            b4.addActionListener(this); //Lägger till en lyssnare för knappen b4
            b4.setActionCommand("Stop"); //Lägger till ett kommando för knappen b4
            b5.addActionListener(this); //Lägger till en lyssnare för knappen b5
            b5.setActionCommand("Exit"); //Lägger till ett kommando för knappen b5
     
            pack();
            setVisible(true); //Gör att alla peneler blir synliga
            setDefaultCloseOperation(EXIT_ON_CLOSE); //Gör att programmet avslutas när man trycker på kryss
        }
     
        @Override
        public void actionPerformed(ActionEvent action) {
            //throw new UnsupportedOperationException("Not supported yet.");
            String event = action.getActionCommand();
     
            ArrayList<CD> collection = new ArrayList<CD>();
     
            int lol = 0;
     
            if (event.equals("Add")) {
     
                collection.add(new CD(r1, r2, r3, r4));
     
                a1.setText(nyTrack);
     
     
            } else if (event.equals("Print")) {
                String PrintOut = list.toString();
                a1.setText(PrintOut());
     
     
            } else if (event.equals("Sort")) {
                list.sort(collection);
     
            } else if (event.equals("Stop")) {
                r1.setText(null);
                r2.setText(null);
                r3.setText(null);
                r4.setText(null);
     
            } else if (event.equals("Exit")) {
                System.exit(0);
     
            }
        }
    }

    package katalogen;
     
    import javax.swing.JTextField;
     
    public class CD {
     
        String nyArtist;
        String nyTitle;
        Double nyLength;
        int nyTrack;
     
        CD(String Artist, String Title, int Track, double Length) {
            //throw new UnsupportedOperationException("Not yet implemented");
     
            this.nyArtist = Artist;
            this.nyLength = Length;
            this.nyTitle = Title;
            this.nyTrack = Track;
     
        }
     
        CD(JTextField r1, JTextField r2, JTextField r3, JTextField r4) {
            throw new UnsupportedOperationException("Not yet implemented");
        }
     
        public String nyArtist() {
     
            return nyArtist;
     
        }
     
        public double nyLength() {
     
            return nyLength;
     
        }
     
        public String nyTitle() {
     
            return nyTitle;
     
        }
     
        public int nyTrack() {
     
            return nyTrack;
     
        }
        //public int (CD) 
    }

    package katalogen;
     
    import java.util.*;
     
    public class List {
     
        CD ny;
     
        private ArrayList<CD> collection = new ArrayList<CD>();
     
        public void newCD(String Artist, String Title, int Track, double Length) {
            collection.add(new CD(Artist, Title, Track, Length));
     
            int lol = 0;
     
     
     
            Object[] elements = collection.toArray();
     
     
     
            System.out.println(elements);
        }
     
        //public void sort() {
            //Collections.sort(collection);
        }


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Really need some help with Objects inside an ArrayList

    Quote Originally Posted by ISuckSoBad View Post
    Tell me if there is something you don't understand.
    I don't understand what your question is - probably because you haven't yet asked one...


    And sorry for the swedish comments by the side...
    Those comments are totally redundant and make the code much harder to read, but rather than apologise for them, why didn't you just delete them? If you won't make any effort to make your code readable, why should we make any effort to read it?

Similar Threads

  1. [SOLVED] How to get the return of a method inside an object in an ArrayList?
    By Hallowed in forum Java Theory & Questions
    Replies: 7
    Last Post: May 1st, 2011, 11:44 PM
  2. [SOLVED] Accessing methods of an object inside in ArrayList
    By jmack in forum Java Theory & Questions
    Replies: 3
    Last Post: January 22nd, 2011, 12:28 PM
  3. Minimum value of objects in ArrayList
    By Sputnik in forum Loops & Control Statements
    Replies: 2
    Last Post: October 23rd, 2010, 02:20 PM
  4. [SOLVED] Passing arrayList inside class
    By KrisTheSavage in forum Collections and Generics
    Replies: 1
    Last Post: March 27th, 2010, 01:45 PM
  5. Arraylist Objects to Database
    By frankycool in forum JDBC & Databases
    Replies: 3
    Last Post: November 15th, 2009, 08:01 PM