I’ve got a GUI window asking for time spent on a break. The outcome that I would like to get is, for example, 1:15 – int hours = 1 and int mins = 15 – after you click the continue button. The outcome I’m getting is either hours, or mins, because I can’t make JComboBox and JButton work together (I think). Also, I don’t quite know how to check whether user entered a number or an invalid input. Here is the code:
@SuppressWarnings("serial")
public class FormattedTextFields extends JPanel implements ActionListener {
private int hours;
private JLabel hoursLabel;
private JLabel minsLabel;
private static String hoursString = " hours: ";
private static String minsString = " minutes: ";
private JFormattedTextField hoursField;
private NumberFormat hoursFormat;
public FormattedTextFields() {
super(new BorderLayout());
hoursLabel = new JLabel(hoursString);
minsLabel = new JLabel(minsString);
hoursField = new JFormattedTextField(hoursFormat);
hoursField.setValue(new Integer(hours));
hoursField.setColumns(10);
hoursLabel.setLabelFor(hoursField);
minsLabel.setLabelFor(minsLabel);
JPanel fieldPane = new JPanel(new GridLayout(0, 2));
JButton cntButton = new JButton("Continue");
cntButton.setActionCommand("cnt");
cntButton.addActionListener(this);
JButton prevButton = new JButton("Back");
String[] quarters = { "15", "30", "45" };
JComboBox timeList = new JComboBox(quarters);
timeList.setSelectedIndex(2);
timeList.addActionListener(this);
fieldPane.add(hoursField);
fieldPane.add(hoursLabel);
fieldPane.add(timeList);
fieldPane.add(minsLabel);
fieldPane.add(prevButton);
fieldPane.add(cntButton);
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
add(fieldPane, BorderLayout.CENTER);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("FormattedTextFieldDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new FormattedTextFields());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("cnt")) {
hours = ((Number) hoursField.getValue()).intValue();
minutes = Integer.parseInt(timeList.getSelectedItem().toString());
// \d mean every digit charater
Pattern p = Pattern.compile("\\d");
Matcher m = p.matcher(hoursField.getValue().toString());
if (m.matches()) {
System.out.println("Hours: " + hours);
System.out.println("Minutes: " + minutes);
} else {
hoursField.setValue(0);
JOptionPane.showMessageDialog(null, "Numbers only please.");
}
}
}
} // end class
–EDIT–
updated ActionPerformed method
You need a valid reference to the combo box that is visible in the action listener for the ActionListener to be able to call methods on it and extract the value it is holding. Currently your JComboBox is declared in the class’s constructor and so is only visible in the constructor and no where else. To solve this the combo box needs to be a class field, meaning it is declared in the class itself, not some method or constructor.
For example: