0

I am creating a small program in Java eclipse windowbuilder. The user inputs the left hand side (LHS) and right hand side (RHS) of the inequality statement such that LHS < (m/n) < RHS. The program will output the lowest integer values m and n which satisfy the condition. I have the logic/code required to do this, but I am unsure how to get the user values of LHS and RHS in windowbuilder. The text field to the left is called lhs and the text field on the right is called rhs, which you can see in the image below. How can I get the user's inputs for these (as double data type)? When the user enters these two values, I will put the code logic under actionPerformed. Thanks!

JButton btnCompute = new JButton("Compute");
        btnCompute.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }

enter image description here

 package ntheory;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Ntheory {

    private JFrame frame;
    private JTextField lhs;
    private JTextField rhs;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Ntheory window = new Ntheory();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Ntheory() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        lhs = new JTextField();
        lhs.setBounds(6, 46, 68, 26);
        frame.getContentPane().add(lhs);
        lhs.setColumns(10);

        JLabel label = new JLabel("<");
        label.setBounds(86, 51, 10, 16);
        frame.getContentPane().add(label);

        JLabel label_1 = new JLabel("<");
        label_1.setBounds(159, 51, 10, 16);
        frame.getContentPane().add(label_1);

        rhs = new JTextField();
        rhs.setColumns(10);
        rhs.setBounds(181, 46, 68, 26);
        frame.getContentPane().add(rhs);

        JLabel lblmn = new JLabel("(m/n)");
        lblmn.setBounds(108, 51, 61, 16);
        frame.getContentPane().add(lblmn);

        JButton btnCompute = new JButton("Compute");
        btnCompute.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        btnCompute.setBounds(261, 46, 117, 29);
        frame.getContentPane().add(btnCompute);
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Programmer
  • 1,266
  • 5
  • 23
  • 44
  • Possible duplicate of [How to Retrieve value from JTextField in Java Swing?](http://stackoverflow.com/questions/5752307/how-to-retrieve-value-from-jtextfield-in-java-swing) – OneCricketeer Oct 16 '16 at 09:15
  • Second duplicate. http://stackoverflow.com/questions/5769669/convert-string-to-double-in-java – OneCricketeer Oct 16 '16 at 09:16

1 Answers1

1
JButton btnCompute = new JButton("Compute");
btnCompute.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        try {
            double leftValue = Double.parseDouble(lhs.getText());
            double rightValue = Double.parseDouble(rhs.getText());
            // Do your stuff with it.
        }
        catch ( NumberFormatException ex ) {
            // Do stuff if the input is not a number.
        }
    }
}
Crusha K. Rool
  • 1,502
  • 15
  • 24
  • Thanks! Do you happen to know how I can output something? Say for example If I wanted to add leftValue and rightValue and then display this to the user? double sum = leftValue+rightValue; On command line I would use System.out.println, but I am new to using GUI for java. So how can I output? :) – Programmer Oct 16 '16 at 09:20
  • If there is a method called `getText()`, then guess what other method there is as well. For future reference you might just want to look at the API: https://docs.oracle.com/javase/8/docs/api/javax/swing/JTextField.html – Crusha K. Rool Oct 16 '16 at 09:21
  • I don't follow, I'm unaware of the getText() method as well. I only know how to print using System.out.println("HelloWorld") -> Which I believe is for outputting to console only? (command line) – Programmer Oct 16 '16 at 09:32
  • 1
    @CrushaK.Rool is trying to lead you to `set…` as the API counterpart to `get…`. – trashgod Oct 16 '16 at 10:50