Results 1 to 8 of 8
- 05-23-2008, 05:21 AM #1
Member
- Join Date
- May 2008
- Posts
- 21
- Rep Power
- 0
[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:
- 05-23-2008, 05:39 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You try to get the array name on combo box, and corresponding values want to display on text fields. Is that right?
- 05-23-2008, 07:43 AM #3
Member
- Join Date
- May 2008
- Posts
- 21
- Rep Power
- 0
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.
- 05-23-2008, 07:58 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 05-23-2008, 08:20 AM #5
Member
- Join Date
- May 2008
- Posts
- 21
- Rep Power
- 0
Java Code:public void itemStateChanged(ItemEvent evt) { plainWrapComboBox.???
- 05-23-2008, 08:29 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Return an object there. You can cast it into string.Java Code:getSelectedItem();
- 05-23-2008, 08:57 AM #7
Member
- Join Date
- May 2008
- Posts
- 21
- Rep Power
- 0
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(); } }
- 05-23-2008, 09:17 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
My friend I told this. :);)
getSelectedItem() return an object. So you have to think about that. Like this, much simple.
Try and let me know what happened. :)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 = }
Similar Threads
-
Making a Table Row with JPanel Composed of JTextField and JComboBox Selectable
By datechie in forum AWT / SwingReplies: 2Last Post: 07-30-2008, 12:33 PM -
Adding items to a jComboBox
By tronovan in forum New To JavaReplies: 0Last Post: 08-08-2007, 08:48 AM -
JComboBox setDisabledTextColor
By Jack in forum AWT / SwingReplies: 2Last Post: 07-02-2007, 05:32 AM -
Help with jComboBox
By Marcus in forum AWT / SwingReplies: 2Last Post: 07-01-2007, 11:08 PM -
jcombobox
By Freddie in forum AWT / SwingReplies: 4Last Post: 05-11-2007, 12:48 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks