Results 1 to 7 of 7
  1. #1
    jorgelex008 is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default Multiple ComboBox - Reading Text

    Hi all,

    Question:

    How can I read the value of multiple ComboBoxes?

    Say I have four ComboBoxes and an "Enter" button.

    When I click on the button I want to read the values of the ComboBoxes.

    Any help much appreciated.

    Thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by jorgelex008 View Post
    How can I read the value of multiple ComboBoxes?
    It's no different from how you would read from one combo box. The key is to be able to get a reference to the comboboxes in order to call getSelectedIndex() or getSelectedItem() on them. For details, please look here: Combo Box Tutorial

  3. #3
    jorgelex008 is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default

    Hi Fubarable,

    I had been looking at it... I managed to read only one combobox at a time upon an actionevent (when I clicked on the combobox).

    from sun's tutorial:
    /** Listens to the combo box. */
    public void actionPerformed(ActionEvent e) {
    JComboBox cb = (JComboBox)e.getSource();
    String petName = (String)cb.getSelectedItem();
    But I would like to read the existing values of all the comboboxes in the panel say if I click on a button.

    (note: I'm new to Java though I have VBA knowledge and some VB... so I know I'm struggling a bit - or a lot! - with the basics).


    thanks.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Since you will be pressing a button and doing things from the button's action listener, getSource won't work, since that will return the button. As I mentioned above, the key here is to have a reference to your combo boxes. If the comboboxes are declared as class variables, and the action listener's code is held in the same class, then the combo boxes will be visible in the action listener's actionPerformed method, and you'll need to call getSelectedItem on these (combo box) variables.

    If this doesn't help, then I advise you to create a very small program that has two simple jcomboboxes and a jbutton and nothing else and try to solve this on the small scale.

    Advise number two: since you are very new to Swing (and it seems Java as well), try to avoid using code-generators such as NetBeans to generate your Swing code. Write it on your own.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    For example:
    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    
    public class MultipleComboBoxes {
       private static final String[] DATA_A = {"0", "1", "2", "3"};
       private static final String[] DATA_B = {"A", "B", "C", "D"};
       
       private JPanel mainPanel = new JPanel();
       
       // combo boxes declared as class level variables 
       private JComboBox comboA = new JComboBox(DATA_A);
       private JComboBox comboB = new JComboBox(DATA_B);
       
       public MultipleComboBoxes() {
          JButton getSelectionBtn = new JButton("Get Selection");
          
          getSelectionBtn.addActionListener(new ActionListener() {
             
             public void actionPerformed(ActionEvent e) {
                
                // in the button's action listener, use the references to both 
                // combo boxes to get the selected items
                Object itemA = comboA.getSelectedItem();
                Object itemB = comboB.getSelectedItem();
                
                String optionString = "comboA: " + itemA.toString() + "\n" +
                		"comboB: " + itemB.toString();
                JOptionPane.showMessageDialog(mainPanel, optionString);
             }
          });
          
          mainPanel.add(comboA);
          mainPanel.add(comboB);
          mainPanel.add(getSelectionBtn);
       }
    
       public JComponent getComponent() {
          return mainPanel;
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("MultipleComboBoxes");
          frame.getContentPane().add(new MultipleComboBoxes().getComponent());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }

  6. #6
    jorgelex008 is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default

    Many thanks! I'll have to mull through through all of this. Your code works great... now I'm studying it. (lots to learn! also reading over java guides).

    Thanks again.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by jorgelex008 View Post
    Many thanks! I'll have to mull through through all of this. Your code works great... now I'm studying it. (lots to learn! also reading over java guides).
    Thanks again.
    You're quite welcome. Oh, and welcome to the forum!

Similar Threads

  1. Reading text using PDFBOX
    By umadas in forum Advanced Java
    Replies: 15
    Last Post: 01-21-2012, 08:47 AM
  2. Reading In Text Files
    By Dukey in forum New To Java
    Replies: 4
    Last Post: 04-04-2009, 11:53 PM
  3. Replies: 2
    Last Post: 02-18-2008, 04:24 AM
  4. Reading text from a URL using BufferedReader
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-26-2007, 10:17 AM
  5. Reading text file
    By Lennon-Guru in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 11:38 PM

Tags for this Thread

Posting Permissions

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