Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Adding a selected Index from a JList box using the JButton. I'm so confused. :S
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
antitru5t




PostPosted: Wed Mar 25, 2009 1:30 am   Post subject: Adding a selected Index from a JList box using the JButton. I'm so confused. :S

Hi everyone,

I'm brutally stuck on JAVA.
My teacher has mentioned about retrieving from vectors. But, I seriously do not get it at all. His assignment is painstakingly hard.

I need to get my Movie Titles, number of copies. I want to implement (Combo Box), type of movie, DVD or Blu ray and Pricing when I press the JButton "Buy".

The main idea is that I want to get things to show up.
I have been using the getSelectedIndex() feature and I've been getting values like -1. Eek?!? :S

How am I supposed to use vectors to get getSelectedIndex()?

Here is my coding so far, if you want to look at it and try to fix it.

For a visual image of the assignment, here it is.

URL: [url] http://www.scribd.com/doc/13082448/CSIS1275Assign3w2009 [/url]


MOVIEBUY.JAVA

code:

 
/////////////////////////////////////////
///            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, ListSelectionListener
{
                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);
                DefaultListModel dataJListInput = new DefaultListModel(); //
                JList listArea = new JList(dataJListInput);
                JScrollPane listAreaScrollPane = new JScrollPane(listArea);
                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()
                {
                           movieList.createList(); // initialize movieList   
                                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);
                                listbox01.addListSelectionListener(this);
                                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);
                                listbox02.addListSelectionListener(this);
                                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);   
                                listbox03.addListSelectionListener(this);
                                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(listAreaScrollPane);
                                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)
                                  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);
                         }
                 
                  if(e.getSource() instanceof JButton)
                  {
                     if(e.getSource() == buttonBuy)
                          {
                             {
                              // *********************** So, what should I write here.. can you give me some hints? :(
                                  }
                                 
                          }   
                 
                  }
                         
            }   
                         
          public void valueChanged(ListSelectionEvent e)   
        {
                  for(int i = 0; i < movieList.mList.length; i++)
                     if(listbox01.getSelectedIndex() == i)   
                          {
                          listbox02.setSelectedIndex(i);
                          listbox03.setSelectedIndex(i);
                          }
                  }
 }





MOVIEINFO.JAVA

code:


//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   
    ////////////////////////////////////////////////   
       
       
         
  }   
 
}




If you can take a look at the code and try to help me ASAP, that would be great. Thanks! Very Happy

- Nathe
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Wed Mar 25, 2009 7:31 am   Post subject: RE:Adding a selected Index from a JList box using the JButton. I\'m so confused. :S

You could try using the JList.getSelectedIndicies() method, which will return ALL the selections made: http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#getSelectedIndices() .
antitru5t




PostPosted: Wed Mar 25, 2009 3:01 pm   Post subject: Re: Adding a selected Index from a JList box using the JButton. I'm so confused. :S

Hello DemonWasp. I am still stuck at the moment. I need the getSelectedValue and not the getSelectedIndex or getSelectedIndices.. .. hmm

Here is what I have attempted so far and this is what I have. I think that the answer is close, but I'm not there yet because I can't get it to display on the JList listarea box.

code:



                  if(e.getSource() instanceof JButton)
                  {
                     if(e.getSource() == buttonBuy)
                          {
                             {
                                   vtMovieTitleSelected.addElement(listbox01.getSelectedValue());
                                        listArea.setListData(vtMovieTitleSelected);
                                       
                                  }
                                  
                                  
                          }   




Please help me ASAP.

- Nathe
antitru5t




PostPosted: Wed Mar 25, 2009 3:08 pm   Post subject: Re: Adding a selected Index from a JList box using the JButton. I'm so confused. :S

I used getSelectedIndices and got a value of [I@19ee1ac, which means that I have something at least. . . .. How do I convert this to a String?
DemonWasp




PostPosted: Wed Mar 25, 2009 5:32 pm   Post subject: RE:Adding a selected Index from a JList box using the JButton. I\'m so confused. :S

getSelectedIndices() returns an array of integers - int[] . You should be able to determine which movies are selected from there.
antitru5t




PostPosted: Wed Mar 25, 2009 9:21 pm   Post subject: This is what I have attempted. Can you help me fix it?

I stil have zero results.

So far, this is my coding. If you could help me, that would be great.

code:


                  if(e.getSource() instanceof JButton)
                  {
                     if(e.getSource() == buttonBuy)
                          {
                             {
                                        dataJListInput.addElement(listbox01.getSelectedValues());
                                       
                                  }
                                  
                                  
                          }   
                  
                  }
                        
            }   

antitru5t




PostPosted: Wed Mar 25, 2009 9:24 pm   Post subject: Re: Adding a selected Index from a JList box using the JButton. I'm so confused. :S

In other words, my coding does not work.
DemonWasp




PostPosted: Thu Mar 26, 2009 1:28 am   Post subject: RE:Adding a selected Index from a JList box using the JButton. I\'m so confused. :S

Trying to add an int[] to an array isn't going to work. Replace that line with something like this:

code:

int[] selected = listbox01.getSelectedValues();
for ( int i = 0 ; i < selected.length; ++i ) {
    dataJListInput.addElement( selected[i] );
}
Sponsor
Sponsor
Sponsor
sponsor
antitru5t




PostPosted: Thu Mar 26, 2009 1:34 am   Post subject: Re: Adding a selected Index from a JList box using the JButton. I'm so confused. :S

I get an error of :

MovieBuy.java:211: incompatible types
found : java.lang.Object[]
required: int[]
int[] selected = listbox01.getSelectedValues();

How should I fix this?
DemonWasp




PostPosted: Thu Mar 26, 2009 7:55 am   Post subject: RE:Adding a selected Index from a JList box using the JButton. I\'m so confused. :S

Ugh. Okay, actually looked at your code; turns out you've set your list box so it can't even select more than one thing at a time. You should be able to use getSelectedValue() to get an object representing what you put into the JList. I'm not quite sure what you put in to that list, but if it was a String, then you can change it to a string by casting it to a String:

code:
String myString = (String)listbox01.getSelectedValue();


Side note: Don't use Vector unless your program is multithreaded (it isn't). Use List instead:

code:
List myList = new ArrayList();
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: