Swing/AWT/SWT :: How To Validate User Input In Dialog Box
Oct 20, 2014
I need to validate an input box so that the user can enter only round numbers.
The user can't enter any other characters except 0 to 9, they shouldn't be able to enter decimal points either.
They would be answering questions like, 'What is 7 times 8?'
Also, I need to validate it so that the program doesnt crash if they leave the input box blank or empty.
View Replies
ADVERTISEMENT
Apr 19, 2014
I am trying to match validate user input via regex comparison. But i fail to do so.
The string that will be inserted will have the following pattern: "digit/s " and it can be repeated a random number of times.
The program will extend to characters in the future but for now i am struggling with digits.
i am using a StringBuilder to build the regex string. Or maybe i could use something else but so far i didn't make it.
So far i have this regex
Java Code: "(d+s+)+" mh_sh_highlight_all('java');
The problem is that the newline character will interfere with the last number.
So if i enter
Java Code: "3 4 555" mh_sh_highlight_all('java');
- it will not match. And the regex will only see 3 4.
But if i enter
Java Code: "3 4 555 " mh_sh_highlight_all('java');
- with an extra space at the end it works. Obviously that is not the desired case.
I did find a workaround, but i fear it is somewhat broken
I changed to
Java Code: "(d+s*)+" mh_sh_highlight_all('java');
But in this case the pattern should see 3 4 5 5 5 as the regular expression. Am i correct?
I also tried some other variants but none of them worked:
Java Code: "d+(s+|*)" mh_sh_highlight_all('java');
Or i could do something like
Java Code: "(d+s){" + i + "}" mh_sh_highlight_all('java');
- where i is the number of values i enter.
Am i on the right track here? What am i forgetting?
View Replies
View Related
Apr 9, 2015
Where do I input this segment?
Scanner user_input = new Scanner( System.in );// This line goes at the top with the other global variables
public void getUserInput(){
System.out.print("Enter litres: ");
litres = user_input.nextDouble( );
System.out.print("Enter mileage: ");
mileage = user_input.nextDouble( );
System.out.print("Enter speed: ");
speed = user_input.nextDouble( );
[Code] ....
This is my client file.
class Client{
public static void main(String []args){
Bike R1=new Bike(5.0, 60.0,30.0);//create bike object with params
Bike R2=new Bike();//without params
System.out.println(R1.increaseSpeed());//calling methods
System.out.println(R1.maxDistance());
System.out.println(R2.increaseSpeed());
System.out.println(R2.maxDistance());
}
}
View Replies
View Related
Oct 20, 2014
I'd like to change the text on the two buttons which I have in my Dialog Box, how should I do this?
Also, when I click the X Button in the top right of the Dialog Box, nothing happens, and the program only stops when I end it in jGrasp. How do I make this button close the program when clicked on?
import javax.swing.*;
import java.util.Random;
public class SwingInputExample {
public static void main(String args[]) {
[Code] .....
View Replies
View Related
Oct 13, 2014
Below is my current program for my java class. The assignment is to validate a date when entered by user.
import java.util.*;
public class Date{
public static void main (String arg[]){
int month = 0;
int day = 0;
int year = 0;
[Code] ......
My issue with my program is the output for the day.
If I enter 30 for the date I get this: Date is invalid
Date is invalid
Date is invalid
Date is invalid
Date is invalid
I wanted to know how what is wrong and why it is doing this.
View Replies
View Related
Sep 21, 2014
I'm doing a problem where someone will input a name and it will show their title or vice versa. It works fine when the user inputs the name but whenever they input a title it is coming back not found. Not sure what I did wrong.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class JEmployeeTitle2 extends JApplet implements //class extends JApplet and AL.
ActionListener
[code]....
View Replies
View Related
Apr 22, 2013
Code given below does real time validation for 2 JTextFields. While entering some values to txt1 and txt2 enables the save button and removing values from txt2 or txt1 reset the save button to disable. I use Netbeans as IDE.What I want to do is, enable Save button after checking multiple JTextFields for validity. If any of the text fields is empty, btnSave must be disabled.This program gives expected result up to some extent. But there is little issue. After form appears for the first time, When I type something on Textfield1, Save button enables without checking Textfield2. This happens only at the first time.
public class NewJFrame extends javax.swing.JFrame {
private Boolean isValidFromTextField1 = true;
private Boolean isValidFromTextField2 = true;
public NewJFrame() {
initComponents();
btnSave.setEnabled(false);
[code]....
View Replies
View Related
Jun 7, 2014
I'm developing a Swing application for the first time. To test my application, I have currently hardcoded some text labels and fields in a JPanel, so that a user can enter input details for a maximum of 3 segments. But this limits the maximum number of segments to 3. However, the requirement is that when the user enters the number of segments (could be greater than 3 as well), corresponding number of input sets need to be taken.
Screenshot attached, with values entered.
Basically I want to know how I should go about taking the inputs from the user.
View Replies
View Related
Feb 20, 2014
The below program ask the user to enter the value of x 1 , Y 1 and radius in separate boxes , What would look nice is if the user can Enter the Value X ,Y and radius in the same box.
import javax.swing.JOptionPane;
public class C3E29GeometryTwoCircles {
public static void main(String[] args) {
// Ask the user to enter the x1 coordinate.
String strNumber = JOptionPane.showInputDialog("Enter the value of x1 coordinate " );
double x1 = Double.parseDouble(strNumber);
strNumber = JOptionPane.showInputDialog("Enter the value of y1 coordinate " );
double y1 = Double.parseDouble(strNumber);
[code]...
View Replies
View Related
Aug 9, 2001
I am wondering how to do the following:when a user clicks on a save menu item, a save JFileChooser pops up. I would like to have a default file name set in the FileName box on the chooser, similar to what applications like Microsft word has. Right now, no file name will appear until the user has selected a file.I also need to know how to get the text from this box in the case that the user enters a new file name (the getSelection function is not useful here because the user will not have selected an existing file.)
View Replies
View Related
Mar 7, 2014
I wish to display a progress bar during a lengthy file translation process. When I created a ProgressBar class to do this and tested it with a short test program, it seemed to work fine. But when I add my ProgressBar class to a package containing various other classes, put that package in a jar file, and then execute the class using the progress bar from a menu in my main program, the frame of the progress bar shows up with the appropriate caption but the JLabel, JProgressBar, and JButton are not displayed. I have gone over my code numerous times and checked that the ProgressBar class constructor is getting the proper arguments.
// package gov.nasa.jpl.ephemeris;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Progress bar with message text above the bar and a Cancel button below it
*/
public class ProgressBar extends JFrame {
private boolean _cancelled = false;
private JProgressBar _progressBar = null;
[Code] ....
I have also been wondering whether I need a separate instance of the border for each panel, or whether I can just create one instance and use it for all three panels.
View Replies
View Related
Feb 17, 2014
The question is Write a program that displays all the numbers from 100 to 1000, ten per line, that are divisible by 5 and 6. and separated by exactly a space.
My assignment requirements are to display this in Dialog Box / message box . I have written this code so far
import javax.swing.JOptionPane;
public class Exercise04_10 {
public static void main(String[] args) {
int count = 1;
for (int i = 100; i <= 1000; i++)
if (i % 5 == 0 && i % 6 == 0)
How to display the output in dialog box?
View Replies
View Related
Apr 22, 2014
I want to calculate the height of a dialog that contain only a table depending on how many rows that table it has so I'm using the cross multiplication rule.For example i check initially that my table is well fitting into the dialog(Without space at the bottom) for some value and after that i use the cross multiplication rule because data are dynamic.But unfortunately this rule does not solve the problem.What's the best rule for fitting a dynamic table inside a dialog or a frame? Here is my code.
public class FlowLayoutChangingGap {
public static void main(String[] args) {
// just for testing perpose but data are getting from database and it's dynamic
Object[][] data = {
{ "Kathy", "Smith", "Snowboarding", new Integer(5) },
{ "John", "Doe", "Rowing", new Integer(3) },
[Code] ....
View Replies
View Related
Apr 9, 2014
Write a java program that will ask a user to input grades until the user inputs the sentinel value -1. The program should test each input to determine whether 0<=grade<=100. If the grade entered is within this range, the program should print "Grade accepted;" if not, the program should print "Invalid input".
View Replies
View Related
Nov 4, 2014
I have been struggling with this program for weeks. This program is supposed to take a user's inputted odd number and then print out all prime numbers lower than that number.
public class PrimeNumber
{
Scanner scan = new Scanner(System.in);
int userNum;
String neg;
public void getUserNum()
[code]...
View Replies
View Related
May 2, 2014
I've used Netbeans IDE to create a simple Swing JFrame container, class NewJFrame.
NewJFrame calls another JFrame with dialog in it. Problem is there is an endless call made to it. Here's the issue relevant piece of code:
//Code for calling class
package p;
public class NewJFrame extends javax.swing.JFrame implements ActionListener {
.........
public NewJFrame() {
initComponents(); //IDE auto generated code for binding
[Code] ....
I searched but i cannot understand why run class is getting called multiple times. I have attached the screen prints for both the screens. 2nd screen print(of called class) is very shallow since screen was blinking unstopped due to multiple calls, but i have attached a faint image of it.
View Replies
View Related
Oct 14, 2014
How can I do a subtraction quiz program using input dialog quiz?
View Replies
View Related
Nov 13, 2014
I have been coding in my class at school (Grade 11 Computer science) and i just downloaded the program on my computer at home, unfortunately i cannot access my computer notes at home and i also dont remember certian specifics of coding, so my question is how would i get user input to create a program. The comments are the parts i dont remember. (I am trying to slowly build my memory with this stuff)
Here is my code so far:
import java.util;
[highlight=java]
public class hello_world {
public static void main(String[] args) {
string name;
//WHAT DO I PUT HERE????
[code]....
View Replies
View Related
Apr 19, 2014
I'm working on creating a dice game and getting the users input is giving me a really hard time. I have the main game working but this part I'm stuck on. The problem is that it's not giving me the chance to enter the second input when I ask for the game.
//Create Scanner object
Scanner keys = new Scanner(System.in);
//Get chips
[Code]....
*****This is what I get when I run it
run:
How much money would you like to change? 50
You now have $50 in chips.
What game do you want to play? You did not pick dice.
View Replies
View Related
Oct 11, 2014
Is there a way you can control user input?
For example, is it possible to only allow the user to enter digits?
View Replies
View Related
Feb 4, 2014
I have been given a piece of work to do for College that requires me to format user input. For example: This is an example of text that will have straight left and right margins after formatting
will transform into:
This..is..an.example
of..text..that..will
have..straight..left
and...right..margins
after.....formatting
The user inputs the width, in the case above being 20. Each line has to therefore be 20 characters long. All spaces are to be replaced with full stops.
This.is.an.example.o
f.text.that.will.hav
e.straight.left.and.
right.margins.after.
formatting..........
public class Main {
public static void main ( String args[])
[code]....
View Replies
View Related
Jul 2, 2014
Goal is to get user input and sum together. I've tried it different ways. This is the one with the least amount of problems.
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: java.lang.Integer.parseInt
ArrayList<Integer> leftCU = new ArrayList<>(); {
System.out.println
("Please enter one at a time the number of CU's for each class that is left to complete. Enter Q when done.");
leftCU.add(in.nextInt());
[Code] .....
View Replies
View Related
Jan 9, 2015
I am trying to get user input for a char value and am having some difficulty getting input for a char value.
Java Code:
//imports packages
import java.io.*;
import java.text.DecimalFormat;
class ModuleCulminatingTask {
public static void main (String args []) {
//declares variables
float var3, var4;
long var5 = (int)(Math.random()*10);
[code]....
View Replies
View Related
Apr 10, 2014
I'm not sure what is wrong with my code:
import java.util.Scanner;
public class Main {
int age = 0;
System.out.println("Enter your age: ");
// Read in values
age = in.nextInt();
}
The in is underlined, telling me "cannot find symbol"
View Replies
View Related
Apr 19, 2014
I'm working on creating a dice game and getting the users input is giving me a really hard time. I have the main game working but this part I'm stuck on. The problem is that it's not giving me the chance to enter the second input when I ask for the game.
//Create Scanner object
Scanner keys = new Scanner(System.in);
//Get chips
System.out.print("How much money would you like to change? ");
int chips = keys.nextInt();
System.out.println("You now have $" + chips + " in chips.");
[code]...
This is what I get when I run it run/How much money would you like to change?
View Replies
View Related
Mar 25, 2014
Java Code:
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
public class SortTable {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
[Code]...
View Replies
View Related