Reputation: 5
i am new to JAVA and is it possible for the Textfields to only accept numerical inputs? Is it also possible to have an equation inside the code? e.g: (gross*10)
Here is the code that i wanted to have a numerical input: //Display private class Handler implements ActionListener{ public void actionPerformed(ActionEvent e){
String name = fName.getText();
String email = fEmail.getText();
String movie = fMovie.getText();
String role = fRole.getText();
String salary = fSalary.getText(); // < I WANT THIS TO VARIABLE TO BE DOUBLE
String gross = fGross.getText(); // < SO THIS ASWELL
JOptionPane.showMessageDialog(null,"Name: " + name +"\n Email: " +email +"\n Movie: " +movie +"\n Role: " +role +"\n Salary: " +salary +"\n Gross: " +gross);
}
}
FULL CODE:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Text1 {
static Container contain;
static JFrame f;
static JPanel p;
static JLabel l,lblName,lblEmail,lblMovie,lblRole,lblSalary,lblGross;
static JTextField fName,fEmail,fMovie,fRole,fSalary,fGross;
static JButton submit;
Handler h = new Handler();
public static void main(String[] args) {
new Text1();
}
public Text1(){
f = new JFrame("Movies");
f.setLayout(null);
contain = f.getContentPane();
p = new JPanel();
p.setLayout(null);
p.setBounds(0,0,700,700);
contain.add(p);
l = new JLabel("<html>Write a program to accept the name, email address, latest movie, role, salary, box office gross. Display all the inputs and the bonus and total income.The total income came from the salary and the bonus which is the 10% of the box office gross.</html>");
l.setBounds(10,5,670,100);
p.add(l);
//--Name
lblName = new JLabel("Name: ");
lblName.setBounds(78,52,89,150);
p.add(lblName);
//--Email
lblEmail = new JLabel("Email: ");
lblEmail.setBounds(78,84,89,150);
p.add(lblEmail);
//--Movie
lblMovie = new JLabel("Latest Movie: ");
lblMovie.setBounds(78,116,130,150);
p.add(lblMovie);
//--Role
lblRole = new JLabel("Role: ");
lblRole.setBounds(78,148,89,150);
p.add(lblRole);
//--Salary
lblSalary = new JLabel("Salary: ");
lblSalary.setBounds(78,180,89,150);
p.add(lblSalary);
//--Gross
lblGross = new JLabel("Gross: ");
lblGross.setBounds(78,212,89,150);
p.add(lblGross);
//TextFields :3
fName = new JTextField();
fName.setBounds(217,117,150,20);
p.add(fName);
fEmail = new JTextField();
fEmail.setBounds(217,147,150,20);
p.add(fEmail);
fMovie = new JTextField();
fMovie.setBounds(217,180,150,20);
p.add(fMovie);
fRole = new JTextField();
fRole.setBounds(217,213,150,20);
p.add(fRole);
fSalary = new JTextField();
fSalary.setBounds(217,244,150,20);
p.add(fSalary);
fGross = new JTextField();
fGross.setBounds(217,275,150,20);
p.add(fGross);
//Buttonerino
submit = new JButton("Submit");
submit.setBounds(78,310,80,20);
submit.addActionListener(h);
p.add(submit);
f.setSize(710,710);
f.setVisible(true);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//Display
private class Handler implements ActionListener{
public void actionPerformed(ActionEvent e){
String name = fName.getText();
String email = fEmail.getText();
String movie = fMovie.getText();
String role = fRole.getText();
String salary = fSalary.getText();
String gross = fGross.getText();
JOptionPane.showMessageDialog(null,"Name: " + name +"\n Email: " +email +"\n Movie: " +movie +"\n Role: " +role +"\n Salary: " +salary +"\n Gross: " +gross);
}
}
}
Upvotes: 0
Views: 115
Reputation: 100
formatText = new JFormattedTextField(createFormatter("######"));
formatText.setColumns(20);
This example will only allow user to enter digits. I set it to allow only 6 digits, but you can change that number with more or fewer #'s.
You can also do the calculation and assign the answer to the Text field
Upvotes: 1