Results 1 to 8 of 8
  1. #1
    crazydeo is offline Member
    Join Date
    May 2008
    Posts
    21
    Rep Power
    0

    Question [SOLVED] 2 D Array and JComboBox on a JPanel

    I don't know if I will be able to explain this well, but here it goes.

    I havea 2 dimensional array with values in a Jpanel and I want to use a combobox instead of a JList to show the values in the array.

    For example:
    user chooses auto part number in combobox
    3 textfields are updated with corresponding part numbers for generic brands.

    Hopefully some one can help me out!! :confused:

  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
    19

    Default

    You try to get the array name on combo box, and corresponding values want to display on text fields. Is that right?

  3. #3
    crazydeo is offline Member
    Join Date
    May 2008
    Posts
    21
    Rep Power
    0

    Default

    No, actually I have the ComboBox that is is being filled by the values from the array but I need the textfields to populate from the array as well and then correspond with the ComboBox.... When I pick a different value in the ComboBox the textfields should auto populate with the new values.

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

    Default

    Ok, why it's difficult. At the state change of the combobox, get the selected text. Using that I think you can find values which wants to add into text fields. Set text on text fields then.

  5. #5
    crazydeo is offline Member
    Join Date
    May 2008
    Posts
    21
    Rep Power
    0

    Default

    Java Code:
    public void itemStateChanged(ItemEvent evt)
    	{
    		plainWrapComboBox.???

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

    Default

    Java Code:
    getSelectedItem();
    Return an object there. You can cast it into string.

  7. #7
    crazydeo is offline Member
    Join Date
    May 2008
    Posts
    21
    Rep Power
    0

    Default

    I think i'm lost:(:(

    Java Code:
    import java.text.*;
    import javax.swing.*;
    
    import java.awt.event.*;
    
    public class PlainWrapParts extends JFrame implements ItemListener, ActionListener
    {
    	//create the main panel, radio buttons, and text fields for the panel
    	JPanel mainPanel = new JPanel();
    
    	String[][] partNumberString =
        {  {"PR214", "MR43T", "RBL8", "14K22"},
       	{"PR223", "R43", "RJ6", "14K24"},
       	{"PR224", "R43N", "RN4", "14K30"},
       	{"PR246", "R46N", "RN8", "14K32"},
       	{"PR247", "R46TS", "RBL17Y", "14K33"},
       	{"PR248", "R46TX", "RBL12-6", "14K35"},
       	{"PR324", "S46", "J11", "14K38"},
       	{"PR326", "SR46E", "XEJ8", "14K40"},
       	{"PR444", "47L", "H12", "14K44"}    };
        String [] brandString = {"Plain Wrap", "Brand A", "Brand C", "Brand X"};
        int counterInteger = 0;
        int plainWrapInteger [] = new int [14];
        String plainWrapString, brandAString, brandCString, brandXString, nameString;
    	JComboBox plainWrapComboBox = new JComboBox(partNumberString[0]);
    	JTextField brandATextField = new JTextField(5);
    	JTextField brandCTextField = new JTextField(5);
    	JTextField brandXTextField = new JTextField(5);
    	JButton clearButton = new JButton("Clear Items");
    	JButton summaryButton = new JButton("Summary of Sales");
    	JTextField plainWrapAddTextField = new JTextField(5);
    	JTextField brandAAddTextField = new JTextField(5);
    	JTextField brandCAddTextField = new JTextField(5);
    	JTextField brandXAddTextField = new JTextField(5);
    	JButton addButton = new JButton("Add an item");
    	JTextArea outputTextArea = new JTextArea(20,30);
    	JScrollPane mainScrollPane = new JScrollPane(outputTextArea);
    
    
    	public static void main(String[] args)
    	{
    		// Create an object of the class
    		PlainWrapParts myPartsStore = new PlainWrapParts();
    		myPartsStore.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    	}
    	
    	public PlainWrapParts()
    	{
    		//setting title, size of the designFrame
    		designFrame();
    		add(mainPanel);
    		setTitle("Plain Wrap Auto");
    		setSize(500,600);
    		setVisible(true);
    	}
    	
    	public void designFrame()
    	{
    		//add items to the panels
    		
    		mainPanel.add(new JLabel("Plain Wrap"));
    		mainPanel.add(plainWrapComboBox);
    		mainPanel.add(new JLabel("Brand A"));
    		mainPanel.add(brandATextField);
    		mainPanel.add(new JLabel("Brand C"));
    		mainPanel.add(brandCTextField);
    		mainPanel.add(new JLabel("Brand X"));
    		mainPanel.add(brandXTextField);
    		mainPanel.add(new JLabel("Plain Wrap"));
    		mainPanel.add(plainWrapAddTextField);
    		mainPanel.add(new JLabel("Brand A"));
    		mainPanel.add(brandAAddTextField);
    		mainPanel.add(new JLabel("Brand C"));
    		mainPanel.add(brandCAddTextField);
    		mainPanel.add(new JLabel("Brand X"));
    		mainPanel.add(brandXAddTextField);
    		mainPanel.add(addButton);
    		mainPanel.add(clearButton);
    		mainPanel.add(mainScrollPane);
    		
    		brandXTextField.addActionListener(this);
    		brandXAddTextField.addActionListener(this);
    		plainWrapComboBox.addItemListener(this);
    		clearButton.addActionListener(this);
    		addButton.addActionListener(this);
    	}
    	
    	//Different actions performed when the buttons are pushed
    	public void itemStateChanged(ItemEvent evt)
    	{
    		
    		String partNumberString = plainWrapComboBox.getSelectedItem();
    		plainWrapTextField = 
    	}
    	
    	public void actionPerformed(ActionEvent evt)
    	{		
    		Object sourceObject = evt.getSource();
    		
    		//if purchase button pushed everything sent to total class for calculation
    		if(sourceObject == addButton)
    		{
    			getInput();
    			add();
    		}
    		
    		//if clear button pushed everything thing is cleared
    		else if(sourceObject == clearButton)
    		{
    			clear();
    		}
    	}
    	
    	 public void getInput()
    	    {
    	         //validate entry
    	 
    	        try
    	        {
    	            if((!(plainWrapAddTextField.getText()).equals("")))
    	            {
    	                 nameString = plainWrapAddTextField.getText();
    	 
    	                if((!(brandAAddTextField.getText()).equals("")))
    	                {
    	                         nameString = brandAAddTextField.getText();
    	                        if((!(brandCAddTextField.getText()).equals("")))
    	                        {
    	                                nameString = brandCAddTextField.getText(); 
    	                                if((!(brandXAddTextField.getText()).equals("")))
    	                                {
    	                                        nameString = brandXAddTextField.getText(); 
    	 
    	                                try 
    	                                {
    	 
    	                                    add();
    	                                }
    	 
    	                                catch (NullPointerException e)
    	                                {
    	                                	JOptionPane.showMessageDialog(null, "invalid");
    	                    	            brandATextField.selectAll();
    	                    	            brandATextField.requestFocus();
    	                                }
    	                                }
    	                                else
    	                                {    JOptionPane.showMessageDialog(null, "Enter Brand X");
    	                                brandXAddTextField.selectAll();
    	                                brandXAddTextField.requestFocus();
    	                        }}
    	                        else
    	                        {    JOptionPane.showMessageDialog(null, "Enter Brand C");
    	                        brandCAddTextField.selectAll();
    	                        brandCAddTextField.requestFocus();
    	                }}
    	                else
    	                {
    	                        JOptionPane.showMessageDialog(null, "Enter Brand A");
    	                        brandAAddTextField.selectAll();
    	                        brandAAddTextField.requestFocus();
    	                }
    	            }
    	 
    	            else
    	            {
    	                JOptionPane.showMessageDialog(null, "Enter Plain Wrap");
    	                plainWrapAddTextField.selectAll();
    	                plainWrapAddTextField.requestFocus();
    	            }
    	        }
    	        catch(NullPointerException e)
    	        {
    	            JOptionPane.showMessageDialog(null, "invalid");
    	            brandATextField.selectAll();
    	            brandATextField.requestFocus();
    	 
    	        }
    	    }
    	 
    	public void add()
    	{
    		boolean done = false;
    		int arrayCount = plainWrapComboBox.getItemCount() - 1;
    		for (int i = 0; i <= arrayCount; i++)
    		if(plainWrapString.equals(partNumberString[0][i]))
    		{
    		done = true;			
    		break;
    		}
    		if(done)
    		{
    			JOptionPane.showMessageDialog(null, "Duplicate part number!");
    		}
    		else if(plainWrapString.length() != 0 && brandAString.length() != 0 
    		&& brandCString.length() != 0 && brandXString.length() != 0)
    		{
    		plainWrapAddTextField.setText(plainWrapString);
    		brandAAddTextField.setText(brandAString);
    		brandCAddTextField.setText(brandCString);
    		brandXAddTextField.setText(brandXString);
    		}
    	}
    	
    	public boolean checkInput(String userInputString)
    	{
    		boolean foundBoolean = false;
    		int indexInteger = 0;
    		String inputString = new String();
    		
    		while(!foundBoolean && indexInteger < plainWrapComboBox.getItemCount())
    		{
    			inputString = (String) plainWrapComboBox.getItemAt(indexInteger);
    			if(userInputString.equalsIgnoreCase(inputString))
    			{
    				foundBoolean = true;
    			}
    			else
    			{
    				indexInteger++;
    			}
    		}
    		return foundBoolean;
    	}
    	
    	public void displayPlainWrapFound(boolean partsBoolean, String inputString)
    	{
    		if(partsBoolean)
    		{
    			JOptionPane.showMessageDialog(null,"This part is already in the list");
    			plainWrapAddTextField.selectAll();
    			plainWrapAddTextField.requestFocus();
    		}
    		else
    		{
    			plainWrapComboBox.addItem(inputString);
    			JOptionPane.showMessageDialog(null,"This part is added to the list");
    			plainWrapAddTextField.setText("");
    		}
    	}
    	public void clear()
    	{
    		plainWrapAddTextField.setText("");
    		brandAAddTextField.setText("");
    		brandCAddTextField.setText("");
    		brandXAddTextField.setText("");
    		outputTextArea.setText("");
    		plainWrapComboBox.requestFocus();
    	}
    	
    }

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

    Default

    My friend I told this. :);)

    getSelectedItem() return an object. So you have to think about that. Like this, much simple.

    Java Code:
    	//Different actions performed when the buttons are pushed
    	public void itemStateChanged(ItemEvent evt)
    	{
    		String ss = (String) plainWrapComboBox.getSelectedItem();
    		//String partNumberString = plainWrapComboBox.getSelectedItem();
    		//plainWrapTextField = 
    	}
    Try and let me know what happened. :)

Similar Threads

  1. Replies: 2
    Last Post: 07-30-2008, 12:33 PM
  2. Adding items to a jComboBox
    By tronovan in forum New To Java
    Replies: 0
    Last Post: 08-08-2007, 08:48 AM
  3. JComboBox setDisabledTextColor
    By Jack in forum AWT / Swing
    Replies: 2
    Last Post: 07-02-2007, 05:32 AM
  4. Help with jComboBox
    By Marcus in forum AWT / Swing
    Replies: 2
    Last Post: 07-01-2007, 11:08 PM
  5. jcombobox
    By Freddie in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2007, 12:48 AM

Posting Permissions

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