0

I am doing my first java program with a GUI. I need to change the input from the user entered into a jTextField to a int variable i can use for work.

Nat Ritmeyer
  • 5,634
  • 8
  • 45
  • 58
sobey
  • 1
  • 4

5 Answers5

0

Get the text and use Integer.parseInt(your String here);

prmottajr
  • 1,816
  • 1
  • 13
  • 22
0
int a = Integer.parseInt(jtextfield.getText());
// `jtextfield` will be your `JTextField` object
AJ.
  • 4,526
  • 5
  • 29
  • 41
0
    JTextField jTextField=new JTextField(); // initialize textFild
    String str=jTextField.getText(); // get text value 

Now you can convert this into int value.

    int val=Integer.parseInt(jTextField.getText());
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

jtextfield.gettext() will return the entered String.. we can parse this string into integer

integer.parseInt(txtfield.getText());

but if we are typing values other than numbers 0-9 in the text filed it show java.lang.NumberFormatException

only numbers can be parsed into the integer

int num=Integer.parseInt("2")// is correct

int num=Integer.parseInt("two")//this will give numberformat exception
Java_Alert
  • 1,159
  • 6
  • 24
  • 50
Lijo
  • 6,498
  • 5
  • 49
  • 60
0
int result = Integer.parseInt(jTextField.getText());

and apply

class MyIntFilter extends DocumentFilter {
   @Override
   public void insertString(FilterBypass fb, int offset, String string,
         AttributeSet attr) throws BadLocationException {

      Document doc = fb.getDocument();
      StringBuilder sb = new StringBuilder();
      sb.append(doc.getText(0, doc.getLength()));
      sb.insert(offset, string);

      if (test(sb.toString())) {
         super.insertString(fb, offset, string, attr);
      } else {
         // warn the user and don't allow the insert
      }
   }

   private boolean test(String text) {
      try {
         Integer.parseInt(text);
         return true;
      } catch (NumberFormatException e) {
         return false;
      }
   }

   @Override
   public void replace(FilterBypass fb, int offset, int length, String text,
         AttributeSet attrs) throws BadLocationException {

      Document doc = fb.getDocument();
      StringBuilder sb = new StringBuilder();
      sb.append(doc.getText(0, doc.getLength()));
      sb.replace(offset, offset + length, text);

      if (test(sb.toString())) {
         super.replace(fb, offset, length, text, attrs);
      } else {
         // warn the user and don't allow the insert
      }

   }

   @Override
   public void remove(FilterBypass fb, int offset, int length)
         throws BadLocationException {
      Document doc = fb.getDocument();
      StringBuilder sb = new StringBuilder();
      sb.append(doc.getText(0, doc.getLength()));
      sb.delete(offset, offset + length);

      if (test(sb.toString())) {
         super.remove(fb, offset, length);
      } else {
         // warn the user and don't allow the insert
      }

   }
}

(This class copied from Link)

PlainDocument doc = (PlainDocument) jTextField.getDocument();
doc.setDocumentFilter(new MyIntFilter());

This way you can restrict your input to numbers and get the string to integer by parsing it

Community
  • 1
  • 1
SKT
  • 1,821
  • 1
  • 20
  • 32