Get state/value of all user input components
I have designed a Java GUI which contains various components on different panels: comboBoxes, textFields, buttons, checkBoxes, etc. Most of those components are generated dynamically by corresponding methods. I want to have the present values of all input fields. I do not need to know if/when a component property was changed, what I need is the current value/state (set by the user or present upon initializing the GUI). Is there a way to simply get those values/states without implementing item or action listeners?
To give an example, a comboBox contains elements from arrays. When the GUI is shown the value within the comboBox is set by default to be equal to the first array element. The user can, of course, select any other element. Then (upon pressing a button e.g.) it should be determined which element is currently selected/set within that comboBox (and all other ones). I do not need to know if the selected value has changed, thus it may be possible to do this without listeners. Even with listeners it would not be possible to have an elegant solution when a value does not change and remains as initialized. Any suggestions?
# 4
> What is the "state" of a component?
- If a component is a JCheckbox then the state is selected or deselected
- If a component is a JComboBox then the selected value is the one currently shown within that box
- If a component is a JTextField then the value is the text entered by the user into it
> If the components are added to a Container then you can get all the components
> of the Container and iteraterate though each component to get the required
> "state" information.
Principally that is what I am looking for. Could you be a bit more specific please: do I need to add each relevant component to the Container during its generation (even if some of the generated components are not always visible)?
# 5
Do you look for something like that?
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
/**
*
* @author Annette
*/
public class StateTest extends JFrame implements ActionListener {
JPanel panel;
int i = 0;
/** Creates a new instance of StateTest */
public StateTest() {
panel = new JPanel();
JPanel buttonPanel = new JPanel();
JButton b = new JButton("Add CheckBox");
b.addActionListener(this);
buttonPanel.add(b);
b = new JButton("Add ComboBox");
b.addActionListener(this);
buttonPanel.add(b);
b = new JButton("Add TextField");
b.addActionListener(this);
buttonPanel.add(b);
b = new JButton("Get State");
b.addActionListener(this);
buttonPanel.add(b);
getContentPane().add(panel, BorderLayout.CENTER);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
this.setPreferredSize(new Dimension(600,400));
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Add CheckBox")) {
panel.add(new JCheckBox("CheckBox " + i));
i++;
panel.revalidate();
panel.repaint();
}
else if (e.getActionCommand().equals("Add ComboBox")) {
String[] d = {i+"-1", i+"-2", i+"-3"};
JComboBox cb = new JComboBox(d);
panel.add(cb);
i++;
panel.revalidate();
panel.repaint();
}
else if (e.getActionCommand().equals("Add TextField")) {
panel.add(new JTextField("" + i, 20));
i++;
panel.revalidate();
panel.repaint();
}
else if (e.getActionCommand().equals("Get State")) {
for (int j=0; j<panel.getComponentCount(); j++) {
Component c = panel.getComponent(j);
if (c instanceof JCheckBox) {
JCheckBox cb = (JCheckBox)c;
System.out.println("JCheckBox " + cb.getText() + " " + cb.isSelected());
}
else if (c instanceof JComboBox) {
JComboBox cb = (JComboBox)c;
System.out.println("JComboBox " + cb.getSelectedItem());
}
else if (c instanceof JTextField) {
JTextField tf = (JTextField)c;
System.out.println("JTextField " + tf.getText());
}
}
}
}
public static void main(String[] args) {
StateTest st = new StateTest();
}
}
Annette
Life is supposed to be fun, you know.>