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

Thread: JAVA JList Problem in visual images

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

    Question JAVA JList Problem in visual images

    Hi everyone,

    I'm struggling again. . .

    I have a JList problem because I would want to know how to automatically highlight the DVD/ blu Ray stock in their respective colours (green for DVD, yellow for blu Ray) for that movie title.

    I already have the highlighting done in my code.

    For a visual image of the assignment, here it is.
    URL: CSIS1275Assign3w2009 - Homework and Academic Work

    MY PROBLEM IS AT QUESTION 2.

    MOVIEBUY.JAVA

     
    /////////////////////////////////////////   
    ///            NATHAN CHAN            ///   
    ///           ASSIGNMENT 3            ///   
    ///      INSTRUCTOR: GILBERT TSUI     ///   
    ///         COURSE: CSIS 1275         ///   
    ///           SECTION:  01            ///   
    ///     DUE DATE: MARCH 31st 2009     ///   
    /////////////////////////////////////////        
     
    // Please run this program at a 1280 x 1024 resolution.   
     
    import javax.swing.*;    
    import javax.swing.event.*;   
    import java.awt.event.*;    
    import java.awt.*;   
    import java.lang.*;   
    import java.util.*;   
    import javax.swing.border.*;   
     
    public class MovieBuy extends JApplet implements ActionListener   
    {    
            ImageIcon imageClickForMovies = new ImageIcon("clickForMovies1.GIF");   
            JButton buttonClickForMovies = new JButton(imageClickForMovies);   
            MovieList movieList = new MovieList();   
            //String[] data01 = {"Speed Racer", "Star Trek XI", "The Dark Knight", "The X Files 2", "Hulk", "Iron Man", "Made of Honor"};   
            Vector vtMovieTitles = new Vector();   
            JList listbox01 = new JList(vtMovieTitles);   
            //String[] data02 = {"50", "3", "0", "34", "78", "0", "25"};   
            Vector vtDVDStock = new Vector();   
            JList listbox02 = new JList(vtDVDStock);   
            //String[] data03 = {"23", "0", "78", "5", "100", "0", "56"};   
            Vector vtbluRayStock = new Vector();   
            JList listbox03 = new JList(vtbluRayStock);   
            ButtonGroup DVDBluRayGroup = new ButtonGroup();   
            JRadioButton radDVD = new JRadioButton("DVD", true);   
            JRadioButton radBluRay = new JRadioButton("blu Ray", false);   
            String[] copies = {"1 Copy(ies)", "2 Copy(ies)", "3 Copy(ies)", "4 Copy(ies)", "5 Copy(ies)", "6 Copy(ies)", "7 Copy(ies)"};   
            JComboBox cmbCopies = new JComboBox(copies);   
            Label label1999 = new Label("$19.99");   
            Label label3599 = new Label("$39.99");   
            ImageIcon imageBuy = new ImageIcon("buyLogo1.GIF");   
            JButton buttonBuy = new JButton(imageBuy);   
            JList listArea = new JList();   
            JButton btnClearButton = new JButton("Clear");   
            JButton btnClearAll = new JButton("Clear All");   
            Label lblTotal = new Label("Total:");   
            Label lblEnterCreditCard = new Label("Enter Credit Card#:");   
            JPasswordField passEnterCreditCard = new JPasswordField();   
            JButton btnSubmit = new JButton("Submit");   
            Label lblKeyPadPassword = new Label("Use Keypad to enter password for settings");   
            JPasswordField passKeyPad = new JPasswordField(10);   
            JButton buttonA = new JButton("A");   
            JButton buttonB = new JButton("B");   
            JButton buttonC = new JButton("C");   
            JButton buttonD = new JButton("D");   
            JButton buttonE = new JButton("E");   
            JButton buttonF = new JButton("F");   
            JButton buttonG = new JButton("G");   
            JButton buttonH = new JButton("H");   
            JButton buttonI = new JButton("I");   
            JButton buttonJ = new JButton("J");   
            JButton buttonEnter = new JButton("Enter");   
            JButton buttonReset = new JButton("Reset");   
            // Panels   
            JPanel panel01 = new JPanel(new GridLayout(0,5,8,8));   
            JPanel panel01a = new JPanel();   
            JPanel panel01b = new JPanel();   
            JPanel panel01c = new JPanel();   
            JPanel panel01d = new JPanel();   
            JPanel panel01e = new JPanel(new GridLayout(3,0));   
            JPanel panel01eA = new JPanel(new GridLayout(0,2));   
            JPanel panel01eB = new JPanel(new GridLayout(0,2));   
            JPanel panel02 = new JPanel(new FlowLayout());   
            JPanel panel03 = new JPanel(new GridLayout(0,2,8,8));   
            JPanel panel03a = new JPanel(new GridLayout(1,0));   
            JPanel panel03b = new JPanel(new GridLayout(5,0));   
            JPanel panel03b1 = new JPanel(new GridLayout(0,2));   
            JPanel panel04 = new JPanel(new FlowLayout());      
          JPanel panel04a = new JPanel(new GridLayout(4,0));      
            JPanel panel04a1 = new JPanel();   
          JPanel panel04a2 = new JPanel(new GridLayout(2,5,3,3));      
          JPanel panel04a3 = new JPanel(new FlowLayout(FlowLayout.CENTER));            
     
            public void init()   
            {   
                   setLayout(new GridLayout(4,1));   
                    add(panel01);   
                        panel01.add(panel01a);   
                        panel01.add(panel01b);   
                        panel01.add(panel01c);   
                        panel01.add(panel01d);   
                        panel01.add(panel01e);   
                    panel01b.add(listbox01);   
                    panel01c.add(listbox02);   
                    panel01d.add(listbox03);   
                    listbox01.setFixedCellHeight(25);   
                  listbox01.setFixedCellWidth(240);   
                  listbox01.setVisibleRowCount(7);   
                  listbox01.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);   
                    listbox02.setForeground(Color.gray);   
                    listbox02.setSelectionForeground(Color.gray);   
                    listbox02.setSelectionBackground(Color.green);   
                    listbox02.setFixedCellHeight(25);   
                  listbox02.setFixedCellWidth(240);   
                  listbox02.setVisibleRowCount(7);   
                  listbox02.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);   
                    listbox03.setForeground(Color.gray);   
                    listbox03.setSelectionForeground(Color.gray);   
                  listbox03.setSelectionBackground(Color.yellow);   
                    listbox03.setFixedCellHeight(25);   
                  listbox03.setFixedCellWidth(240);   
                  listbox03.setVisibleRowCount(7);   
                  listbox03.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);      
                    panel01b.setBorder(new TitledBorder("Movie Titles"));   
                    panel01b.setBackground(Color.white);   
                    panel01c.setBorder(new TitledBorder("DVD Stock"));   
                    panel01c.setBackground(Color.white);   
                    panel01d.setBorder(new TitledBorder("blu Ray Stock"));   
                    panel01d.setBackground(Color.white);   
                    add(panel02);   
                    add(panel03);   
                    add(panel04);   
                    buttonClickForMovies.addActionListener(this);   
                    buttonClickForMovies.setPreferredSize(new Dimension(255,225));   
                    panel01a.add(buttonClickForMovies);   
                    panel01e.add(panel01eA);   
                    panel01eA.add(radDVD);   
                    panel01eA.add(label1999);   
                    label1999.setFont(new Font("Arial", Font.BOLD, 12));   
                    label1999.setBackground(Color.green);   
                    panel01e.add(panel01eB);   
                    panel01eB.add(radBluRay);   
                    panel01eB.add(label3599);   
                    DVDBluRayGroup.add(radDVD);   
                    DVDBluRayGroup.add(radBluRay);   
                    label3599.setFont(new Font("Arial", Font.BOLD, 12));   
                    panel01e.add(cmbCopies);   
                    label3599.setBackground(Color.yellow);   
                    buttonBuy.setPreferredSize(new Dimension(315,225));   
                    buttonBuy.addActionListener(this);   
                    panel02.add(buttonBuy,BorderLayout.CENTER);   
                    panel03.add(panel03a);   
                    panel03.add(panel03b);   
                    Border blackline = BorderFactory.createLineBorder(Color.black);                
                    panel03a.setBorder(blackline);   
                    panel03a.add(listArea);   
                    panel03b.add(btnClearButton);   
                    panel03b.add(btnClearAll);   
                    panel03b.add(lblTotal);   
                    lblTotal.setBackground(Color.cyan);   
                    panel03b.add(panel03b1);   
                    lblEnterCreditCard.setAlignment(Label.RIGHT);   
                    lblEnterCreditCard.setFont(new Font("Arial", Font.BOLD, 12));   
                    panel03b1.add(lblEnterCreditCard);   
                    panel03b1.add(passEnterCreditCard);   
                    panel03b.add(btnSubmit);   
                   panel04.add(panel04a);      
                panel04a.setBackground(Color.red);      
                panel04a.setBorder(new TitledBorder("For Employee use ONLY"));      
                panel04a.setBackground(Color.red);      
                lblKeyPadPassword.setFont(new Font("Arial", Font.BOLD, 12));      
                panel04a.add(lblKeyPadPassword);      
                    panel04a1.setBackground(Color.red);            
                    panel04a.add(panel04a1);      
                    panel04a1.add(passKeyPad);   
                panel04a.add(panel04a2);      
                panel04a2.add(buttonA);      
                panel04a2.add(buttonB);      
                panel04a2.add(buttonC);      
                panel04a2.add(buttonD);      
                panel04a2.add(buttonE);      
                panel04a2.add(buttonF);      
                panel04a2.add(buttonG);      
                panel04a2.add(buttonH);      
                panel04a2.add(buttonI);      
                panel04a2.add(buttonJ);      
                panel04a.add(panel04a3);      
                panel04a3.setBackground(Color.red);      
                panel04a3.add(buttonEnter);      
                panel04a3.add(buttonReset);     
     
                    }   
     
           public void actionPerformed(ActionEvent e)   
            {   
               if(e.getSource() instanceof JButton)   
                {   
                     if(e.getSource() == buttonClickForMovies)   
                     movieList.createList();   
                      for(int i = 0; i < movieList.mList.length; i++) // add elements to vtMovieTitles   
                            vtMovieTitles.addElement(movieList.mList[i].getMovieTitle());   
                            listbox01.setListData(vtMovieTitles);   
                      for(int i = 0; i < movieList.mList.length; i++)  // add elements to vtDVDStock      
                            vtDVDStock.addElement(movieList.mList[i].getMovieStock('D'));   
                            listbox02.setListData(vtDVDStock);   
                      for(int i = 0; i < movieList.mList.length; i++)  // add elements to vtblueRayStock   
                          vtbluRayStock.addElement(movieList.mList[i].getMovieStock('B'));   
                            listbox03.setListData(vtbluRayStock);   
                 }   
     
            public void valueChanged(ListSelectionEvent e)   
            {   
     
            // What should I write here?  Should I use an index like "i"?   
     
          }    
     
       }   
     }

    MOVIEINFO.JAVA

     
    //For Assign3 Winter 2009    
     
    class MovieInfo   
    {   
     
      private String strMovieTitle;   
      private int intMovieDVD;   
      private int intMovieBlueRay;   
     
      private static double doubMovieDVDPrice = 19.99;   
      private static double doubMovieBlueRayPrice = 35.99;   
     
      MovieInfo(String title, int mDVD, int mBRay)   
      {   
     
         strMovieTitle = title;   
          intMovieDVD = mDVD;   
          intMovieBlueRay = mBRay;   
     
     
      }   
     
      public String getMovieTitle()   
      {   
        return strMovieTitle;   
      }   
     
     
      public int getMovieStock(char which)   
      {   
         if(which == 'D')   
             {   
               return intMovieDVD;   
               }   
          else  
             {   
               return intMovieBlueRay;   
              }   
        }      
        // you finish this    
         // You can use "which" to determine which   
         // type of movie to retrieve the stock for          
     
     
     
      public double getMoviePrice(char which)   
      {   
         if(which == 'D')   
              {   
                return doubMovieDVDPrice;   
                }   
          else  
              {   
                return doubMovieBlueRayPrice;   
                }   
        }     
         // you finish this    
         // You can use "which" to determine which   
         // type of movie to retrieve the stock for          
     
     
      public void setMovieStock(char which, int amt)   
      {   
         if(which == 'D')   
             {   
              intMovieDVD  = amt;   
              }   
          else    
             {   
              intMovieBlueRay = amt;   
              }   
     }     
     
         // you finish this   
         // You can use "which" to determine which   
         // type of movie to set the stock for      
     
       public static void setMoviePrice(char which, double prce)   
        {   
          if(which == 'D')   
              {   
               doubMovieDVDPrice = prce;   
                }   
            else    
              {   
               doubMovieBlueRayPrice = prce;   
              }   
     
        // you finish this    
         // You can use "which" to determine which   
         // type of movie to set the price for    
      }   
     
     
     
    }   
     
    ///////////////////////////////   
     
    class MovieList   
    {   
     
      MovieInfo[] mList;   
     
      public void createList()   
      {   
     
          mList = new MovieInfo[7];   
            mList[0] = new MovieInfo("Speed Racer",50,23);   
            mList[1] = new MovieInfo("Star Trek XI",3,0);   
            mList[2] = new MovieInfo("The Dark Knight",0,78);   
            mList[3] = new MovieInfo("The X Files 2",34,5);   
            mList[4] = new MovieInfo("Hulk",78,100);   
            mList[5] = new MovieInfo("Iron Man", 0,0);   
            mList[6] = new MovieInfo("Made of Honor",25,56);   
     
            MovieInfo.setMoviePrice('D', 19.99);   
            MovieInfo.setMoviePrice('B', 35.99);   
     
        ////////////////////////////////////////////////   
        // IMPORTANT: PLEASE use "setMoviePrice()" method to    
        // set the PRICES FOR DVD OR Blu Ray here   
        //            19.99 FOR DVD    
        //            35.99 for Blu Ray   
        ////////////////////////////////////////////////   
     
     
     
      }   
     
    }

    Thanks for reading, and I hope you can help me on this problem.

    By the way, this is what I have attempted so far but it didn't work.

     
     
    	   public void actionPerformed(ActionEvent e)
    		{
    		   if(e.getSource() instanceof JButton)
    			{
    				 if(e.getSource() == buttonClickForMovies)
    				 movieList.createList();
    				  for(int i = 0; i < movieList.mList.length; i++) // add elements to vtMovieTitles
    				     	vtMovieTitles.addElement(movieList.mList[i].getMovieTitle());
    						listbox01.setListData(vtMovieTitles);
    				  for(int i = 0; i < movieList.mList.length; i++)  // add elements to vtDVDStock   
    						vtDVDStock.addElement(movieList.mList[i].getMovieStock('D'));
    						listbox02.setListData(vtDVDStock);
    				  for(int i = 0; i < movieList.mList.length; i++)  // add elements to vtblueRayStock
    				      vtbluRayStock.addElement(movieList.mList[i].getMovieStock('B'));
    						listbox03.setListData(vtbluRayStock);
    			 }
     
    		}

    - Nathe


  2. #2

    Default Re: JAVA JList Problem

    Sorry antitru5t but I don't understand what you want to do. Can u explain me please?

  3. #3

    Default Re: JAVA JList Problem

    Try this:

    Your class has to implement ListSelectionListener.

    Then you add the listener to the JList:

    listbox01.addListSelectionListener(this);

    And finally you write the method:

    public void valueChanged(ListSelectionEvent e) {
    		int i=e.getFirstIndex();
    		int j=e.getLastIndex();
    		listbox02.setSelectedIndex(i+1);
    		listbox03.setSelectedIndex(i+1);
    	}

  4. #4

    Default Re: JAVA JList Problem

    antitru5t did I solve your problem?

  5. #5
    Junior Member
    Join Date
    Mar 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Wink Re: JAVA JList Problem

    Thanks leandro. I've already figured out the problem and fixed it myself! Thanks.

    - Nathe

Similar Threads

  1. Replies: 10
    Last Post: June 22nd, 2009, 07:45 AM
  2. [SOLVED] Error "TitleChanger is abstract; cannot be instantiated"
    By Uzual in forum AWT / Java Swing
    Replies: 2
    Last Post: May 26th, 2009, 11:23 AM
  3. Java GUI problem in paintComponent?
    By Richard_ in forum AWT / Java Swing
    Replies: 2
    Last Post: May 1st, 2009, 08:19 AM
  4. Replies: 1
    Last Post: March 31st, 2009, 02:49 PM
  5. Dropping to graphic element dragged from JList
    By tua1 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 29th, 2008, 08:22 AM