Results 1 to 8 of 8
  1. #1
    archcavalier is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Exclamation help get the value from the combo box

    i need to get the value form the combo box since i have to pass the value to another class to compute the total amount.

    here is the code for the combo box...

    private void typeComboActionPerformed(java.awt.event.ActionEven t evt) {
    // TODO add your handling code here:
    int value = 0;

    JComboBox combotype = (JComboBox)evt.getSource();
    String type = (String)combotype.getSelectedItem();

    if("Classic".equals(type))
    {
    value = 55;
    } else if("Rubberized".equals(type))
    {
    value = 150;
    } else if("Plastisol".equals(type))
    {
    value = 300;
    } else
    {
    value = 0;
    }
    }

    i need to pass the value from the combo box to the other classes...

    here are the other classes that needs it value...

    public double getPrice(int thePrice)
    {
    double price = (0.00285 * thePrice) * 2;

    return price;
    }

    public double getPricePerShirt(double theArea)
    {
    double pps = theArea * getPrice(thePrice);

    return pps;
    }

    public double getTotal(int theQty)
    {
    double total = theQty * getPricePerShirt(theArea);

    return total;
    }

    can somebody help me?...

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    maybe have your action handler that receives events from the combo box, have its own listeners, for which you may invent some onAction() interface and make your other classes implement this interface, so that the combo box action handler in turn invokes this custom onAction() to all of its listeners, which haappens to be your classes that need this information.

    a more general idea might be to have everything register with your own central event dispatcher, so that is, this combo box action handler generates a custom event to send the selected value into your own custom event. the advantage of this is you have your custom event dispatcher be the top level controller, a kind of event pipeline where successive events can be fired synchronously if required, where after each event the control comes back to this event manager. its generally more sensible to do this than have event handlers themselves fire events to their own nested listeners. like its more sensible to wire up perhaps with a xml bean factory such as spring framework.

  3. #3
    archcavalier is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    my other problem is that im using netbeans. and im not quite familiar with it. my initial idea was to use one class to handle all the gui and another class to handle all the functions/calculations.

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by archcavalier View Post
    my other problem is that im using netbeans. and im not quite familiar with it. my initial idea was to use one class to handle all the gui and another class to handle all the functions/calculations.
    An excellent idea, if you ask me. Also, travishein's recommendations are (as always) top notch.

  5. #5
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    I've written small program. It shows how to get value from the
    combobox, and use that value with other classes and methods.

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JComboBox;
    import javax.swing.SwingUtilities;
    
    public class ComboHelp {
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			
    			@Override
    			public void run() {
    				HelpFrame frame = new HelpFrame();
    				frame.showGUI();
    			}
    		});
    	}
    }
    
    class HelpFrame extends JFrame {
    	HelpPanel panel;
    	JButton button;
    	public HelpFrame() {
    		panel = new HelpPanel();
    		
    				
    		button = new JButton("Get value from Combo");
    		button.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				OtherClass othclass = new OtherClass();
    				String type = panel.getValueFromCombo();
    				othclass.printSomeStuffs(type);
    			}
    		});
    		
    		panel.add(button);
    		getContentPane().add(panel);
    		
    	}
    	
    	public void showGUI() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(200,200);
    		setLocation(100,100);
    		setVisible(true);
    	}
    }
    
    class HelpPanel extends JPanel {
    	JComboBox comboBox;
    	public HelpPanel() {
    		String[] comboBoxItems = { "monkey", "lion", "tiger", "elephant", "giraffe" };
    		comboBox = new JComboBox(comboBoxItems);
    		add(comboBox);
    		
    		comboBox.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				type = (String)comboBox.getSelectedItem();
    			}
    		});
    		
    	}
    	
    	private String type;
    	
    	public String getValueFromCombo() {
    		return type;
    	}
    }
    
    class OtherClass {
    	public void printSomeStuffs(String type) {
    		if(type != null)
    			System.out.println("This is selected type " +type);
    	}
    }

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    cselic: In your example above, you may wish to simplify the HelpPanel class a bit as there's no need for the intermediate variable "type". e.g.,
    Java Code:
    class HelpPanel extends JPanel {
      JComboBox comboBox;
    
      public HelpPanel() {
        String[] comboBoxItems = {"monkey", "lion", "tiger", "elephant", "giraffe"};
        comboBox = new JComboBox(comboBoxItems);
        add(comboBox);
    
        // comboBox.addActionListener(new ActionListener() {
    
      }
    
      // private String type;
    
      public String getValueFromCombo() {
        // return type;
        return comboBox.getSelectedItem().toString();
      }
    }
    Suerte

  7. #7
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    there's no need for the intermediate variable "type"
    You're absolutely right. Thanks for a good advice.

    I tried to use a variable "type" from the problem:
    Java Code:
     private void typeComboActionPerformed(java.awt.event.ActionEven t evt) { 
    // TODO add your handling code here:
    int value = 0;
    
    JComboBox combotype = (JComboBox)evt.getSource();
    [B][COLOR="Blue"]String type = (String)combotype.getSelectedItem();[/COLOR][/B]
    
    if("Classic".equals(type))
    {
    value = 55;
    } else if("Rubberized".equals(type))
    {
    value = 150;
    } else if("Plastisol".equals(type))
    {
    value = 300;
    } else
    {
    value = 0;
    } 
    }

    Anyway, it is better to write my little program without variable "type".

  8. #8
    archcavalier is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    cselic: In your example above, you may wish to simplify the HelpPanel class a bit as there's no need for the intermediate variable "type". e.g.,
    Java Code:
    class HelpPanel extends JPanel {
      JComboBox comboBox;
    
      public HelpPanel() {
        String[] comboBoxItems = {"monkey", "lion", "tiger", "elephant", "giraffe"};
        comboBox = new JComboBox(comboBoxItems);
        add(comboBox);
    
        // comboBox.addActionListener(new ActionListener() {
    
      }
    
      // private String type;
    
      public String getValueFromCombo() {
        // return type;
        return comboBox.getSelectedItem().toString();
      }
    }
    Suerte
    thats a great idea. ive never thought of that. thanks guys

Similar Threads

  1. combo box help... plus hi this is my first post
    By okabeer in forum AWT / Swing
    Replies: 3
    Last Post: 04-06-2009, 06:05 PM
  2. Combo Box Population
    By barusk in forum Java Applets
    Replies: 1
    Last Post: 03-20-2009, 01:48 PM
  3. creation of one combo box form another combo box
    By er.tyagigaurav in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-09-2008, 03:14 PM
  4. graph + combo box
    By abelah in forum New To Java
    Replies: 7
    Last Post: 10-13-2008, 05: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
  •