Results 1 to 7 of 7
Thread: Multiple ComboBox - Reading Text
- 01-04-2010, 09:10 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 3
- Rep Power
- 0
-
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
- 01-04-2010, 09:30 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 3
- Rep Power
- 0
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:
But I would like to read the existing values of all the comboboxes in the panel say if I click on a button./** Listens to the combo box. */
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
String petName = (String)cb.getSelectedItem();
(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.
-
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.
-
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(); } }); } }
- 01-04-2010, 11:22 PM #6
Member
- Join Date
- Jan 2010
- Posts
- 3
- Rep Power
- 0
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.
-
Similar Threads
-
Reading text using PDFBOX
By umadas in forum Advanced JavaReplies: 15Last Post: 01-21-2012, 08:47 AM -
Reading In Text Files
By Dukey in forum New To JavaReplies: 4Last Post: 04-04-2009, 11:53 PM -
Breaking huge text into multiple parts.Please help..
By waNnY in forum New To JavaReplies: 2Last Post: 02-18-2008, 04:24 AM -
Reading text from a URL using BufferedReader
By Java Tip in forum Java TipReplies: 0Last Post: 12-26-2007, 10:17 AM -
Reading text file
By Lennon-Guru in forum New To JavaReplies: 1Last Post: 12-15-2007, 11:38 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks