-1

I have a JTextField that a user will enter a number.

How is it that I check whether or not the number that they entered is greater than 0?

I am very new to this so sorry if it is a bit obvious to you.

Here is my code

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;


public class TaxiFrame extends JFrame implements ActionListener {


      private JLabel L1 = new JLabel("Number of Taxis:");
      private JLabel L2 = new JLabel("Type an integer and press enter");
      private JTextField t1 = new JTextField ("            ");




    public TaxiFrame() {
        super("This is the Frame");
        setSize(500, 300);
        getContentPane().setBackground(Color.CYAN);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new BorderLayout(10, 10));

         JPanel p = new JPanel();     


         p.setOpaque(false);
         p.add(L1);
         getContentPane().
         add("South", p);

          p.setOpaque(false);
          p.add(t1);
          getContentPane().
          add("South", p);

          p.setOpaque(false);
          p.add(L2);
          getContentPane().
          add("South", p);

        setVisible(true);

        t1.addActionListener(this);

    }

    public static void main(String[] args) {
        new TaxiFrame();
    }


    public void actionPerformed(ActionEvent e)
    {

        if (e.getSource() == t1)
        {

        }

    }

}
BenMorel
  • 34,448
  • 50
  • 182
  • 322

3 Answers3

0
  1. Get the text from the JTextField. Calling getText() on the JTextField will do this. Check the JTextField tutorial for more on this.
  2. Parse the String to an int with Integer.parseInt(...).
  3. Check if > 0.

In the future, break down all such questions into their individual steps, and try to solve each step at a time. Doing this will help you find your solutions by yourself.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

Try this,

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;


public class TaxiFrame extends JFrame implements ActionListener {


      private JLabel L1 = new JLabel("Number of Taxis:");
      private JLabel L2 = new JLabel("Type an integer and press enter");
      private JTextField t1 = new JTextField ("            ");




    public TaxiFrame() {
        super("This is the Frame");
        setSize(500, 300);
        getContentPane().setBackground(Color.CYAN);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new BorderLayout(10, 10));

         JPanel p = new JPanel();     


         p.setOpaque(false);
         p.add(L1);
         getContentPane().
         add("South", p);

          p.setOpaque(false);
          p.add(t1);
          getContentPane().
          add("South", p);

          p.setOpaque(false);
          p.add(L2);
          getContentPane().
          add("South", p);

        setVisible(true);

        t1.addActionListener(this);

    }

    public static void main(String[] args) {
        new TaxiFrame();
    }


    public void actionPerformed(ActionEvent e)
    {

        if (e.getSource() == t1)
        {
            if(Integer.parseInt(t1.getText().trim()) > 0)
            {
                //Do here
            }
        }

    }

}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Vishal Santharam
  • 1,963
  • 1
  • 16
  • 30
0
 use "textfieldfilter" by making class JTextFieldFilter

JTextField limiting character amount input and accepting numeric only and in @Override public void insertString function provide your condition

hope it help

Community
  • 1
  • 1