Java – format text fields with jcombobox
I have a GUI window asking for break time The result I want is, for example, 1:15 – int hours = 1 and int mins = 15 – after clicking the Continue button The result I get is either an hour or a minute, because I can't let JCombo@R_328_2419 @Work with JButton (I think) In addition, I'm not sure how to check whether the user has entered a number or an invalid input This 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" }; JCombo@R_328_2419@ timeList = new JCombo@R_328_2419@(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)); 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
Solution
You need a valid reference to the combo box visible in the action listener so that the actionlistener can call the method on it and extract the value it holds At present, your JCombo@R_328_2419 @Declared in the constructor of a class, so it is only visible in the constructor and not elsewhere To solve this problem, the combo box needs to be a class field, which means that it is declared in the class itself, not some methods or constructors
For example:
import java.awt.event.*; import javax.swing.*; public class Foo002 extends JPanel implements ActionListener { JCombo@R_328_2419@ combo1 = new JCombo@R_328_2419@(new String[]{"Fe","Fi","Fo","Fum"}); public Foo002() { JCombo@R_328_2419@ combo2 = new JCombo@R_328_2419@(new String[]{"One","Two","Three","Four"}); JButton helloBtn = new JButton("Hello"); helloBtn.addActionListener(this); // I really hate doing this! add(combo1); add(combo2); add(helloBtn); } private static void createAndShowGUI() { JFrame frame = new JFrame("FormattedTextFieldDemo"); frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); frame.add(new Foo002()); 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) { // this works because combo1 is visible in this method System.out.println(combo1.getSelectedItem().toString()); // this doesn't work because combo2's scope is limited to // the constructor and it isn't visible in this method. System.out.println(combo2.getSelectedItem().toString()); } }