neewb's Profile User Rating: -----

Reputation: 3 Apprentice
Group:
Active Members
Active Posts:
81 (0.12 per day)
Joined:
16-October 12
Profile Views:
490
Last Active:
User is offline Jun 07 2014 10:36 PM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   neewb has not set their status

Posts I've Made

  1. In Topic: Using Multiple Selection Choices

    Posted 3 Jun 2014

    View PostCasiOo, on 31 May 2014 - 06:29 AM, said:



    I have an update...I did some research on the Pattern class and came up with another method of extracting a String and then trying to parse it:

    import java.awt.*;
    import java.awt.event.*;
    import java.util.regex.*;
    import javax.swing.*;
    
    // The MiscProducts class allows the user (optional) to select one or more misc items
    public class MiscProductsPanel extends JPanel 
    {
    	public final double TAPE = 10.00;                                  // Set the grip tape to 10
    	public final double BEARINGS = 30.00;                              // Set the bearings to 30
    	public final double PADS = 2.00;                                   // Set the riser pads to 2
    	public final double NUTS_BOLTS = 3.00;                             // Set the nuts and bolts kit to 3
    	
    	private JList miscList;                                             // The misc products
    	private JList selectedMiscList;                                     // Selected misc products
    	private JLabel label;                                               // Displays a message
    	private JButton button;                                             // A button for the misc selection
    	
    	// The following array holds the values that will be displayed in the misc list
    	private String[] misc = { "Grip Tape: $10", "Bearings: $30", "Riser Pads: $2", "Nuts & bolts kit: $3" };
    	
    	// Constructor, takes no arguments
    	public MiscProductsPanel()
    	{
    		setLayout(new GridLayout(1, 0));
    		
    		// Build the panels
    		buildMiscPanel();
    		buildSelectedMiscPanel();
    		buildMiscSelectionsPanel();
    		
    		// Add a border around the panel
    		setBorder(BorderFactory.createTitledBorder("Misc Products"));
    	}
    	
    	// The buildMiscPanel method adds a list with the types of misc products to a panel
    	private void buildMiscPanel()
    	{
    		// Create the list
    		miscList = new JList(misc);
    		
    		// Set the selection mode to multiple interval selection
    		miscList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    		
    		// Add the list to the panel
    		add(miscList);
    	}
    	
    	// The buildSelectedMiscPanel method adds a list to a panel. This will hold the selected misc items
    	private void buildSelectedMiscPanel()
    	{
    		// Create the list
    		selectedMiscList = new JList();
    		
    		// Add the list to the panel
    		add(selectedMiscList);	
    	}
    	
    	// The buildMiscSelectionsPanel adds a selections button to the panel
    	private void buildMiscSelectionsPanel()
    	{
    		// Create the button
    		button = new JButton("Get Misc Selections");
    		
    		// Add an action listener to the button
    		button.addActionListener(new ButtonListener());
    		
    		// Add the button to the panel
    		add(button);
    	}
    	
    	// Private inner class that handles the event when the user selects an item from the list
    	private class ButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			
    			selectedMiscList.setListData( miscList.getSelectedValuesList().toArray());
    		}
    		
    	}
    	
    	// This method will return the cost of the misc items
    	public double getMiscCost()
    	{
    		double miscCost = 0.0;
    		
    		int items = selectedMiscList.getModel().getSize();
    		int i;
    		for (i=0; i<items; i++)
    		{
    			selectedMiscList.setSelectedIndex(i);
    			Pattern p = Pattern.compile("[$]*");
    			String[] dollar = (p.split((CharSequence) selectedMiscList.getSelectedValue()));
    			for (String s : dollar)
    			{
    				miscCost = miscCost + dollar[i].length();
    			}
    			
    			
    		}
    		
    		
    			return miscCost;
        }
    		
    }
    
    
    


    The outcome is very close...just still not correct. Any ideas on what is missing? Thanks.
  2. In Topic: Using Multiple Selection Choices

    Posted 31 May 2014

    View PostCasiOo, on 31 May 2014 - 06:29 AM, said:



    And...I am going to take your advice on the parallel arrays. I don't really need the other one, so I will ditch it:

    private int[] extraStuff{ 10, 30, 2, 3 };
    


    I can just add those into the get method, once I figure out how to grab the indexes from the Object array.
  3. In Topic: Using Multiple Selection Choices

    Posted 31 May 2014

    View PostCasiOo, on 31 May 2014 - 06:29 AM, said:

    Ah my bad on the
    selectedMiscList.setListData(miscList.getSelectedValuesList());
    
    

    setListData accepts a Vector, and getSelectedValuesList returns a List
    Vector is implementing List, so you can't actually just pass a List to the method
    I'll have to take that one on my cape ^^/>/>

    What you instead could be doing (Checked it this time!):
    		public void actionPerformed(ActionEvent e)
    		{
    			Vector<String> selectedValues = new Vector<String>();
    			selectedValues.addAll(miscList.getSelectedValuesList());
    			selectedMiscList.setListData(selectedValues);
    		}
    
    


    You also notice how your misc and extraStuff arrays are parallel?
    This is a bad design, and you should be implementing a new class instead of working with parallel arrays
    There are tutorials here on DIC, teaching you how to move away from parallel arrays
    	// This method will return the cost of the misc items
    	public double getMiscCost()
    	{
    		double miscCost = 0.0;
    		
    		//Loop through all of the selected indexes
    		for (Integer selectedIndex : selectedMiscList.getSelectedIndices()) {
    			//Lookup the value in the extraStuff array, and add it to miscCost
    			miscCost += extraStuff[selectedIndex];
    		}
    		
    		return miscCost;
    	}
    
    


    Thanks for helping me out. I was curious to see if this revision would work, but it still is not giving the correct results. It will only give me the single selections not the multiple.

    Would there be any way of actually sending the Object array as an argument to the method that handles the calculation?

    public double getMiscCost(Object[] selection)
    


    And then maybe pull out the results from its index? I know that the Object array would have to be parsed somehow to an int. Would that work?
  4. In Topic: Using Multiple Selection Choices

    Posted 30 May 2014

    View PostCasiOo, on 30 May 2014 - 05:46 PM, said:

    Can I see how you declare your JLists, please?
    Did you remember to give them a generic type? You might have to lookup what it is


    I have this:

    private JList miscList;                                             // The misc products
    	private JList selectedMiscList;                                     // Selected misc products
    



    And then I have this in the ButtonListener:

    Object[] selection = selectedMiscList.setListData(miscList.getSelectedValuesList());
    


    Did I screw that up? I apologize for all of these questions...I have been working on this assignment since Sunday and it really is driving me crazy. What is worse is that the book that I have does not give me any examples on how to calculate when using multiple selections. They just teach you how to get the values when the user presses the button.
  5. In Topic: Using Multiple Selection Choices

    Posted 30 May 2014

    View PostCasiOo, on 30 May 2014 - 07:55 AM, said:

    You guys are going the wrong way when trying to solve the problem
    Please remember that JList is generic JList<E>

    Quote

    E - the type of the elements of this list

    So you can specify which elements the JList should contain, so you don't have to do any casting or parsing of objects

    This:
    public void actionPerformed(ActionEvent e)
    {
    	// Get the selected values
    	Object[] selection =  miscList.getSelectedValues();
    	
    	// Store the selected items in selectedMiscList
    	selectedMiscList.setListData(selection);
    }
    
    

    Becomes:
    public void actionPerformed(ActionEvent e)
    {
    	selectedMiscList.setListData(miscList.getSelectedValuesList());
    }
    
    


    Your IDE should give you a warning, because you have forgot to give JList a generic type :)/>/>


    I understand fully of what you are explaining; however I am getting an error on the setListData method with this line:

    public void actionPerformed(ActionEvent e)
    {
    	selectedMiscList.setListData(miscList.getSelectedValuesList());
    }
    


    I get a compiler error telling me that setListData is not applicable for the arguments JList and one of the cures is to cast the argument 1 to Vector....

My Information

Member Title:
D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Click here to e-mail me

Friends

neewb hasn't added any friends yet.

Comments

neewb has no profile comments yet. Why not say hello?