2

I am trying to make an executable JButton (which opens a new window)radiobutton is chosen and the textfiled is filled within a specific range (the textfield should be from 1800 to 2013) . For the radiobuttons I made a default choise for now, but I cannot figure out how can I return a warning that the textfield should be filled (a number between 1800 and 2013) and if it is there then it run the program.

EDIT: So if my code is:

JFrame ....
JPanel ....

JTextField txt = new JTextField();
JButton button = new JButton("run");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//Do things here
}
});
txt.addFocusListener(new FocusListener() {
      ....
}

how can I use the ItemStateListener. Should I define a listener and then what?

POD
  • 509
  • 8
  • 20
  • 1
    When the button is clicked (in it's `actionPerformed` method), you could check the range and use a `JOptionPane` to if the values are out of range. You could use a `ChangeListener` or `ItemStateListener` to monitor the changes of the radio buttons and only enable the `JButton` when the rangers are within acceptable ranges. – MadProgrammer Apr 20 '13 at 03:51
  • Can your question be stated as *"How to issue a warning if required text field is empty?"*? If so, add that as an [edit to the question](http://stackoverflow.com/posts/16116354/edit). If not, form a question and do the same. – Andrew Thompson Apr 20 '13 at 03:51
  • 1
    *"textfield should be filled (a number between 1800 and 2013)"* For numeric values, use a `JSpinner` instead. – Andrew Thompson Apr 20 '13 at 03:59
  • @MadProgrammer Thanks, that is a good choise. – POD Apr 20 '13 at 04:35
  • @AndrewThompson Good point for the 'JSpinner'. – POD Apr 20 '13 at 04:37
  • 1
    I would suggest, you to please have a look at this wonderful answer, [Drawing warning symbols during form validation in Swing](http://stackoverflow.com/a/14170292/1057230) by @DavidKroukamp, which might be of interest to you on the topic – nIcE cOw Apr 20 '13 at 04:51
  • 1
    This answer regarding, [allow introducting digits in JTextField](http://stackoverflow.com/a/9478124/1057230), is again a good topic to look at. More information can be found on Java Docs related to [implementing Document Listeners](http://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html#filter) :-) – nIcE cOw Apr 20 '13 at 04:59

1 Answers1

3
public void actionPerformed(ActionEvent e)  
{  
    String s = txt.getText();  
    char[] cArr = s.toCharAray();  
    ArrayList<Character> chars = new ArrayList<Character>();  
    for (char c : cArr)  
        if (c.isDigit())  
            chars.add(c);  
    cArr = new char[chars.size()];  
    for (int i = 0;i<chars.size();i++)  
        cArr[i] = char.get(i);  
    s = new String(cArr);  
    txtField.setText(s);  
    if (s.equals(""))  
    {  
        // issue warning  
        return;  
    }  
    int input = Integer.parseInt(s);  
    if (input >=  1800 && input <= 2013)  
    {  
        // do stuff  
    }  
}  

Basically, read the string in the text field, remove all non-numeric characters from it, and only proceed if it is in the range specified.

Michael Hall
  • 187
  • 5
  • My biggest concern is about the conditional button. How can I pass the condition of the textfield (whether it fullfill the condition or not) into the actionlistener that is desined for the button? – POD Apr 20 '13 at 04:33
  • If the TextField is globally accessible (which it should be) then you can access the text it contains with txt.getText(); Just use the code I posted above to check if it meets the condition. – Michael Hall Apr 20 '13 at 04:37
  • http://pastebin.com/QU0JxmCN This method will check if the text meets the condition. – Michael Hall Apr 20 '13 at 04:44