Want to ask how can i compare a textfield with a JDateChooser?
if ((JTextField)jDateChooser1.getDateEditor().getUiComponent()).getText() < txtdate) {
JOptionPane.showMessageDialog(null, "Cant choose a date that is more previous >than the present date!");
} else {
JOptionPane.showMessageDialog(null, "Book Successfully Borrowed!");
}
Related
I'm trying to get the user to input there name if it is left blank it will ask again, if they fill it out it sets a JLabel or hit cancel to get out.
My last if statement is wrong it does not like nameEnt.
public Player() {
//setBackground(Color.green);
setSize(600, 400);
name = new JLabel();//Input hint
JOptionPane nameOption = new JOptionPane();
String nameEnt = nameOption.showInputDialog("First Name: ");
if (!nameEnt.matches("[a-zA-Z]+")) {
name.setText(nameEnt);
}
if (nameEnt.length() == 0) {
//if this condition is true JOption stays until name is entered or canceled
}
if (nameEnt == nameOption.CANCEL_OPTION) {
System.exit(0);
}
}
The JOptionPane.CANCEL_OPTION is a static int field, and you can't compare String with int with ==.
Good practice
In your case you want to use ok and cancel button JOptionPane.showConfirmDialog and JOptionPane.showInputDialog() in one shot and this is not possible, i suggest to use this instead :
JTextField nameF = new JTextField(20);//create TextField
JPanel myPanel = new JPanel();//cerate JPanel
myPanel.add(new JLabel("Name"));
myPanel.add(nameF);//add your JTextField to your panel
int result;
do {
result = JOptionPane.showConfirmDialog(null, myPanel,
"Title of Panel", JOptionPane.OK_CANCEL_OPTION);//add your panel to JOptionPane
if (result == JOptionPane.OK_OPTION) {//if the user press OK then
if (nameF.getText().isEmpty()) {//check if the input is empty
//if this condition is true JOption stays until name is entered or canceled
} else if (!nameF.getText().matches("[a-zA-Z]+")) {//check if the input match with your regex
//name match exactly
//name.setText(nameF.getText());
}
}
} while (result != JOptionPane.CANCEL_OPTION);//If the user hit cancel then exit
As per the JOptionPane API, if the user cancels the dialog, null is returned.
And so the correct solution is to to not to use equals, but rather to check the return value for null and to do this first, before checking its length.
public Player() {
//setBackground(Color.green);
setSize(600, 400);
name = new JLabel();//Input hint
JOptionPane nameOption = new JOptionPane();
String nameEnt = nameOption.showInputDialog("First Name: ");
if (nameEnt == null) {
// user canceled. get out of here.
System.exit(0);
// or return;
// or throw some exception
}
if (!nameEnt.matches("[a-zA-Z]+")) {
name.setText(nameEnt);
}
if (nameEnt.length() == 0) {
//if this condition is true JOption stays until name is entered or canceled
}
// if (nameEnt == nameOption.CANCEL_OPTION) {
// System.exit(0);
// }
}
But why are you creating a JOptionPane this way? Better to use the static method of creation.
// don't use null as the first parameter if the GUI is already showing
String nameEnt = JOptionPane.showInputDialog(null, "First Name: ");
if (nameEnt == null) {
// user canceled. get out of here.
System.exit(0);
}
Or maybe something like this, if you're trying to loop to get input:
public Player() {
setSize(600, 400); // This is not good to do. Ask for details and I'll tell.
name = new JLabel();// Don't forget to add this to the GUI!
String nameEnt = "";
while (nameEnt.trim().isEmpty()) {
// if the GUI is already showing, pass a component from it as the first param here, not null
nameEnt = JOptionPane.showInputDialog(null, "First Name: ");
if (nameEnt == null) {
// user canceled. get out of here.
System.exit(0);
// or return;
// or throw some exception
} else if (!nameEnt.matches("[a-zA-Z]+")) {
name.setText(nameEnt);
} else {
// set it to "" so that we keep looping
nameEnt = "";
}
}
}
This question already has answers here:
How do I concatenate two strings in Java?
(23 answers)
Closed 6 years ago.
I have the following method in a test environment, I'm trying to attach the variable count "loginAttempts"
to the JOptionPane window so the user would know how many failed login attempts they've had so far.
Essentially, I want to concatenate another string and a variable to this line if possible .
JOptionPane.showMessageDialog(null, "Enter a valid username and a password. You've had ||loginAttempts|| "failed login attempts", "Error", JOptionPane.ERROR_MESSAGE);
Here's the method out of the application"
private void loginButton(java.awt.event.ActionEvent evt) {
username = userName.getText();
password = userPassword.getText();
if (username.equals(finalusername) && password.equals(finalpassword) && loginAttempts <= 3) {
loginSuccess = true;
} else {
loginSuccess = false;
loginAttempts = loginAttempts + 1;
}
if (loginSuccess) {
JOptionPane.showMessageDialog(null, "You have successfully logged in", "Success", JOptionPane.DEFAULT_OPTION);
} else if (loginAttempts > 3) {
JOptionPane.showMessageDialog(null, "You have had 3 failed login attempt, your account has been locked","Locked", JOptionPane.ERROR_MESSAGE);
} else if (!loginSuccess) {
JOptionPane.showMessageDialog(null, "Enter a valid username and a password", "Error", JOptionPane.ERROR_MESSAGE);
}
}
You can concatenate this way (replace the last else with that ) :
} else if (!loginSuccess) {
JOptionPane.showMessageDialog(null, "Enter a valid username and a password, Number of attempts : "+loginAttempts , "Error", JOptionPane.ERROR_MESSAGE);
}
So there is this certain part of my program where I can create an account and the created account will be inserted into my database. And I'm trying to code something where *refer to the code:
public void actionPerformed(ActionEvent e) {
String user = userField.getText().trim();
String pass = passField.getText().trim();
String conPass = confirmPass.getText().trim();
try{
// TODO Auto-generated method stub
if(e.getSource()==submit){
if (user.equals(user)&&pass.length()==0){
JOptionPane.showMessageDialog(null, "Fill in the empty field!");
}//check if the pass field is blank
else if(user.length()<5){
JOptionPane.showMessageDialog(null,"Username must be at least 5 characters!");
}
else if(user.equals(user)&&pass.equals(conPass)&&pass.length()!=0){
String sqlLogin = "insert into tblLogin (username,pssword) values ('"+user+"','"+pass+"')";;
getQuery(sqlLogin);
JOptionPane.showMessageDialog(null, "Account Successfully Created!");
create.dispose();
GUI gui = new GUI();
}//if(pass.equals(conPass))
else if(user.length()==0&&pass.length()==0){
JOptionPane.showMessageDialog(null, "Fill in the empty field!");
}//check if both fields are blank
else if (user.length()==0 &&pass.equals(pass)){
JOptionPane.showMessageDialog(null, "Fill in the empty field!");
}//check if user field is blank
else if(user.equals(user)&&pass!=conPass){
JOptionPane.showMessageDialog(null, "Password do not match!");
}//check if password and confirm pass matches
}
I dont really know how to say the problem but look in the if and else if statements, if the user meet one the those conditions, the program should print the JOptionPane thing. Except for the second else if.
You might be wondering why I put these codes at my else if
else if(user.equals(user)&&pass.equals(conPass)&&pass.length()!=0){
String sqlLogin = "insert into tblLogin (username,pssword) values ('"+user+"','"+pass+"')";;
getQuery(sqlLogin);
JOptionPane.showMessageDialog(null, "Account Successfully Created!");
create.dispose();
The reason for this is that, my program is having some logic error when I try to put it in if statement. Please help me with my code thanks :) Feel free to write a new code for me :DD
i might try something like this:
public static boolean isSet(String s){
if(s==null || "".equals(s)) return false;
return true;
}
//.... your validation here
if(isSet(user) && isSet(pass) && isSet(conPass) && pass.equals(conPass)){
//create account
}else{
//smth wrong eg. if(!pass.equals(conPass) { //wrongpass }
}
There will be a ShowInputDialog box for user to key in numbers. When user keys in character it will show a MessageDialog box to indicate invalid input.
How do I return to the previous showInputDialog for user to reenter the number after the user click on "OK" button on the MessageDialog box?
The codes:
public static void addStock(Stock s[],int index)
{
s[index] = new Stock();
try{
s[index].setNumber(Double.parseDouble(showInputDialog(null,"Enter number: ")));
}catch(NumberFormatException e){
showMessageDialog(null, "Invalid input. Enter digits only.", "Error!",ERROR_MESSAGE);
}
}
*Edit: It is an inventory system using dialog boxes as the interface to interact with user. For example, to add,delete,update stock in the system.
Try this one :
boolean isValSet = false;
while(!isValSet)
{
try {
String val=JOptionPane.showInputDialog(null,"Enter number: ");
double parseDouble = Double.parseDouble(val);
//set you parseDouble to your attribute.
isValSet=true;
} catch(NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Invalid input. Enter digits only.", "Error!",JOptionPane.ERROR_MESSAGE);
}
}
I'm using JDateChooser for a Javaapp (this is the 1st time i use it). I want to catch fault when the JDateChooser is empty with code like this:
if(dcs1.getDate().toString().isEmpty()){
lblOk.setText("Empty");
}else{
Date d = dcs1.getDate();
DateFormat df = new SimpleDateFormat("MM-dd-yyyy");
String t = df.format(d);
lblOk.setText(t);
}
JLabel lblOk;
JDateChooser dcs1;
But it dont work.
Any1 can help me plzz.
Date date;
date = JDateChooser.getDate();
if (date == null)
{
JOptionPane.showMessageDialog(null, "Choose Date from Right Box.", "Error", JOptionPane.ERROR_MESSAGE);
JDateChooser.grabFocus();
return false;
}
String s = ((JTextField)dateChooser.getDateEditor().getUiComponent()).getText();
if (s.equals("") {
JOptionPane.showMessageDialog(null, "Please type birthday", "Warning!", JOptionPane.ERROR_MESSAGE);
}
if (jDateChooserBirthDate.getDate() == null) {
JOptionPane.showMessageDialog(null, "Please type birthday", "Warning!", JOptionPane.ERROR_MESSAGE);
}
if(jDateChooser1.getDate() == null){
JOptionPane.showMessageDialog(this, "Date not selected","No selection",JOptionPane.INFORMATION_MESSAGE);
jDateChooser1.requestFocusInWindow();
}
I used DateChooserCombo. and The following code worked for me.
String test = dateChooserID.getText();
if(!test.equals("")){
//DO WHATEVER YOU WANT (DATE IS SELECTED IN THIS CASE);
}
else{
JOptionPane.showMessageDialog(this, "date not choosen");
}