Results 1 to 2 of 2
  1. #1
    Rubidoux is offline Member
    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Default Incrementing and loops

    I'm doing my first year project at uni and have hit a stumbling block. I have to make a basic version of a media player that will allow a user to add tracks that are stored in a MS Access database to a playlist, and give them a rating.

    My first problem is in the rating of the track. I want to be able to enter a numeric value and have the rating displayed in an number of * corresponding to the value. Can you see what im doing wrong in this piece of code?

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Update extends JFrame
                      implements ActionListener {
        JTextField trackNo = new JTextField(2);
    	JTextField rate = new JTextField(2);
        TextArea information = new TextArea(6, 50);
        JButton find = new JButton("Find Track");
        JButton rating = new JButton("Rate");
    
        public Update() {
            setLayout(new BorderLayout());
            setBounds(100, 100, 400, 200);
            setTitle("Update Playlist");
    
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            JPanel top = new JPanel();
            top.add(new JLabel("Enter Track Number:"));
            top.add(trackNo);
            top.add(find); find.addActionListener(this);
    		add("North", top);
    
    		JPanel bottom = new JPanel();
    		bottom.add(new JLabel("Rate Track:"));
    		bottom.add(rate);
            bottom.add(rating); rating.addActionListener(this);
    		add("South", bottom);
    
            JPanel middle = new JPanel();
            middle.add(information);
            add("Center", middle);
    
            setResizable(false);
            setVisible(true);
    	}
    	    public void actionPerformed(ActionEvent e) {
    		
            if (e.getSource() == find) {
    			String key = trackNo.getText();
                String name = LibraryData.getName(key);
                information.setText(name + " - " + LibraryData.getArtist(key));
    			information.append("\nPlay count: " + LibraryData.getPlayCount(key));
                if (name == null) {
                    information.setText("No such track number");}
    				}if (e.getSource() == rating){
    					String key = trackNo.getText();
    					String stars = rate.getText();
    					information.append("\nRating: " + stars);
    				
    			
    		}
    	}
    
        private String stars(int rate) {
            String stars = "";
            for (int i = 0; i < rate; ++i) {
                stars += "*";
            }
            return stars;
        }
    }
    I also have to be able to increment the play count for each track when the play button is used. I have no idea where to start on this. Here is the code

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Playlist extends JFrame
                      implements ActionListener {
        JTextField trackNo = new JTextField(2);
        TextArea information = new TextArea(6, 50);
        JButton add = new JButton("Add track to playlist");
        JButton play = new JButton("Play Playlist");
    	JButton clear = new JButton("Clear Playlist");
        
    	public Playlist() {
            setLayout(new BorderLayout());
            setBounds(100, 100, 400, 200);
            setTitle("Create Playlist");
    
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            JPanel top = new JPanel();
            top.add(new JLabel("Enter Track Number:"));
            top.add(trackNo);
            top.add(add); add.addActionListener(this);
    		add("North", top);
    
    		JPanel bottom = new JPanel();
            bottom.add(play); play.addActionListener(this);
            bottom.add(clear); clear.addActionListener(this);
    		add("South", bottom);
    
            JPanel middle = new JPanel();
            middle.add(information);
            add("Center", middle);
    
            setResizable(false);
            setVisible(true);
    	}
    	    public void actionPerformed(ActionEvent e) {
    		
            if (e.getSource() == add) {
    	    String key = trackNo.getText();
                String name = LibraryData.getName(key);
                information.setText(name + " - " + LibraryData.getArtist(key));
    			information.append("\n" + "Test");
    				if (name == null) {
    					information.setText("No such track number"); }
    						} if (e.getSource() == clear){
    							information.setText("");
    			
    		}
    	}
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    What's wrong with your code? You should know that, I mean did you comes with any error in your code? If so please send the complete error to see. I don't think that anyone wants to test your code and do fixes.

Similar Threads

  1. need some help with loops!
    By Chewart in forum New To Java
    Replies: 2
    Last Post: 12-03-2009, 11:32 PM
  2. when should we use loops
    By shahemaan in forum New To Java
    Replies: 1
    Last Post: 10-31-2009, 01:38 AM
  3. While Loops, need a bit of help.
    By Keno777 in forum New To Java
    Replies: 7
    Last Post: 10-30-2009, 08:24 PM
  4. Incrementing jbutton in for loop
    By RobertF in forum New To Java
    Replies: 3
    Last Post: 03-12-2009, 11:41 AM
  5. Replies: 1
    Last Post: 05-29-2008, 06:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •