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 program for 2D Array with values in Jpanel and combobox to display values in array

  1. #1
    Junior Member
    Join Date
    May 2008
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Java program for 2D Array with values in Jpanel and combobox to display values in array

    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!!

    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","PR223","PR224","PR246","PR247","PR248","PR324", "PR326","PR444"},
            {"MR43T","R43","R43N","R46N","R46TS","R46TX","S46","SR46E","47L"},
            {"RBL8","RJ6","RN4","RN8","RBL17Y","RBL12-6","J11","XEJ8","H12"},
            {"14K22","14K24","14K30","14K32","14K33","14K35","14K38","14K40","14K44"}};
        String [] brandString = {"Plain Wrap", "Brand A", "Brand C", "Brand X"};
        int counterInteger = 0;
        int plainWrapInteger [][] = new int [14][4];
        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 ss = (String) plainWrapComboBox.getSelectedItem();
     
    	}
     
    	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 (Exception 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(Exception 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();
    	}
    }
    Last edited by crazydeo; May 27th, 2008 at 12:47 PM. Reason: added code


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Smile Re: 2 D Array and JComboBox on a JPanel

    Do you want all the values in 'partNumberString' to be visable in the combobox? I cannot see a JList in your code..

    Which 3 text fields do you want to update once a selection is made from the combobox?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    May 2008
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 D Array and JComboBox on a JPanel

    I want brandATextField, BrandCTextField, and BrandXtextField to be updated once a selection has been made in the combobox with the corresponding info from partString. The other textfields are only to add new parts to the array.

  4. #4
    Junior Member
    Join Date
    May 2008
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 D Array and JComboBox on a JPanel

    Any ideas?

  5. #5
    Banned
    Join Date
    May 2008
    Posts
    25
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: 2 D Array and JComboBox on a JPanel

    Quote Originally Posted by crazydeo View Post
    Any ideas?
    Take a look at the addnew() method i wrote for you in that other topic. Replace the JTextarea with textfield and it will update those fields.

Similar Threads

  1. Problem with reset on JComboBox
    By WorkingMan in forum AWT / Java Swing
    Replies: 4
    Last Post: April 25th, 2013, 01:19 PM
  2. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 09:02 AM